婆罗门
精华
|
战斗力 鹅
|
回帖 0
注册时间 2001-12-24
|
把具体算法给你看算了
public void fillQuest(clsBottle bottleA,clsBottle bottleB,int target)
{
TraceElementType temp = new TraceElementType();
//tag this spot
this.TM.setTag(bottleA.getCurrent(),bottleB.getCurrent(),1);
//print trace
this.TraceStack.printTrace(bottleA.getVolume(),bottleB.getVolume());
if(bottleA.getCurrent() == target) {
this.TraceStack.printTrace(bottleA.getVolume(),bottleB.getVolume());
System.out.println(\"finished\");
System.exit(0);
return;
}
if(bottleB.getCurrent() == target) {
this.TraceStack.printTrace(bottleA.getVolume(),bottleB.getVolume());
System.out.println(\"finished\");
System.exit(0);
return;
}
if((bottleA.getCurrent() - bottleB.getVolume() + bottleB.getCurrent()) == target) {
bottleA.fill(bottleB);
this.TraceStack.printTrace(bottleA.getVolume(),bottleB.getVolume());
System.out.println(\"finished\");
System.exit(0);
return;
}
if((bottleB.getCurrent() - bottleA.getVolume() + bottleA.getCurrent()) == target) {
bottleB.fill(bottleA);
this.TraceStack.printTrace(bottleA.getVolume(),bottleB.getVolume());
System.out.println(\"finished\");
System.exit(0);
return;
}
if((bottleB.getCurrent() + bottleA.getCurrent()) == target)
{
if(bottleA.getVolume() > bottleB.getVolume())
{
bottleB.fill(bottleA);
this.TraceStack.printTrace(bottleA.getVolume(),bottleB.getVolume());
System.out.println(\"finished\");
System.exit(0);
return;
}
else
{
bottleA.fill(bottleB);
this.TraceStack.printTrace(bottleA.getVolume(),bottleB.getVolume());
System.out.println(\"finished\");
System.exit(0);
return;
}
}
//action
temp.setApart(bottleA.getCurrent());
temp.setBpart(bottleB.getCurrent());
//1:splash A
bottleA.splash();
if(this.TM.getTag(bottleA.getCurrent(),bottleB.getCurrent()) == 1) {
bottleA.setCurrent(temp.getApart());
bottleB.setCurrent(temp.getBpart());
}
else {
this.TE.setApart(bottleA.getCurrent());
this.TE.setBpart(bottleB.getCurrent());
this.TraceStack.push(this.TE);
System.out.println(bottleA.getName() + \" splash\");
this.fillQuest(bottleA,bottleB,this.target);
}
//2:splash B
bottleB.splash();
if(this.TM.getTag(bottleA.getCurrent(),bottleB.getCurrent()) == 1) {
bottleA.setCurrent(temp.getApart());
bottleB.setCurrent(temp.getBpart());
}
else {
this.TE.setApart(bottleA.getCurrent());
this.TE.setBpart(bottleB.getCurrent());
this.TraceStack.push(this.TE);
System.out.println(bottleB.getName() + \" splash\");
this.fillQuest(bottleA,bottleB,this.target);
}
//3:reload A
bottleA.reload();
if(this.TM.getTag(bottleA.getCurrent(),bottleB.getCurrent()) == 1) {
bottleA.setCurrent(temp.getApart());
bottleB.setCurrent(temp.getBpart());
}
else {
this.TE.setApart(bottleA.getCurrent());
this.TE.setBpart(bottleB.getCurrent());
this.TraceStack.push(this.TE);
System.out.println(bottleA.getName() + \" load\");
this.fillQuest(bottleA,bottleB,this.target);
}
//4:reload B
bottleB.reload();
if(this.TM.getTag(bottleA.getCurrent(),bottleB.getCurrent()) == 1) {
bottleA.setCurrent(temp.getApart());
bottleB.setCurrent(temp.getBpart());
}
else {
this.TE.setApart(bottleA.getCurrent());
this.TE.setBpart(bottleB.getCurrent());
this.TraceStack.push(this.TE);
System.out.println(bottleB.getName() + \" load\");
this.fillQuest(bottleA,bottleB,this.target);
}
//5: A to B
bottleA.fill(bottleB);
if(this.TM.getTag(bottleA.getCurrent(),bottleB.getCurrent()) == 1) {
bottleA.setCurrent(temp.getApart());
bottleB.setCurrent(temp.getBpart());
}
else {
this.TE.setApart(bottleA.getCurrent());
this.TE.setBpart(bottleB.getCurrent());
this.TraceStack.push(this.TE);
System.out.println(bottleA.getName() + \" to \" + bottleB.getName());
this.fillQuest(bottleA,bottleB,this.target);
}
//6:B to A
bottleB.fill(bottleA);
if(this.TM.getTag(bottleA.getCurrent(),bottleB.getCurrent()) == 1) {
bottleA.setCurrent(temp.getApart());
bottleB.setCurrent(temp.getBpart());
}
else {
this.TE.setApart(bottleA.getCurrent());
this.TE.setBpart(bottleB.getCurrent());
this.TraceStack.push(this.TE);
System.out.println(bottleB.getName() + \" to \" + bottleA.getName());
this.fillQuest(bottleA,bottleB,this.target);
}
//7: back
this.TE = this.TraceStack.pop();
bottleA.setCurrent(this.TE.getApart());
bottleB.setCurrent(this.TE.getBpart());
this.fillQuest(bottleA,bottleB,this.target);
}
public clsFillWater() {
this.TE = new TraceElementType();
this.TM = new TagMatrix(100,100);
this.TraceStack = new Tstack(400);
clsBottle bottleA = new clsBottle(\"A\");
clsBottle bottleB = new clsBottle(\"B\");
bottleA.setVolume(11);
bottleB.setVolume(9);
this.target = 4;
System.out.println(\"[start: bottleA\'s volume = \" + bottleA.getVolume() +
\" , bottleB\'s volume = \" + bottleB.getVolume() +
\" , target = \" + this.target + \"]\");
this.TraceStack.push(this.TE);
this.fillQuest(bottleA,bottleB,this.target);
}
public static void main(String[] args) {
clsFillWater clsFillWater1 = new clsFillWater();
}
}
------------------------------------------------------------------
运行结果(A=5 B=3 要求=4)
[start: bottleA\'s volume = 5 , bottleB\'s volume = 3 , target = 4]
(0,0)
A load
(0,0) (5,0)
B load
(0,0) (5,0) (5,3)
A splash
(0,0) (5,0) (5,3) (0,3)
B to A
(0,0) (5,0) (5,3) (0,3) (3,0)
B load
(0,0) (5,0) (5,3) (0,3) (3,0) (3,3)
B to A
(0,0) (5,0) (5,3) (0,3) (3,0) (3,3) (5,1)
A splash
(0,0) (5,0) (5,3) (0,3) (3,0) (3,3) (5,1) (0,1)
B to A
(0,0) (5,0) (5,3) (0,3) (3,0) (3,3) (5,1) (0,1) (1,0)
B load
(0,0) (5,0) (5,3) (0,3) (3,0) (3,3) (5,1) (0,1) (1,0) (1,3)
B to A
(0,0) (5,0) (5,3) (0,3) (3,0) (3,3) (5,1) (0,1) (1,0) (1,3) (4,0)
finished
--------------------------------------------------------------------
(A=11 B=9 要求=4)
[start: bottleA\'s volume = 11 , bottleB\'s volume = 9 , target = 4]
(0,0)
A load
(0,0) (11,0)
B load
(0,0) (11,0) (11,9)
A splash
(0,0) (11,0) (11,9) (0,9)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0)
B load
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7)
A splash
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0)
B load
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5)
A splash
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0)
B load
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3)
A splash
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0)
B load
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1)
A splash
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0)
B load
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0)
B load
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9) (11,8)
A splash
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9) (11,8) (0,8)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9) (11,8) (0,8) (8,0)
B load
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9) (11,8) (0,8) (8,0) (8,9)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9) (11,8) (0,8) (8,0) (8,9) (11,6)
A splash
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9) (11,8) (0,8) (8,0) (8,9) (11,6) (0,6)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9) (11,8) (0,8) (8,0) (8,9) (11,6) (0,6) (6,0)
B load
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9) (11,8) (0,8) (8,0) (8,9) (11,6) (0,6) (6,0) (6,9)
B to A
(0,0) (11,0) (11,9) (0,9) (9,0) (9,9) (11,7) (0,7) (7,0) (7,9) (11,5) (0,5) (5,0) (5,9) (11,3) (0,3) (3,0) (3,9) (11,1) (0,1) (1,0) (1,9) (10,0) (10,9) (11,8) (0,8) (8,0) (8,9) (11,6) (0,6) (6,0) (6,9) (11,4)
finished |
|