Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 100 Accepted Submission(s) : 30
Special Judge
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- POUR(i,j) pour from pot i to pot j; after this operation either the potj is full (and there may be some water left in the pot i), or the poti is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactlyC liters of water in one of the pots.
3 5 4
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)#include<iostream> #include<string.h> #include<queue> using namespace std;
int a,b,c; int logal[1000][1000]={0}; struct dalao { int mla; int mlb; int step; string lalala[2000]; }; dalao t1,t2;
int bfs() { memset(logal,0,sizeof(logal));
queue<dalao>k;
t1.mla=0; t1.mlb=0; t1.step=0; //t1.lalala[0]=""; k.push(t1); logal[0][0]=1; //int y=0; while(k.empty()==0) { // cout<<y++<<endl;
//dalao j; t1=k.front(); //cout<<t1.mla<<" "<<t1.mlb<<" "<<" step"<<t1.step<<endl;
k.pop(); if(t1.mla==c||t1.mlb==c) { cout<<t1.step<<endl; for(int i=1;i<=t1.step;i++) { cout<<t1.lalala[i]<<endl; } return 1; } if(t1.mla==0) { t2=t1; t2.mla=a; t2.step++; t2.lalala[t2.step]="FILL(1)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); } } if(t1.mla>0&&t1.mla<=a) { t2=t1; t2.mla=0; t2.step++; t2.lalala[t2.step]="DROP(1)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); } }
if(t1.mlb<b&&t1.mla!=0) { t2=t1; t2.step++; if(t2.mla+t2.mlb<=b) { t2.mlb+=t2.mla; t2.mla=0; } else { t2.mla=t2.mla+t2.mlb-b; t2.mlb=b; } t2.lalala[t2.step]="POUR(1,2)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); } }
if(t1.mlb==0) { //cout<<"****"<<endl; t2=t1; t2.mlb=b; t2.step++; t2.lalala[t2.step]="FILL(2)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); }
} if(t1.mlb>0&&t1.mlb<=b)// { t2=t1; t2.mlb=0; t2.step++; t2.lalala[t2.step]="DROP(2)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); }
}
if(t1.mla<a&&t1.mlb!=0)//这里要特别注意,不要写错顺序 {
t2=t1; t2.step++; if(t2.mla+t2.mlb<=a) { t2.mla+=t2.mlb; t2.mlb=0; } else {
t2.mlb=t2.mla+t2.mlb-a; t2.mla=a;
}
t2.lalala[t2.step]="POUR(2,1)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); } }
} return 0; } int main() { //int a,b,c; while(cin>>a>>b>>c) { //memset(logal,0,sizeof(logal)); int ans=bfs(); if(ans==0) cout<<"impossible"<<endl;
} return 0;
}