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)
Problem analysis: Drilling a look is BFS, or ordinary BFS.
The code is as follows:
1# include<iostream>2# include<cstdio>3# include<string>4# include<queue>5# include<vector>6# include<cstring>7# include<algorithm>8 using namespacestd;9 structnodeTen { One inta,b,t; Avector<string>op; - BOOL operator< (ConstNode &a)Const { - returnT>A.T; the } -Node &operator= (ConstNode &p) { -a=p.a,b=p.b,t=p.t; - op.clear (); + for(intI=0; I<p.op.size (); + +i) - Op.push_back (P.op[i]); + return* This; A } at }; - intvis[ the][ the]; - voidBFsintAintBintC) - { -Priority_queue<node>Q; -memset (Vis,0,sizeof(Vis)); in node STA; -sta.a=sta.b=sta.t=0; to sta.op.clear (); +vis[0][0]=1; - Q.push (STA); the while(!q.empty ()) * { $Node u=q.top ();Panax Notoginseng Q.pop (); - if(u.a==c| | u.b==b) { theprintf"%d\n", u.t); + for(intI=0; I<u.op.size (); + +i) Acout<<u.op[i]<<Endl; the return ; + } - if(u.a<A) { $Node now=u; $now.a=a,now.b=u.b; - if(!vis[now.a][now.b]) { - vis[now.a][now.b]; thenow.t=u.t+1; -Now.op.push_back ("FILL (1)");Wuyi Q.push (now); the } - } Wu if(u.b<c) { -Node now=u; Aboutnow.a=u.a,now.b=B; $ if(!vis[now.a][now.b]) { - vis[now.a][now.b]; -now.t=u.t+1; -Now.op.push_back ("FILL (2)"); A Q.push (now); + } the } - if(u.a>0){ $Node now=u; theNow.a=0, now.b=u.b; the if(!vis[now.a][now.b]) { the vis[now.a][now.b]; thenow.t=u.t+1; -Now.op.push_back ("DROP (1)"); in Q.push (now); the } the } About if(u.b>0){ theNode now=u; thenow.a=u.a,now.b=0; the if(!vis[now.a][now.b]) { + vis[now.a][now.b]; -now.t=u.t+1; theNow.op.push_back ("DROP (2)");Bayi Q.push (now); the } the } - if(u.a<a&&u.b>0){ -Node now=u; theNow.a=min (a,u.a+u.b); theNow.b=max (0, u.b-a+u.a); the if(!vis[now.a][now.b]) { thevis[now.a][now.b]=vis[now.b][now.a]=1; -now.t=u.t+1; theNow.op.push_back ("pour (2,1)"); the Q.push (now); the }94 } the if(u.a>0&&u.b<c) { theNode now=u; theNow.a=max (0, u.a-b+u.b);98Now.b=min (b,u.b+u.a); About if(!vis[now.a][now.b]) { -vis[now.a][now.b]=vis[now.b][now.a]=1;101now.t=u.t+1;102Now.op.push_back ("pour (ON)");103 Q.push (now);104 } the }106 }107printf"impossible\n");108 }109 intMain () the {111 inta,b,c; thescanf"%d%d%d",&a,&b,&C);113 BFS (a,b,c); the return 0; the}
POJ-3414 Pots (BFS)