Pots
Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
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 4 Sample Output 6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1) Source Northeastern Europe 2002, Western Subregion
|
#include <iostream> #include <cstring> #include <queue> using namespace std; const int SIZE=100; typedef struct { int a; int b; int step; }Node; struct pn { int prex; int prey; }prev[SIZE+1][SIZE+1]; bool visit[SIZE+1][SIZE+1]; int a,b,c; void DFS(int x, int y) { if(prev[x][y].prex+prev[x][y].prey!=0) DFS(prev[x][y].prex,prev[x][y].prey); int px=prev[x][y].prex; int py=prev[x][y].prey; //FILL(1) if(px!=a&&x==a&&y==py) { cout<<"FILL(1)"<<endl; return; } //FILL(2) if(py!=b&&y==b&&x==px) { cout<<"FILL(2)"<<endl; return; } //DROP(1) if(px!=0 && x==0 && y==py) { cout<<"DROP(1)"<<endl; return ; } //DROP(2) if(py!=0 && y==0 && x==px) { cout<<"DROP(2)"<<endl; return; } //从A倒入B或B倒入A if(px+py==x+y) { //A->B if(x==0||y==b) { cout<<"POUR(1,2)"<<endl; } else cout<<"POUR(2,1)"<<endl; return; } } void BFS() { Node temp, temp1; queue<Node> nq; bool isSolve; isSolve=false; visit[0][0]=true; temp.a=0; temp.b=0; temp.step=0; nq.push(temp); while(!nq.empty()) { temp=nq.front(); nq.pop(); if(temp.a==c || temp.b==c) { cout<<temp.step<<endl; DFS(temp.a,temp.b); isSolve=true; break; } temp1.step=temp.step+1; //OP 0 FILL(1) temp1.a=a; temp1.b=temp.b; if(!visit[temp1.a][temp1.b]) { visit[temp1.a][temp1.b]=true; prev[temp1.a][temp1.b].prex=temp.a; prev[temp1.a][temp1.b].prey=temp.b; nq.push(temp1); } //OP 1 FILL(2) temp1.a=temp.a; temp1.b=b; if(!visit[temp1.a][temp1.b]) { visit[temp1.a][temp1.b]=true; prev[temp1.a][temp1.b].prex=temp.a; prev[temp1.a][temp1.b].prey=temp.b; nq.push(temp1); } //OP 2 DROP(1) temp1.a=0; temp1.b=temp.b; if(!visit[temp1.a][temp1.b]) { visit[temp1.a][temp1.b]=true; prev[temp1.a][temp1.b].prex=temp.a; prev[temp1.a][temp1.b].prey=temp.b; nq.push(temp1); } //OP 3 DROP(2) temp1.a=temp.a; temp1.b=0; if(!visit[temp1.a][temp1.b]) { visit[temp1.a][temp1.b]=true; prev[temp1.a][temp1.b].prex=temp.a; prev[temp1.a][temp1.b].prey=temp.b; nq.push(temp1); } //OP 4 POUR(1,2) temp1.b=(temp.a+temp.b)<b?(temp.a+temp.b):b; temp1.a=temp.a-(temp1.b-temp.b); if(!visit[temp1.a][temp1.b]) { visit[temp1.a][temp1.b]=true; prev[temp1.a][temp1.b].prex=temp.a; prev[temp1.a][temp1.b].prey=temp.b; nq.push(temp1); } //OP 5 POUR(2,1) temp1.a=(temp.a+temp.b)<a?(temp.a+temp.b):a; temp1.b=temp.b-(temp1.a-temp.a); if(!visit[temp1.a][temp1.b]) { visit[temp1.a][temp1.b]=true; prev[temp1.a][temp1.b].prex=temp.a; prev[temp1.a][temp1.b].prey=temp.b; nq.push(temp1); } } if(!isSolve) cout<<"impossible"<<endl; } int main() { while(cin>>a>>b>>c) { memset(visit,0,sizeof(visit)); memset(prev,0,sizeof(prev)); BFS(); } return 0; }