蚂蚁(java新手上路)


/*
 有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、18厘米、23厘米这五个位置上各有一只蚂蚁。
 木杆很细,不能同时通过两只蚂蚁。
 开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。
 当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。
 假设蚂蚁们每秒钟可以走一厘米的距离。
 编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
 要求:用类模拟出蚂蚁的行为特性,
 进而模拟出五只蚂蚁在木杆上的运行过程来编程求解。
 不能通过数学的方式直接用公式计算。
 */


class Ant{
	private boolean isAlive;
	private int direction;//-1左  :1右
	private int position;//不能改
	
	public Ant(){
		this(true,-1,11);
	}
	public Ant(int position){
		this(true,-1,position);//初始化为左
	}
	public Ant(boolean isAlive,int direction,int position){
		this.isAlive=isAlive;
		this.direction=direction;
		this.position=position;
	}
	public void move(){//多次测试,不能使用这方法
		if(this.direction>0){
			this.position += this.direction;
		}else{
			this.position += this.direction;
		}
	}
	public void set(int position){
		this.set(true,-1,position);
		System.out.println(position);
	}
	public void setisAlive(boolean isAlive){
		this.isAlive=isAlive;
	}
	public void set(boolean isAlive,int direction,int positon){
		this.isAlive=true;
		this.direction=direction;
		this.position=position;
	}
	public int getPosition(){
		return this.position;
	}
	public int getDirection(){
		return this.direction;
	}
	public String toString(){
		return isAlive+"\t"+direction+"\t"+position+"\t";
	}
	public boolean getisAlive(){
		return this.isAlive;
	}
}

class woodenPole{//木杆上有五只蚂蚁
	int shaft;//木杆
	int count;
	
	public woodenPole(){
	}
	public woodenPole(int shaft){
		this(shaft,0);
	}
	public woodenPole(int shaft,int count){
		this.shaft=shaft;
		this.count=count;
	}
	public void note(){
		System.out.println("----------------------------------------------------------");
		System.out.println("有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、18厘米、23厘米这五个位置上");
		System.out.println("各有一只蚂蚁。");
		System.out.println("木杆很细,不能同时通过两只蚂蚁。");
 		System.out.println("开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。");
 		System.out.println("当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。");
 		System.out.println("假设蚂蚁们每秒钟可以走一厘米的距离。");
 		System.out.println("编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。");
 		System.out.println("要求:用类模拟出蚂蚁的行为特性,");
 		System.out.println("进而模拟出五只蚂蚁在木杆上的运行过程来编程求解。");
 		System.out.println("不能通过数学的方式直接用公式计算。");
		System.out.println("----------------------------------------------------------");
	}
	public void activate(Ant[] a){
		for(int i=0;i<a.length;i++){
			a[i].setisAlive(true);
		}
		this.enterArray(a);
	}
	public void shift(Ant[] a,int[][] b){
		for(int i=1;i<b[0].length;i++){//先判断蚂蚁是否相遇,也就是他们的距离差是否为0
			if( ( b[1][i]-b[1][i-1] == 1 ) || ( b[1][i]-b[1][i-1] == 2 ) ){
				if( b[0][i-1]==1&&b[0][i]==-1 ){
					b[0][i-1]  = -b[0][i-1];
					b[0][i]    = -b[0][i];
				}
			}
		}
	}
	public void orientation(int[][] arr,int n){//判断方向,核心二
		int[] byteNumber=this.byNumber(arr[0].length);
		for(int j=0;j<arr[0].length;j++){//先判断方向
			int by= n&byteNumber[j];//与运算
			if( by == 0 ){
				arr[0][j]=-1;
			}else{
				arr[0][j]= 1;
			}
		}
	}
	public void pursuitOfAHome(Ant[] a,int[][] b){//核心一
		this.count++;
		for(int i=0;i<b[0].length;i++){//五只蚂蚁同时行动
			if( a[i].getisAlive() ){
				if( b[1][i] <= 0||b[1][i] >= this.shaft){
					a[i].setisAlive(false);
					b[2][i]=this.count-1;
				}
				b[1][i] += b[0][i];
			}
		}
		if(this.count>40){//防止死循环的鸿沟
			return ;
		}
		if( !this.isAllDeath(a) ){
			pursuitOfAHome(a,b);
		}
	}
	public void print(int[] a){
		System.out.println();
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	public void print(Ant[] a){
		for(int i=0;i<a.length;i++){
			System.out.println(a[i]);
		}
	}
	public void print(int[][] arr){
		System.out.println();
		for(int i=0;i<arr.length;i++){
			for(int j=0;j<arr[i].length;j++)
				System.out.print(arr[i][j]+" ");
			System.out.println();
		}
		System.out.println();
	}
	public int sumAnt(int[] b){
		int sum=0;
		for(int i=0;i<b.length;i++){
			sum += b[i];
		}
		return sum;
	}
	public int slowestAnt(int[] b){
		int slowest=b[0];
		for(int i=1;i<b.length;i++){
			if(slowest<b[i])
				slowest=b[i];
		}
		//this.print(b);
		return slowest;
	}
	public int[] byNumber(int n){//用来相位取数,被orientation调用
		int[] arr=new int[n];
		arr[0]=1;
		for(int i=1;i<n;i++){//进行位运算的值
			arr[i]=(int)Math.pow(2,i);
		}
		return arr;
	}
	public int[][] enterArray(Ant[] a){//木杆上蚂蚁的位置与方向放入二位数组中,个更改的,重新传入就不变
		int[][] b=new int[3][a.length];
		for(int i=0;i<5;i++){
			b[0][i] = a[i].getDirection();
			b[1][i] = a[i].getPosition();
		}
		return b;
	}
	public boolean isAllDeath(Ant[] a){
		boolean flag=true;
		for(int i=0;i<a.length;i++)
			if( a[i].getisAlive() )
				flag=false;
		if(flag)
			return true;
		else
			return false;
	}
	public static void main(String args[]){
		Ant[] a=new Ant[]{new Ant(true,-1,3),new Ant(7),new Ant(11),new Ant(17),new Ant(23)};
		woodenPole fiveAnt=new woodenPole(27);
		fiveAnt.note();
		int[][] arr=fiveAnt.enterArray(a);
		fiveAnt.pursuitOfAHome(a,arr);
		int min,max,y;
		min=max=fiveAnt.slowestAnt(arr[2]);
		for(int i=1;i<32;i++){//核心算法,计算各种走法
			fiveAnt.count=0;
			fiveAnt.activate(a);//激活蚂蚁,历史重来。
			arr=fiveAnt.enterArray(a);//使位置不变
			fiveAnt.orientation(arr,i);//判断方向
			fiveAnt.pursuitOfAHome(a,arr);//a传入是为了判生死的
			y=fiveAnt.slowestAnt(arr[2]);
			if( min>y ){
				min=y;
			}
			if( max<y ){
				max=y;
			}
		}
		System.out.println("min:"+min+"\nmax:"+max);
	}
}


/*
 有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、18厘米、23厘米这五个位置上各有一只蚂蚁。
 木杆很细,不能同时通过两只蚂蚁。
 开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。
 当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。
 假设蚂蚁们每秒钟可以走一厘米的距离。
 编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
 要求:用类模拟出蚂蚁的行为特性,
 进而模拟出五只蚂蚁在木杆上的运行过程来编程求解。
 不能通过数学的方式直接用公式计算。
 */


写了两天,不容易啊!





























  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值