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
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1) 题目分析:打眼一看就是BFS,还是普通的BFS。 代码如下:
1 # include2 # include 3 # include 4 # include 5 # include 6 # include 7 # include 8 using namespace std; 9 struct node 10 { 11 int a,b,t; 12 vector op; 13 bool operator < (const node &a) const { 14 return t>a.t; 15 } 16 node & operator = (const node &p) { 17 a=p.a,b=p.b,t=p.t; 18 op.clear(); 19 for(int i=0;i q; 28 memset(vis,0,sizeof(vis)); 29 node sta; 30 sta.a=sta.b=sta.t=0; 31 sta.op.clear(); 32 vis[0][0]=1; 33 q.push(sta); 34 while(!q.empty()) 35 { 36 node u=q.top(); 37 q.pop(); 38 if(u.a==C||u.b==C){ 39 printf("%d\n",u.t); 40 for(int i=0;i 0){ 65 node now=u; 66 now.a=0,now.b=u.b; 67 if(!vis[now.a][now.b]){ 68 vis[now.a][now.b]; 69 now.t=u.t+1; 70 now.op.push_back("DROP(1)"); 71 q.push(now); 72 } 73 } 74 if(u.b>0){ 75 node now=u; 76 now.a=u.a,now.b=0; 77 if(!vis[now.a][now.b]){ 78 vis[now.a][now.b]; 79 now.t=u.t+1; 80 now.op.push_back("DROP(2)"); 81 q.push(now); 82 } 83 } 84 if(u.a 0){ 85 node now=u; 86 now.a=min(A,u.a+u.b); 87 now.b=max(0,u.b-A+u.a); 88 if(!vis[now.a][now.b]){ 89 vis[now.a][now.b]=vis[now.b][now.a]=1; 90 now.t=u.t+1; 91 now.op.push_back("POUR(2,1)"); 92 q.push(now); 93 } 94 } 95 if(u.a>0&&u.b