一个编程题,用java 语言解决下,思路有点乱。

有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米 这五个位置上各有一只蚂蚁。木杆很细,只能同时通过一只蚂蚁。
开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。 当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。
假设蚂蚁们每秒钟可以走一厘米的距离。 编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。

import java.util.ArrayList;
import java.util.Random;

public class AntTest {
public static final int DIS = 27;
public int[] positionList = { 3, 7, 11, 17, 23 };
ArrayList<Ant> antList = new ArrayList<Ant>();
public int max = 0;
public int min = 10000000;

public static void main(String []args){
AntTest t = new AntTest();
System.out.println("Max time is "+t.max);
System.out.println("Min time is "+t.min);
}

AntTest() {
for(int i=0;i<100000;i++){
this.generateAnts();
System.out.println("Move start");
while(!this.allFinish()){
this.run();
}
System.out.println("End");
this.printResult();
}
}

public void generateAnts(){
if (antList.size() != 0){
antList.clear();
}
Random r = new Random();
int count = 1;
for(int i:positionList){
int direction;
if(r.nextBoolean()){
direction = 1;
}else{
direction = -1;
}
antList.add( new Ant(count++,i,direction));
}
}

public void run() {
for(Ant a:antList){
if(!a.finish){
a.collision();
a.run();
}
}
}

public boolean allFinish(){
for(Ant a:antList){
if(!a.finish){
return false;
}
}
return true;
}

public void printResult(){

for(Ant a:antList){
if(a.time > max) max = a.time;
if(a.time < min) min = a.time;
System.out.println("Ant result "+a.toString());
}

}

public int getTimeResult(boolean flag){
int time;
if(flag){
time = 0;
}else{
time = 100000;
}
for(Ant a:antList){
if(flag){
if(a.time > time){
time = a.time;
}
}else{
if(a.time < time)
time = a.time;
}
}
return time;
}

public class Ant {
public static final int SPEED = 1;
private int id;
private int time;
private int position;
private int direction;
private boolean finish;
private boolean hasBeenCheck;

Ant(int id,int position, int direction) {
this.id = id;
this.position = position;
this.direction = direction;
this.time = 0;
this.finish = false;
this.hasBeenCheck = false;
}

public int getPosition() {
return position;
}

public void setPosition(int position) {
this.position = position;
}

public int getDirection() {
return direction;
}

public void setDirection(int direction) {
this.direction = direction;
}

public void changeDirection() {
this.direction *= -1;
}

public void run() {
// System.out.println("A ant move at position"+position+" in "+direction+" direction");
if (position == DIS || position == 0) {
this.finish = true;
return;
} else {
this.hasBeenCheck = false;
this.time++;
if (direction == 1) {
this.position += SPEED;
}
if (direction == -1) {
this.position -= SPEED;
}
}
}

public int getTime() {
return time;
}

public void setTime(int time) {
this.time = time;
}

public boolean isFinish() {
return finish;
}

public void setFinish(boolean finish) {
this.finish = finish;
}

public boolean collision() {
if (!this.hasBeenCheck) {
for (Ant a : antList) {
if (a.position == this.position) {
a.changeDirection();
this.changeDirection();
}
}
}
return false;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return "Id:"+id+" Time:"+this.time+" position:"+position+" direction:"+direction;
}

}

}

其实有两种方案去解决这个问题。。我用了比较笨的方法就是因为蚂蚁的头朝向是随机的所以我随机模拟了10W次事件期间最大时间和最少时间记录下来。。
结果是
Max time is 24
Min time is 3
如果你只想知道一次的。。把100000改成1就行了
还有种方案是用排列组合的方法试所有的组合。。。可以参考我给你的连接。。也是我回答的。。

参考资料:http://zhidao.baidu.com/question/284776107.html

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-07-07
package java.util;
/*
* 有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米 这五个位置上各有一只蚂蚁。木杆很细,只能同时通过一只蚂蚁。
开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。 当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。
假设蚂蚁们每秒钟可以走一厘米的距离。 编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
*/
class ant{
public final static long stickLength = 27;

private int direct; //0往棍首,1往棍尾
private int position; //相对棍首的位置
private boolean flag; //是否已经离开 1离开
private int aId; //蚂蚁编号

public boolean chgDrct(){
direct = (direct + 1)%2;
return true;
}

public boolean moveOn(){
if (flag) return false;
if (1==direct) position++;
else position--;
if ((position<=0)||(position>stickLength)) {
position = 0;
flag = true;
}
return true;
}

public int getPosition() {
return position;
}

public void setPosition(int position) {
this.position = position;
}

public boolean getFlag() {
return flag;
}

public void setant(int aid,int position){
setPosition(position);
setAId(aid);
Random rand = new Random();
direct = rand.nextInt()%2;
flag = false;
}

public void print(){
System.out.print("当前第"+aId+"只蚂蚁");
if (flag)
System.out.println("已离开杆");
else {
System.out.print("处于离杆首"+position+"厘米处,");
if (1==direct)
System.out.println("正朝杆尾爬!");
else
System.out.println("正朝棍首爬!");
}
}

public void setAId(int id) {
aId = id;
}
}

class stick{
public final static int stickLength = 27;
public stick(){
ant[] a = new ant[5];
int[] initp={3,7,11,17,23};
for (int i=0;i<5;i++)
a[i].setant(i+1,initp[i]);
}
private ant[] a = new ant[5];
private int[] p = new int[stickLength+1];
private boolean endflag; //结束标识
private int conlictcnt;
private int timepassed;

private boolean checkend(){
endflag = true;
for (int i=0;i<5;i++)
endflag &= a[i].getFlag();
return endflag;
}

private boolean dealconflic(){
boolean flag=false;
for (int i=1;(i<=stickLength)&&(!flag);i++)
if (p[i]>1)
flag = true;
//有冲突,进行处理
if (flag){
for (int i=1;(i<=stickLength)&&(!flag);i++)
if (p[i]>1)
for (int j=0;j<5;j++)
if (a[j].getPosition()==i){
a[j].chgDrct();
a[j].moveOn();
}
return true;
}
else return false;
}

public void print(){
for (int i=0;i<5;i++)
a[i].print();
}

public boolean timepass(){
int i;
//清空杆的记录
for (i=0;i<stickLength;i++)
p[i]=0;
timepassed++;
for (i=0;i<5;i++) {
a[i].moveOn();
p[a[i].getPosition()]++;
}
//发生冲突,进行处理
if (dealconflic())
conlictcnt++;
return checkend();
}

public int run(){
int i=1;
while ((true)&&(!timepass())) i++;
return i;
}
}

public class fiveAnt {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
stick aexample = new stick();
System.out.println("花费了"+aexample.run()+"秒");
}

}

//还没完全完成,应该修改setant,设置每个蚂蚁的方向。
//共32种情况,然后比较最小时间和最大时间。本回答被提问者采纳
相似回答