版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、5只蚂蚁走木棍问题优美的 Java非递归解法题目描述:有一根27厘米的细木杆,在第 3厘米、7厘米、11厘米、17厘米、23厘米这五个位 置上各有一只蚂蚁。木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是 任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调 头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。看到很多人发布了他们的解法,我觉得都过于复杂并不甚清晰,以下是我的思路和实现.1 .思路与建模* 将木杆看成0至27的坐标,5只蚂蚁分别在 3, 7,11,17, 23的位置* 创建 enum D
2、irectory Left, Right* 创建Ant类,蚂蚁可以爬行(walk),掉头(shift),并且如果爬出能把自己从蚂蚁队列中移出 去,* 创建Controller类,给定蚂蚁的初始方向,Controller类可以计算出在这种初始方向下蚂蚁全 部爬出需要的时间.* 创建Simulator类,它给出蚂蚁初始方向的所有组合,并使用Controller执行得到所有时间选择最大值和最小值.2 .实现在Model包内:view plaincopy to clipboardprint? package ;public enum Direction Left, Rightpackage ;publ
3、ic enum Direction Left, Right)view plaincopy to clipboardprint?package ;import ;public class Ant public static final int LENGTH = 27;private int position;Direction direction;/* 在木杆上的蚂蚁队列* /ArrayList<Ant> list;public Ant( int p, Direction dir, ArrayList<Ant> list ) this.position = p;this.
4、direction = dir;this.list = list;/*蚂蚁行走* /public void walk()if( direction = Direction.Right ) position+;else position-;/如果大于最大长度获小于0即视为爬出木杆if( position >= LENGTH | position <= 0 ) list.remove( this );/*蚂蚁调头,行走一秒后查看自己是不是需要调头*/public void shift()int index = list.indexOf( this );if( index = 0 &am
5、p;& list.size() > 1 )if( this.position = list.get(1).position ) doShift();)else if( index = list.size()-1 && index > 0 )if( this.position = list.get(index-1).position ) doShift();else if( index > 0 && index < list.size()-2)list.get(index+1).position | this.positionif(
6、 this.positionlist.get(index-1).position) doShift();)private void doShift()this.direction = (this.direction = Direction.Left) ? Direction.Right : Direction.Left; ) ) package ;import ;public class Ant public static final int LENGTH = 27;private int position;Direction direction;/* 在木杆上的蚂蚁队列*/ArrayList
7、<Ant> list;public Ant( int p, Direction dir, ArrayList<Ant> list ) this.position = p;this.direction = dir;this.list = list;)/* 蚂蚁行走* /public void walk()if( direction = Direction.Right ) position+;else position-;/如果大于最大长度获小于0即视为爬出木杆if( position >= LENGTH | position <= 0 ) list.remov
8、e( this );* 蚂蚁调头,行走一秒后查看自己是不是需要调头* /public void shift()int index = list.indexOf( this );if( index = 0 && list.size() > 1 )if( this.position = list.get(1).position ) doShift();else if( index = list.size()-1 && index > 0 )if( this.position = list.get(index-1).position ) doShift()
9、;else if( index > 0 && index < list.size()-2)if( this.position = list.get(index+1).position | this.position list.get(index-1).position)doShift();private void doShift()this.direction = (this.direction = Direction.Left) ? Direction.Right : Direction.Left;在 Controller 包内 view plaincopy to
10、 clipboardprint?package ;import ;import ;import ;public class Controller /* 蚂蚁初始位置* /private int positions = 3, 7, 11, 17, 23 );/* 蚂蚁初始方向* /private Direction dir;private long timer = 0;/* 指定蚂蚁初始方向,创建控制器* param dir* /public Controller( Direction dir ) this.dir = dir;)public long start() ArrayList<
11、Ant> list = this,init();/*蚂蚁队列为空即全部爬出*/while( list.size() != 0 ) /爬行for( int i = 0; i < list.size(); i+ )Ant ant = list.get(i);ant.walk();)/掉头 for( int i = 0; i < list.size(); i+ )Ant ant = list.get(i); ant.shift();)timer+;) return timer;)/* 创建初始蚂蚁队列* /private ArrayList<Ant> init()Arr
12、ayList<Ant> list = new ArrayList<Ant>();for( int i = 0; i < positions.length; i+ )Ant ant = new Ant( positionsi, diri, list ); list.add( ant );) return list;)package ;import ;import ;import ;public class Controller /* 蚂蚁初始位置* /private int positions = 3, 7, 11, 17, 23 );/* 蚂蚁初始方向* /pri
13、vate Direction dir;private long timer = 0;* 指定蚂蚁初始方向,创建控制器* param dir* /public Controller( Direction dir ) this.dir = dir;)public long start() ArrayList<Ant> list = this,init();/*蚂蚁队列为空即全部爬出*/while( list.size() != 0 ) /爬行for( int i = 0; i < list.size(); i+ )Ant ant = list.get(i);ant.walk();
14、)/掉头for( int i = 0; i < list.size(); i+ )Ant ant = list.get(i);ant.shift();)timer+;) return timer;)/* 创建初始蚂蚁队列* /private ArrayList<Ant> init()ArrayList<Ant> list = new ArrayList<Ant>();for( int i = 0; i < positions.length; i+ )Ant ant = new Ant( positionsi, diri, list ); list
15、.add( ant );) return list;)在Root包内view plaincopy to clipboardprint?package cn.dfeng;import ;import ;public class Simulator private long longest = 0;private long shortest = Long.MAX_VALUE;/* 开始模拟* /public void simulate() for (int i = 0; i < 32; i+) Controller con = new Controller(this.getDirection
16、s(i);long time = con.start();if( time > longest ) longest = time;)if( time < shortest ) shortest = time;)"Time: " + time );)/*创建蚂蚁初始位置*/private Direction getDirections(int i) Direction dirs = new Direction5;String binString = Integer.toBinaryString(i);StringBuilder sb = new StringBui
17、lder();int lack = 5 - binString.length();for( int c = 0; c < lack; c+ )sb.append('O');)sb.append( binString );binString = sb.toString();int p = 0;while( p < binString.length() ) if (binString.charAt(p) = '0') dirsp = Direction.Left; else dirsp = Direction.Right;p+;"Round:
18、" + binString );return dirs;/*打印结果*/public void getResult() "Longest time %d.nShortest Time: %d", longest, shortest);public static void main( String args ) Simulator sim = new Simulator();sim.simulate();sim.getResult();package cn.dfeng;import ;import ;public class Simulator private lo
19、ng longest = 0;private long shortest = Long.MAX_VALUE;/*开始模拟*/public void simulate() for (int i = 0; i < 32; i+) Controller con = new Controller(this.getDirections(i); long time = con.start();if( time > longest ) longest = time;)if( time < shortest ) shortest = time;)"Time: " + time );)/*创建蚂蚁初始位置*/private Direction getDirections(int i) Direction dirs = new Direction5;String binString = Integer.toBinaryString(i);StringBuilder sb = new StringBuilder();int lack = 5 - binString.length();for( int c = 0; c < lack;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年浙江省临海市高一数学下册期末考试模拟测试卷附完整答案【名校卷】
- 2026年湖北省麻城市高一数学下册期末考试模拟检测卷及答案(名校卷)
- 2026年湖南省耒阳市高一数学下册期末考试模拟卷(满分必刷)附答案
- 2026年河北省涿州市高一数学下册期末考试模拟卷带答案(能力提升)
- 2026年湖北省石首市高一数学下册期末考试模拟卷及参考答案【研优卷】
- 2026年吉林省珲春市高一数学下册期末考试模拟检测卷含答案【达标题】
- 2026年安徽省天长市高一数学下册期末考试模拟考试卷A4版附答案
- 2026年湖北省汉川市高一数学下册期末考试模拟测试卷及参考答案(研优卷)
- 2026年广东省高州市高一数学下册期末考试模拟考试卷(达标题)附答案
- 2026年广东省化州市高一数学下册期末考试模拟检测卷(考试直接用)附答案
- 安徽离任村干部管理办法
- 高考数学三角函数专题知识训练100题含答案(5份)
- 业务介绍费合同或协议
- 《产科危急重症早期识别中国专家共识(2024年版)》解读课件
- 砧板刀具分色管理制度
- 卡通形象吉祥物设计过程
- 口腔分类分级管理制度
- 养生馆承包合同协议书
- SL631水利水电工程单元工程施工质量验收标准第1部分:土石方工程
- 2025中考重点中学自主招生数学试题及答案详解
- 虚拟电厂运营
评论
0/150
提交评论