poj 3414 -- Pots
Pots
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 9845 | Accepted: 4146 | 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)
题意:倒水游戏,给两个杯子,最初都是空的,有如下操作:
1.将a杯接满
2.将b杯接满
3.将a杯倒掉
4.将b杯倒掉
5.将a杯水倒到b杯,且不能溢出
6.将b杯水倒到a杯,且不能溢出
题目给出3个数,分别表示ab杯子的容量以及期望的结果。问最少需要多少次。如果不能达到,则输出impossible
思路:BFS6种操作。3000多b伤不起。。。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : Pots.cpp 4 * Creat time : 2014-08-04 15:48 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 205 15 using namespace std; 16 int a,b,c; 17 struct Node 18 { 19 int a,b; 20 int op; 21 }node[M][M]; 22 struct Pots{ 23 int a,b; 24 int op,s; 25 }; 26 bool vis[M][M]; 27 const char str[][15] = {{"FILL(1)"},{"FILL(2)"},{"DROP(1)"},{"DROP(2)"},{"POUR(1,2)"},{"POUR(2,1)"}}; 28 Pots pot,temp,p; 29 int BFS(int &steps,int &how) 30 { 31 clr(vis,0); 32 queue<Pots>que; 33 que.push(pot); 34 while(!que.empty()){ 35 p = temp = que.front(); 36 que.pop(); 37 if(temp.a == c && temp.b == c){ 38 steps = temp.s; how = c; 39 return 1; 40 } 41 else if(temp.a == c && temp.b != c){ 42 steps = temp.s; how = temp.b; 43 return 2; 44 } 45 else if(temp.a != c && temp.b ==c){ 46 steps = temp.s; how = temp.a; 47 return 3; 48 } 49 /*---------------操作0,fill(1)---------------*/ 50 p.a = a; p.op = 0; p.s = temp.s + 1; 51 if(!vis[p.a][p.b]){ 52 vis[p.a][p.b] = 1; 53 que.push(p); 54 node[p.a][p.b].op = 0; 55 node[p.a][p.b].a = temp.a; node[p.a][p.b].b = temp.b; 56 } 57 /*---------------操作1,fill(2)---------------*/ 58 p.a = temp.a; p.b = b; p.op = 1; p.s = temp.s + 1; 59 if(!vis[p.a][p.b]){ 60 vis[p.a][p.b] = 1; 61 que.push(p); 62 node[p.a][p.b].op = 1; 63 node[p.a][p.b].a = temp.a; node[p.a][p.b].b = temp.b; 64 } 65 /*---------------操作2,drop(1)---------------*/ 66 p.a = 0; p.b = temp.b; p.op = 2; p.s = temp.s + 1; 67 if(!vis[p.a][p.b]){ 68 vis[p.a][p.b] = 1; 69 que.push(p); 70 node[p.a][p.b].op = 2; 71 node[p.a][p.b].a = temp.a; node[p.a][p.b].b = temp.b; 72 } 73 /*---------------操作3,drop(2)---------------*/ 74 p.a = temp.a; p.b = 0; p.op = 3; p.s = temp.s + 1; 75 if(!vis[p.a][p.b]){ 76 vis[p.a][p.b] = 1; 77 que.push(p); 78 node[p.a][p.b].op = 3; 79 node[p.a][p.b].a = temp.a; node[p.a][p.b].b = temp.b; 80 } 81 /*---------------操作4,pour(1,2)-------------*/ 82 int tt = temp.a - (b - temp.b); 83 if(tt < 0){ 84 p.a = 0; p.b = temp.b + temp.a; 85 } 86 else{ 87 p.a = tt; p.b = temp.b + (temp.a - tt); 88 } 89 p.op = 4; p.s = temp.s + 1; 90 if(!vis[p.a][p.b]){ 91 vis[p.a][p.b] = 1; 92 que.push(p); 93 node[p.a][p.b].op = 4; 94 node[p.a][p.b].a = temp.a; node[p.a][p.b].b = temp.b; 95 } 96 /*---------------操作5,pour(2,1)-------------*/ 97 tt = temp.b - (a - temp.a); 98 if(tt < 0){ 99 p.b = 0; p.a = temp.a + temp.b; 100 } 101 else{ 102 p.b = tt; p.a = temp.a + (temp.b - tt); 103 } 104 p.op = 5; p.s = temp.s + 1; 105 if(!vis[p.a][p.b]){ 106 vis[p.a][p.b] = 1; 107 que.push(p); 108 node[p.a][p.b].op = 5; 109 node[p.a][p.b].a = temp.a; node[p.a][p.b].b = temp.b; 110 } 111 } 112 return -1; 113 } 114 void Print(int x,int y){ 115 if(x == 0 && y == 0){ 116 return ; 117 } 118 Print(node[x][y].a,node[x][y].b); 119 printf("%s\n",str[node[x][y].op]); 120 } 121 int main(int argc,char *argv[]) 122 { 123 while(scanf("%d%d%d",&a,&b,&c)!=EOF){ 124 int steps = 0,how = 0; 125 pot.a = 0; pot.b = 0; pot.op = -1; pot.s = 0; 126 int ans = BFS(steps,how); 127 if(ans == -1){ 128 printf("impossible\n"); 129 } 130 else 131 printf("%d\n",steps); 132 if(ans == 1){ 133 Print(c,c); 134 } 135 if(ans == 2){ 136 Print(c,how); 137 } 138 if(ans == 3){ 139 Print(how,c); 140 } 141 } 142 return 0; 143 }
Do one thing , and do it well !