Pots
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total submissions: 19042 |
|
accepted: 8027 |
|
Special Judge |
Description You are 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 are full (and there may are some water left in the pot I), or the PO T I is empty (and all it contents have been moved to the pot J). Write a program to find the shortest possible sequence of this operations that would yield exactly C liters of Water in one of the pots. Input On the "the" and "only" line are the numbers A, B, and C. These are all integers in the range from 1 to and C≤max (A,B). Output The "a" 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 the them. If the desired result can ' t be achieved, the ' the ' of the ' 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 |
Meaning
There are 4 of operations, pour down to find the amount of C.
Point:
DFS BFS are available because of the small amount of data. It's easy to write Dfs. The DFS was written and later found to be not so. Because you want to output the path.
I copied the DFS, ran the output path at once, so the code is very long.
#include <iostream> #include <stdio.h> #include <math.h> #include <stack> #include <string.h
> Using namespace std;
int mp[111][111];
int a,b,c;
int ans=9999999;
void Dfs (int x,int y,int now) {if (x==0&&y==0&&now!=0) return;
if (Now>=ans) return;
if (mp[x][y]<=now&&mp[x][y]!=0) return;
Mp[x][y]=now; if (x==c| |
Y==c) {ans=min (Now,ans);
Return
} if (x>0) {DFS (0,y,now+1);
} if (y>0) {DFS (x,0,now+1); } if (X<a) {DFS (a,y,now+1);//fill a if (y!=0&&a-x>y) {DFS (x+y,0,
NOW+1);
} if (y!=0&&a-x<=y) {DFS (a,y-(a-x), now+1); } if (y<b) {DFS (x,b,now+1);//fill b if (x!=0&&b-y>x) {DFS (
0,X+Y,NOW+1); } if (x!=0&&b-y<=x) {DFS (x (b-y), b,now+1);
{}} int ans1;
int flag=0;
Stack<int> Q;
void dfs1 (int x,int y,int now) {if (x==0&&y==0&&now!=0) return;
if (now>ans1) return;
if (mp[x][y]<=now&&mp[x][y]!=0) return;
Mp[x][y]=now; if (x==c| |
Y==c) {ans=min (Now,ans);
if (Ans1==ans) {flag=1;
Return
} return;
} if (x>0) {DFS1 (0,y,now+1);
if (flag==1) {Q.push (1);
printf ("DROP (%d) \ n", 1);
Return
} if (y>0) {DFS1 (x,0,now+1);
if (flag==1) {Q.push (2);
printf ("DROP (%d) \ n", 2);
Return
} if (X<a) {DFS1 (a,y,now+1);//fill a if (flag==1) {Q.push (3);
printf ("FILL (%d) \ n", 1);
Return
} if (y!=0&&a-x>y) {DFS1 (x+y,0,now+1); if (flag==1) {Q.push (4);
printf ("pour (%d,%d) \ n", 2, 1);
Return
} if (y!=0&&a-x<=y) {DFS1 (a,y-(a-x), now+1);
if (flag==1) {Q.push (5);
printf ("pour (%d,%d) \ n", 2, 1);
Return }} if (Y<b) {DFS1 (x,b,now+1);//fill b if (flag==1) {Q.push (6
);
printf ("FILL (%d) \ n", 2);
Return
} if (x!=0&&b-y>x) {DFS1 (0,x+y,now+1);
if (flag==1) {Q.push (7);
printf ("pour (%d,%d) \ n", 1,2);
Return
} if (x!=0&&b-y<=x) {DFS1 (x (b-y), b,now+1);
if (flag==1) {Q.push (8); printf ("POUR (%d,%d) \ n ", 1,2);
Return
int main () {while (~SCANF ("%d%d%d", &a,&b,&c)) {ans=9999999;
memset (mp,0,sizeof MP);
DFS (0,0,0);
Ans1=ans;
if (ans1==9999999) {printf ("impossible\n");
Continue
} flag=0;
memset (mp,0,sizeof MP);
DFS1 (0,0,0);
printf ("%d\n", ans);
while (!q.empty ()) {int a=q.top ();
Q.pop ();
if (a==1) {printf ("DROP (%d) \ n", 1);
else if (a==2) {printf ("DROP (%d) \ n", 2);
else if (a==3) {printf ("FILL (%d) \ n", 1); else if (a==4| |
a==5) {printf ("pour (%d,%d) \ n", 2, 1);
else if (a==6) {printf ("FILL (%d) \ n", 2);
} else if (a==7| |
a==8) {printf ("pour (%d,%d) \ n", 1,2);
}} return 0; }