再谈蚂蚁的问题

今天将前面留的蚂蚁问题解决了,从中的收获那真的是大大的多啊。 虽然这个问题不是完全由我自己解决的(好吧,大部分的思路是从java私塾老师的视频里看到的),但是在我自己编写这个程序的代码得时候我注意了代码的规范行,总体来说可读性比那个老师在视频里领着做的写的代码要好点。以下是代码:
 
/*
1:有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。
木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,
但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。
编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。

*/




public class MaYi{

	public static void main(String[] args){
		MaYi maYi = new MaYi();
		maYi.antMoveProcess();
	}
	
	public void antMoveProcess(){
	//创建 5 个 ant对象
		Ant[] ant = new Ant[5];
		for(int i=0; i<5; i++){
			ant[i] = new Ant();
		}
	//通过 5 个循环来实现 5个ant实例的随机方向
		for(int antOneDirection=Ant.LEFT; antOneDirection<=Ant.RIGHT; antOneDirection++){
			for(int antTwoDirection=Ant.LEFT; antTwoDirection<=Ant.RIGHT; antTwoDirection++){
				for(int antThreeDirection=Ant.LEFT; antThreeDirection<=Ant.RIGHT; antThreeDirection++){
					for(int antFourDirection=Ant.LEFT; antFourDirection<=Ant.RIGHT; antFourDirection++){
						for(int antFiveDirection=Ant.LEFT; antFiveDirection<=Ant.RIGHT; antFiveDirection++){
						// 初始化 5 个ant实例 
									ant[0].currentSite = 3;
									ant[0].direction = antOneDirection;
									ant[0].isLeft = false;
									
									ant[1].currentSite = 7;
									ant[1].direction = antTwoDirection;
									ant[1].isLeft = false;
									
									ant[2].currentSite = 11;
									ant[2].direction = antThreeDirection;
									ant[2].isLeft = false;
									
									ant[3].currentSite = 17;
									ant[3].direction = antFourDirection;
									ant[3].isLeft = false;
									
									ant[4].currentSite = 23;
									ant[4].direction = antFiveDirection;
									ant[4].isLeft = false;
									
									// 通过一个循环来模拟时间 5 个and实例的运动 也是在这个模拟时间内
									for(int time=1; time<1000; time++){ //注意时间是从1开始的 不能从0开始
										if(!ant[0].isLeft){
											ant[0].step();
										}
										if(!ant[1].isLeft){
											ant[1].step();
										}
										if(!ant[2].isLeft){
											ant[2].step();
										}
										if(!ant[3].isLeft){
											ant[3].step();
										}
										if(!ant[4].isLeft){
											ant[4].step();
										}
										
										if((ant[0].currentSite == ant[1].currentSite) && (ant[0].direction == Ant.RIGHT) && ant[1].direction == Ant.LEFT){
											ant[0].changeDirection();
											ant[1].changeDirection();
										}
										if((ant[1].currentSite == ant[2].currentSite) && (ant[1].direction == Ant.RIGHT) && ant[2].direction == Ant.LEFT){
											ant[1].changeDirection();
											ant[2].changeDirection();
										}
										if((ant[2].currentSite == ant[3].currentSite) && (ant[2].direction == Ant.RIGHT) && ant[3].direction == Ant.LEFT){
											ant[2].changeDirection();
											ant[3].changeDirection();
										}
										if((ant[3].currentSite == ant[4].currentSite) && (ant[3].direction == Ant.RIGHT) && ant[4].direction == Ant.LEFT){
											ant[3].changeDirection();
											ant[4].changeDirection();
										}
										
										if(ant[0].isLeft && ant[1].isLeft && ant[2].isLeft && ant[3].isLeft && ant[4].isLeft){
											System.out.println("The Time is " + time);
											break;
										}
									}
						}
					}
				}
			}
		}
	}
}


class Ant{

	public static final int LEFT = 1;
	public static final int RIGHT = 2;
	public int currentSite;
	public int direction;
	public boolean isLeft = false;
	
	public void step(){
		if(direction == LEFT){
			currentSite = currentSite - 1;
		}else{
			currentSite = currentSite + 1;
		}
		
		if((currentSite == 0) || (currentSite == 27)){
			isLeft = true;
		}
	}
	
	public void changeDirection(){
		if(direction == LEFT){
			direction = RIGHT;
		}else {
			direction = LEFT;
		}
	}
}


通过这个程序呢,让我真正的感受到什么是面向对象语言,我刚拿到这个题目的时候,还是用面向过程的思维去思考这个题,这就在我写代码的时候脑中一片混乱,无从下手。经过老师的视频讲说我知道了,对待一个问题的时候,要想将问题里得对象弄出来,然后根据提议一点一点的解决它。学习java那么长时间了,面向对象的思想还是没有进入自己的脑子里,这一定要注意。

这个代码是根据题目思路来写的,但是如果你仔细的分析题目了以后可以发现,完全可以将两只蚂蚁碰头后调头这个要求去掉,因为不管他们碰不碰头 走的路线都是一样的。所以可以完全将那个changeDirection()函数删除点。这样效率会更高。
这也提醒我们,面对一个问题的时候要多加思考,将问题中无关紧要的条件去掉,这样在编写代码的时候能带来很大的方便行。 代码也不会显得啰嗦臃肿。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值