Pots
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9963 |
|
Accepted: 4179 |
|
Special Judge |
Description
You are given the pots of 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 was full (and there may be some water left in the pot I), or the PO T I was empty (and all its contents has been moved to the pot J).
Write a program to find the shortest possible sequence of these operations that'll yield exactly C liters of Water in one of the pots.
Input
On the first and only line is the numbers A, B, and C. These is all integers in the range from 1 to + 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 is several sequences of minimal length, output any one of them. If the desired result can ' t is achieved, the first and only line of the file must contain the word 'impossible'.
Sample Input
3 5 4
Sample Output
6FILL (2) pour (2,1) DROP (1) pour (2,1) FILL (2) pour (2,1)
Like the two-dimensional bfs that we've done before. Just this need to backtrack path. Very easy add a variable to the struct to record the previous state's subscript in the queue (the hand-struck queue is better.) This time in the use of STL queue as if not generous, and finally find the status of the condition of the reverse print path (because the record is the previous state)
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include <queue >using namespace Std;int m,n,c,s,e,p;typedef struct node{int v1,v2,cur,pre,op;}; BOOL Vis[999][999];int Ans[10010];node que[10010];void BFS () {p=0;s=0;e=0;int Pos;node t={0,0,0,0,0};que[e++]=t;vis[0 ][0]=1;while (s<e) {node f=que[s];p os=s;s++;if (f.v1==c| | F.v2==c) {printf ("%d\n", f.op); int tem=pos;for (int i=0;i<f.op;i++) {ans[p++]=que[tem].cur; Tem=que[tem].pre;} for (int. i=p-1;i>=0;i--) {switch (Ans[i]) {Case 1:printf ("fill (1) \ n"), Break;case 2:printf ("Fill (2) \ n"); Break;case 3 :p rintf ("drop (1) \ n"), Break;case 4:printf ("drop (2) \ n"), break;case 5:printf ("pour (2,1) \ n"); Break;case 6:printf (" (pour) \ n "); break;}} return;} if (f.v1!=m) {t.v1=m;t.op=f.op+1;t.v2=f.v2;if (!vis[t.v1][t.v2]) {vis[t.v1][t.v2]=1; t.cur=1; t.pre=pos; que[e++]=t;}} if (f.v2!=n) {t.v2=n;t.op=f.op+1;t.v1=f.v1;if (!vis[t.v1][t.v2]) {vis[t.v1][t.v2]=1; t.cur=2;t.pre=pos; que[e++]=t;}} if (f.v1!=0) {t.v1=0;t.v2=f.v2;t.op=f.op+1;if (!vis[t.v1][t.v2]) {vis[t.v1][t.v2]=1; t.cur=3; t.pre=pos; que[e++]=t;}} if (f.v2!=0) {t.v2=0;t.v1=f.v1;t.op=f.op+1;if (!vis[t.v1][t.v2]) {vis[t.v1][t.v2]=1; t.cur=4; t.pre=pos; que[e++]=t;}} if (f.v2!=0&&f.v1!=m) {t.v2=f.v2-(M-F.V1); if (t.v2<0) t.v2=0;t.v1=f.v1+f.v2; if (t.v1>m) t.v1=m;t.op=f.op+1;if (!vis[t.v1][t.v2]) {vis[t.v1][t.v2]=1; t.cur=5; t.pre=pos; que[e++]=t;}} if (f.v1!=0&&f.v2!=n) {t.v1=f.v1-(N-F.V2); if (t.v1<0) t.v1=0;t.v2=f.v2+f.v1; if (t.v2>n) t.v2=n;t.op=f.op+1;if (!vis[t.v1][t.v2]) {vis[t.v1][t.v2]=1; t.cur=6; t.pre=pos; que[e++]=t;}}} Puts ("impossible");} int main () {while (cin>>m>>n>>c) {memset (vis,0,sizeof (Vis)); BFs ();} return 0;}
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
POJ 3414--pots (bfs+ backtracking path)