POJ3414Pots【BFS】
大意:有两个杯子 三种操作
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)
问要达到指定的容量需要操作的最少次数
分析:
BFS扩展每种情况然后再输出即可
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 const int maxn = 1000; 8 9 struct Node { 10 int x, y; 11 int caozuo; 12 int a, b; 13 int fa, now; 14 int num; 15 }node[maxn + 10]; 16 17 queue<Node> q; 18 int vis[105][105]; 19 20 void dfs(Node n0) { 21 if(n0.caozuo == 0) { 22 return ; 23 } 24 dfs(node[n0.fa + 1]); 25 if(n0.caozuo == 1) { 26 printf("FILL(%d)\n", n0.a + n0.b); 27 } else if(n0.caozuo == 2) { 28 printf("DROP(%d)\n", n0.a + n0.b); 29 } else { 30 printf("POUR(%d,%d)\n", n0.a, n0.b); 31 } 32 } 33 34 int main() { 35 int A, B, C; 36 while(EOF != scanf("%d %d %d",&A, &B, &C) ) { 37 memset(vis, 0, sizeof(vis)); 38 int cnt = 0; 39 node[cnt++] = (Node) { 0, 0, 0, 0, 0, 0, 0, 0 }; 40 q.push(node[0]); vis[0][0] = 1; 41 bool flag = false; 42 int x = 0; 43 Node ans; 44 cnt++; 45 while(!q.empty()) { 46 if(x++ > maxn) break; 47 48 Node n0 = q.front(); q.pop(); 49 // printf("now = %d x = %d y = %d fa = %d num = %d caozuo = %d\n", n0.now, n0.x, n0.y, n0.fa, n0.num, n0.caozuo); 50 51 if(n0.x == C || n0.y == C) { 52 flag = true; 53 ans = n0; 54 break; 55 } 56 57 if(!vis[A][n0.y]) { 58 node[cnt++] = (Node) { A, n0.y, 1, 1, 0, n0.now, cnt - 1, n0.num + 1}; 59 q.push(node[cnt - 1]); 60 vis[A][n0.y] = 1; 61 } 62 63 if(!vis[n0.x][B]) { 64 node[cnt++] = (Node) { n0.x, B, 1, 1, 1, n0.now, cnt - 1,n0.num + 1 }; 65 q.push(node[cnt - 1]); 66 vis[n0.x][B] = 1; 67 } 68 69 if(!vis[0][n0.y]) { 70 node[cnt++] = (Node) { 0, n0.y, 2, 1, 0, n0.now, cnt - 1,n0.num + 1 }; 71 q.push(node[cnt - 1]); 72 vis[0][n0.y] = 1; 73 } 74 75 if(!vis[n0.x][0]) { 76 node[cnt++] = (Node) { n0.x, 0, 2, 1, 1, n0.now, cnt - 1,n0.num + 1 }; 77 q.push(node[cnt - 1]); 78 vis[n0.x][0] = 1; 79 } 80 81 if(n0.x + n0.y <= B) { 82 if(!vis[0][n0.x + n0.y]) { 83 node[cnt++] = (Node) { 0, n0.x + n0.y, 3, 1, 2, n0.now, cnt - 1, n0.num + 1}; 84 q.push(node[cnt -1]); 85 vis[0][n0.x + n0.y] = 1; 86 } 87 } else { 88 if(!vis[n0.x + n0.y - B][B]) { 89 node[cnt++] = (Node) { n0.x + n0.y - B, B, 3, 1, 2, n0.now, cnt - 1,n0.num + 1 }; 90 q.push(node[cnt - 1]); 91 vis[n0.x + n0.y - B][B] = 1; 92 } 93 } 94 95 if(n0.x + n0.y <= A) { 96 if(!vis[n0.x + n0.y][0]) { 97 node[cnt++] = (Node) { n0.x + n0.y, 0, 3, 2, 1, n0.now, cnt - 1,n0.num + 1}; 98 q.push(node[cnt -1]); 99 vis[n0.x + n0.y][0] = 1; 100 } 101 } else { 102 if(!vis[A][n0.x + n0.y - A]) { 103 node[cnt++] = (Node) { A, n0.x + n0.y - A, 3, 2, 1, n0.now, cnt - 1,n0.num + 1}; 104 q.push(node[cnt - 1]); 105 vis[A][n0.x + n0.y - A] = 1; 106 } 107 } 108 } 109 if(!flag) { 110 puts("impossible"); 111 continue; 112 } 113 printf("%d\n", ans.num); 114 Node n0 = ans; 115 dfs(n0); 116 } 117 return 0; 118 }