Description
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 pot j is full (and there may be some water left in the pot i), or the pot i 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 exactly C liters of water in one of the pots.Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.Sample Input
3 5 4Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
BFS,然后模拟一个树来存走过的路径,压入栈。
//AC: 16MS 740KB
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
char S[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct Node{
int step;
int a,b;
};
struct {
int num;
int parent;
}path[1000000]; //模拟树存路径
bool vis[105][105];
void BFS(int A,int B,int C){
Node now,next;
memset(vis,false,sizeof(vis));
now.step=now.a=now.b=0;
int j=0,k=-1;
vis[0][0]=true;
queue<Node>Q;
stack<int>N;
Q.push(now);
while(!Q.empty()){
now=Q.front();
k++;
Q.pop();
if(now.a==C||now.b==C){
N.push(path[k].num);
while(path[k].parent){
k=path[k].parent;
N.push(path[k].num);
}
printf("%d\n",now.step);
for(int i=1;i<=now.step;i++){
k=N.top();
N.pop();
printf("%s\n",S[k]);
}
return;
}
for(int i=0;i<6;i++){
if(i==0){
next.a=A;
next.b=now.b;
if(!vis[next.a][next.b]){
next.step=now.step+1;
Q.push(next);
vis[next.a][next.b]=true;
j++;
path[j].num=i;
path[j].parent=k;
}
}
if(i==1){
next.a=now.a;
next.b=B;
if(!vis[next.a][next.b]){
next.step=now.step+1;
Q.push(next);
vis[next.a][next.b]=true;
j++;
path[j].num=i;
path[j].parent=k;
}
}
if(i==2){
next.a=0;
next.b=now.b;
if(!vis[next.a][next.b]){
next.step=now.step+1;
Q.push(next);
vis[next.a][next.b]=true;
j++;
path[j].num=i;
path[j].parent=k;
}
}
if(i==3){
next.a=now.a;
next.b=0;
if(!vis[next.a][next.b]){
next.step=now.step+1;
Q.push(next);
vis[next.a][next.b]=true;
j++;
path[j].num=i;
path[j].parent=k;
}
}
if(i==4){
next.a=now.a-(B-now.b);
if(next.a<0)
next.a=0;
next.b=now.b+now.a;
if(next.b>B)
next.b=B;
if(!vis[next.a][next.b]){
next.step=now.step+1;
Q.push(next);
vis[next.a][next.b]=true;
j++;
path[j].num=i;
path[j].parent=k;
}
}
if(i==5){
next.b=now.b-(A-now.a);
if(next.b<0)
next.b=0;
next.a=now.a+now.b;
if(next.a>A)
next.a=A;
if(!vis[next.a][next.b]){
next.step=now.step+1;
Q.push(next);
vis[next.a][next.b]=true;
j++;
path[j].num=i;
path[j].parent=k;
}
}
}
}
printf("impossible\n");
}
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
BFS(a,b,c);
return 0;
}