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