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
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
给你两个壶,容量a,b,让你三种操作 1.接满 2.一个壶倒到另一个壶(这个壶倒空了或者那个壶满了 才停止) 3.把一个壶倒空 问你几步两个壶中能有一个壶容量是能c。
这个题显然bfs,就是node结构体里面在加上一个queue和string保存下之前的状态。没啥了,代码好长啊QAQ。
代码如下:
1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 #include <cstring> 5 #include <cmath> 6 #include <cstdio> 7 #include <queue> 8 #include <vector> 9 using namespace std; 10 struct node 11 { 12 int na,nb,step; 13 vector<int> num; 14 string ns; 15 }; 16 bool vis [110][110]; 17 int a,b,c; 18 bool iffind; 19 void init () 20 { 21 memset(vis,false,sizeof vis); 22 iffind=false; 23 } 24 void bfs(node now) 25 { 26 queue<node>q; 27 q.push(now); 28 while (!q.empty()) 29 { 30 node temp=q.front(); 31 q.pop(); 32 if (vis[temp.na][temp.nb]) 33 continue ; 34 vis[temp.na][temp.nb]=true; 35 if (temp.na==c||temp.nb==c)//success 36 { 37 iffind=true; 38 printf("%d ",temp.step); 39 //printf("=== %d %d ",sizeof(temp.ns),temp.num.size()); 40 for (int i=0;i<temp.num.size();++i) 41 { 42 if (temp.ns[i]=='F') 43 { 44 printf("FILL"); 45 if (temp.num[i]==1) 46 printf("(1) "); 47 else 48 printf("(2) "); 49 } 50 else if (temp.ns[i]=='P') 51 { 52 printf("POUR"); 53 if (temp.num[i]==1) 54 printf("(1,2) "); 55 else 56 printf("(2,1) "); 57 } 58 else 59 { 60 printf("DROP"); 61 if (temp.num[i]==1) 62 printf("(1) "); 63 else 64 printf("(2) "); 65 } 66 } 67 return ; 68 } 69 if (temp.na!=a)//fill a 70 { 71 node now=temp; 72 now.step++; 73 now.na=a; 74 now.ns+="F"; 75 now.num.push_back(1); 76 q.push(now); 77 } 78 if (temp.nb!=b)//fill b 79 { 80 node now=temp; 81 now.step++; 82 now.nb=b; 83 now.ns+="F"; 84 now.num.push_back(2); 85 q.push(now); 86 } 87 if (temp.na>0&&temp.nb<b)//pour a to b 88 { 89 node now=temp; 90 now.step++; 91 now.na-=min(temp.na,b-temp.nb); 92 now.nb+=min(temp.na,b-temp.nb); 93 now.ns+="P"; 94 now.num.push_back(1); 95 q.push(now); 96 } 97 if (temp.nb>0&&temp.na<a)//pour b to a 98 { 99 node now=temp; 100 now.step++; 101 now.nb-=min(temp.nb,a-temp.na); 102 now.na+=min(temp.nb,a-temp.na); 103 now.ns+="P"; 104 now.num.push_back(2); 105 q.push(now); 106 } 107 if(temp.na!=0)// drop a 108 { 109 node now=temp; 110 now.step++; 111 now.na=0; 112 now.ns+='D'; 113 now.num.push_back(1); 114 q.push(now); 115 } 116 if(temp.nb!=0)//drop b 117 { 118 node now=temp; 119 now.step++; 120 now.nb=0; 121 now.ns+='D'; 122 now.num.push_back(2); 123 q.push(now); 124 } 125 } 126 return ; 127 } 128 int main() 129 { 130 while (~scanf("%d%d%d",&a,&b,&c)) 131 { 132 init(); 133 node x; 134 x.na=0,x.nb=0,x.ns="",x.step=0,x.num.clear(); 135 bfs(x); 136 if (iffind==false){ 137 printf("impossible "); 138 } 139 } 140 return 0; 141 }