Pots
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:10042 |
|
Accepted:4221 |
|
Special Judge |
Description
You are given two pots, having the volumeAAndBLiters respectively. The following operations can be saved med:
- Fill (I) fill the potI(1 ≤I≤ 2) from the tap;
- Drop (I) Empty the potITo the drain;
- Pour (I, j) pour from potITo potJ; After this operation either the potJIs full (and there may be some water left in the potI), Or the potIIs empty (and all its contents have been moved to the potJ).
Write a program to find the shortest possible sequence of these operations that will yield exactlyCLiters of water in one of the pots.
Input
On the first and only line are the numbersA,B, AndC. These are all integers in the range from 1 to 100 andC≤ Max (A,B).
Output
The first line of the output must contain the length of the sequence of operationsK. The followingKLines 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)
Application Time: 10 min
Actual Time: 57 min
Cause: all six operations are irreversible.
Idea: there is plenty of time, so we don't need to build a reverse edge, and the data is too small.
#include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn=101;int n,m;typedef unsigned long long ull;int A,B,C;int vis[maxn][maxn];int ans[maxn][maxn][maxn*maxn];struct node{ int a,b; node (int ta,int tb):a(ta),b(tb){}};void printop(int op){ switch(op){ case 0: puts("FILL(1)"); break; case 1: puts("FILL(2)"); break; case 2: puts("POUR(1,2)"); break; case 3: puts("POUR(2,1)"); break; case 4: puts("DROP(1)"); break; case 5: puts("DROP(2)"); break; }}void op(int &a,int &b,int op){ switch(op){ case 0: a=A; break; case 1: b=B; break; case 2: if(b+a<=B){ b+=a; a=0; } else { a-=B-b; b=B; } break; case 3: if(b+a<=A){ a+=b; b=0; } else { b-=A-a; a=A; } break; case 4: a=0; break; case 5: b=0; break; }}void bfs(){ queue <node> que; que.push(node(0,0)); vis[0][0]=0; while(!que.empty()){ node tp=que.front();que.pop(); int ta=tp.a; int tb=tp.b; if(tp.a==C||tp.b==C){ printf("%d\n",vis[tp.a][tp.b]); for(int i=0;i<vis[ta][tb];i++){ int op=ans[ta][tb][i]; printop(op); } return ; } for(int i=0;i<6;i++){ int ta=tp.a; int tb=tp.b; op(ta,tb,i); if(vis[ta][tb]==-1){ vis[ta][tb]=vis[tp.a][tp.b]+1; for(int j=0;j<vis[tp.a][tp.b];j++){ ans[ta][tb][j]=ans[tp.a][tp.b][j]; } ans[ta][tb][vis[tp.a][tp.b]]=i; que.push(node(ta,tb)); } } } puts("impossible");}int main(){ scanf("%d%d%d",&A,&B,&C); memset(vis,-1,sizeof(vis)); bfs(); return 0;}
Quick cut-off poj3414 pots