首页 > 代码库 > POJ 3414 Pots (经典bfs )
POJ 3414 Pots (经典bfs )
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 4
Sample Output
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
Source
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define N 105 struct stud{ int a,b; int time; string mv; }; int a,b,c; int vis[N][N]; int judge(int x,int y) { if(x==c||y==c) return 1; return 0; } stud bfs() { struct stud next,cur; queue<stud>q; while(!q.empty()) q.pop(); cur.a=cur.b=0; cur.mv=""; cur.time=0; if(cur.a==c||cur.b==c) return cur; q.push(cur); memset(vis,0,sizeof(vis)); vis[0][0]=1; while(!q.empty()) { cur=q.front(); q.pop(); if(cur.a<a&&!vis[a][cur.b]) //装满a { next=cur; next.a=a; next.time++; next.mv+="F1"; vis[next.a][next.b]; if(judge(next.a,next.b)) return next; q.push(next); } if(cur.b<b&&!vis[cur.a][b]) //装满b { next=cur; next.b=b; next.time++; next.mv+="F2"; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } if(cur.a>0&&!vis[0][cur.b]) //倒出a { next.a=0; next.b=cur.b; next.mv=cur.mv; next.mv+="D1"; next.time=cur.time+1; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } if(cur.b>0&&!vis[cur.a][0]) //倒出b { next.b=0; next.a=cur.a; next.mv=cur.mv; next.mv+="D2"; next.time=cur.time+1; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } //a给b倒水 if(cur.b<b&&cur.a>0&&cur.a+cur.b<=b&&!vis[0][cur.a+cur.b]) { next.a=0; next.b=cur.a+cur.b; next.time=cur.time+1; next.mv=cur.mv+"P12"; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } else if(cur.b<b&&cur.a>0&&cur.a+cur.b>b&&!vis[cur.a+cur.b-b][b]) { next.a=cur.a+cur.b-b; next.b=b; next.time=cur.time+1; next.mv=cur.mv+"P12"; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } //b给a倒水 if(cur.b>0&&cur.a!=a&&cur.a+cur.b<=a&&!vis[cur.a+cur.b][0]) { next.a=cur.a+cur.b; next.b=0; next.time=cur.time+1; next.mv=cur.mv+"P21"; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } else if(cur.b>0&&cur.a!=a&&cur.a+cur.b>a&&!vis[a][cur.a+cur.b-a]) { next.a=a; next.b=cur.a+cur.b-a; next.time=cur.time+1; next.mv=cur.mv+"P21"; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } } struct stud ans; ans.a=-1; return ans; } int main() { int i; scanf("%d%d%d",&a,&b,&c); struct stud cur; cur=bfs(); if(cur.a==-1) { printf("impossible\n"); return 0; } printf("%d\n",cur.time); int len=cur.mv.size(); for(i=0;i<len;i++) { if(cur.mv[i]=='D') { printf("DROP("); i++; printf("%c)\n",cur.mv.at(i)); } else if(cur.mv[i]=='F') { printf("FILL("); i++; printf("%c)\n",cur.mv.at(i)); } else { printf("POUR("); i++; printf("%c",cur.mv.at(i)); i++; printf(",%c)\n",cur.mv.at(i)); } } return 0; }