Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 17456 | Accepted: 7407 | Special Judge |
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)
Source
1 #include "cstdio" 2 #include "stdlib.h" 3 #include "iostream" 4 #include "algorithm" 5 #include "string" 6 #include "cstring" 7 #include "queue" 8 #include "cmath" 9 #include "vector" 10 #include "map" 11 #include "set" 12 #define mj 13 #define db double 14 #define ll long long 15 using namespace std; 16 const int N=1e2+5; 17 int cas=1; 18 int a,b,c; 19 int ok=0; 20 int v[N][N]; 21 char s[10][10]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"}; 22 struct P 23 { 24 int a,b,t; 25 char s[1000];//记录操作 26 }; 27 28 void bfs() 29 { 30 queue<P>q; 31 P p,u; 32 p.a=0,p.b=0,p.t=0,p.s[0]='0'; 33 v[p.a][p.b]=1; 34 q.push(p); 35 while(!q.empty()){ 36 u=q.front(); 37 q.pop(); 38 if(u.a==c || u.b==c){ 39 ok=1; 40 printf("%d ",u.t); 41 for(int i=0;i<u.t;i++){ 42 printf("%s ",s[u.s[i]-'0']);//输出操作 43 } 44 return ; 45 } 46 if(u.a<a){ 47 p.a=a; 48 p.b=u.b; 49 p.t=u.t+1; 50 strcpy(p.s,u.s);//复制之前的操作 51 p.s[u.t]=1+'0'; 52 if(!v[p.a][p.b]){ 53 v[p.a][p.b]=1; 54 q.push(p); 55 } 56 } 57 if(u.b<b){ 58 p.b=b; 59 p.a=u.a; 60 p.t=u.t+1; 61 strcpy(p.s,u.s); 62 p.s[u.t]=2+'0'; 63 if(!v[p.a][p.b]){ 64 v[p.a][p.b]=1; 65 q.push(p); 66 } 67 } 68 if(u.a!=0){ 69 p.a=0; 70 p.b=u.b; 71 p.t=u.t+1; 72 strcpy(p.s,u.s); 73 p.s[u.t]=3+'0'; 74 if(!v[p.a][p.b]){ 75 v[p.a][p.b]=1; 76 q.push(p); 77 } 78 } 79 if(u.b!=0){ 80 p.b=0; 81 p.a=u.a; 82 p.t=u.t+1; 83 strcpy(p.s,u.s); 84 p.s[u.t]=4+'0'; 85 if(!v[p.a][p.b]){ 86 v[p.a][p.b]=1; 87 q.push(p); 88 } 89 } 90 if(u.a!=0 && u.b<b){ 91 if(b-u.b>=u.a){ 92 p.b=u.a+u.b; 93 p.a=0; 94 } 95 else{ 96 p.a=u.a+u.b-b; 97 p.b=b; 98 } 99 p.t=u.t+1; 100 strcpy(p.s,u.s); 101 p.s[u.t]=5+'0'; 102 if(!v[p.a][p.b]){ 103 v[p.a][p.b]=1; 104 q.push(p); 105 } 106 } 107 if(u.b!=0 && u.a<a){ 108 if(a-u.a>=u.b){ 109 p.a=u.a+u.b; 110 p.b=0; 111 } 112 else{ 113 p.b=u.a+u.b-a; 114 p.a=a; 115 } 116 p.t=u.t+1; 117 strcpy(p.s,u.s); 118 p.s[u.t]=6+'0'; 119 if(!v[p.a][p.b]){ 120 v[p.a][p.b]=1; 121 q.push(p); 122 } 123 } 124 } 125 } 126 int main() 127 { 128 scanf("%d %d %d",&a,&b,&c); 129 memset(v,0, sizeof(v)); 130 bfs(); 131 if(!ok) printf("impossible "); 132 return 0; 133 }