POJ 3414 (BFS)

Posted tcctw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3414 (BFS)相关的知识,希望对你有一定的参考价值。

 这道题还是比较简单的,就是写起来有点麻烦,刚开始做的时候一直一位有什么简单方法,拖了好久。。。还是自己分析问题的能力不行啊

  1 #include<iostream>
  2 #include<queue>
  3 #include<vector>
  4 #include<cstring>
  5 using namespace std;
  6 const int maxn = 110;
  7 int a, b, k;
  8 int vis[maxn][maxn];
  9 struct node {
 10     int v1, v2, step;
 11     vector<int> cmd;
 12 };
 13 
 14 void print(node & ans)
 15 {
 16     vector<int> & cmd = ans.cmd;
 17     for (int i = 0; i < cmd.size(); i++) {
 18         switch (cmd[i]) {
 19         case 1: cout << "FILL(1)\n"; break;
 20         case 2: cout << "FILL(2)\n"; break;
 21         case 3: cout << "DROP(1)\n"; break;
 22         case 4: cout << "DROP(2)\n"; break;
 23         case 5: cout << "POUR(1,2)\n"; break;
 24         case 6: cout << "POUR(2,1)\n"; break;
 25         }
 26     }
 27 }
 28 
 29 void solve()
 30 {
 31     node root;
 32     root.v1 = 0;
 33     root.v2 = 0;
 34     root.step = 0;
 35     vis[0][0] = 1;
 36     queue<node> qq;
 37     qq.push(root);
 38     node ans, x;
 39     int flag = 0;
 40     while (!qq.empty()) {
 41         ans = qq.front(); qq.pop();
 42         if (ans.v1 == k || ans.v2 == k) {flag = 1; break; }
 43         for (int i = 1; i <= 6; i++) {
 44             switch (i) {
 45             case 1: //fill 1
 46                 if (vis[a][ans.v2] == 0) {
 47                     x = ans;
 48                     x.cmd.push_back(1);
 49                     x.v1 = a;
 50                     x.step++;
 51                     vis[x.v1][x.v2] = 1;
 52                     qq.push(x);
 53                 }
 54                 break;
 55             case 2: //fill 2
 56                 if (vis[ans.v1][b] == 0) {
 57                     x = ans;
 58                     x.cmd.push_back(2);
 59                     x.v2 = b;
 60                     x.step++;
 61                     vis[x.v1][x.v2] = 1;
 62                     qq.push(x);
 63                 }
 64                 break;
 65             case 3: //drop 1
 66                 if (vis[0][ans.v2] == 0) {
 67                     x = ans;
 68                     x.cmd.push_back(3);
 69                     x.v1 = 0;
 70                     x.step++;
 71                     vis[x.v1][x.v2] = 1;
 72                     qq.push(x);
 73                 }
 74                 break;
 75             case 4:
 76                 if (vis[ans.v1][0] == 0) {
 77                     x = ans;
 78                     x.cmd.push_back(4);
 79                     x.v2 = 0;
 80                     x.step++;
 81                     vis[x.v1][x.v2] = 1;
 82                     qq.push(x);
 83                 }
 84                 break;
 85             case 5: //pour 1 to 2
 86                 x = ans;
 87                 x.v2 += x.v1;
 88                 if (x.v2 > b) {x.v1 = x.v2 - b; x.v2 = b;}
 89                 else x.v1 = 0;
 90                 if (vis[x.v1][x.v2] == 0) {
 91                     x.cmd.push_back(5);
 92                     x.step++;
 93                     qq.push(x);
 94                     vis[x.v1][x.v2] = 1;
 95                 }
 96                 break;
 97             case 6: //pour 2 to 1
 98                 x = ans;
 99                 x.v1 += x.v2;
100                 if (x.v1 > a) { x.v2 = x.v1 - a; x.v1 = a; }
101                 else x.v2 = 0;
102                 if (vis[x.v1][x.v2] == 0) {
103                     x.cmd.push_back(6);
104                     x.step++;
105                     qq.push(x);
106                     vis[x.v1][x.v2] = 1;
107                 }
108                 break;
109             }
110         }
111     }
112     if (flag) {
113         cout << ans.step << endl;
114         print(ans);
115     }
116     else cout << "impossible" << endl;
117 }
118 
119 int main()
120 {
121     cin >> a >> b >> k;
122     solve();
123 }

 

以上是关于POJ 3414 (BFS)的主要内容,如果未能解决你的问题,请参考以下文章

poj3414Pots(bfs)

poj3414 Pots(BFS)

POJ 3414 Pots(BFS)

POJ 3414 (BFS)

POJ - 3414 Pots (BFS+路径记录)

POJ 3414 Pots (BFS/DFS)