Solution for Water Jug Problem with 2 Jugs > Java Program
Solution for Water Jug Problem with 2 Jugs > Java Program
Artificial Intelligence
Program:
public class Jug {
static int flag=0;
public static void main(String[] args) {
state(0,0);
System.out.println("EXIT");
}
static void state(int x,int y){
System.out.println("X : "+x+" Y: "+y);
if(flag==1){
System.out.println("X : 2 Y: 0");
return;
}
if(y<3){
state(x,++y);
}else if(y==3){
if(x==3){
System.out.println("X : 4 Y: 2");
x=0;
y--;
flag=1;
}else{
x=3;
y=0;
}
state(x,y);
}
}
}
/*run:
X : 0 Y: 0
X : 0 Y: 1
X : 0 Y: 2
X : 0 Y: 3
X : 3 Y: 0
X : 3 Y: 1
X : 3 Y: 2
X : 3 Y: 3
X : 4 Y: 2
X : 0 Y: 2
X : 2 Y: 0
EXIT
BUILD SUCCESSFUL (total time: 0 seconds)
*/
Comments
Post a Comment