POJ 3414 Pots(容量BFS)

Pots
 

大意:给你两个碗,三种操作,看多少步能凑出给你的容量。

思路:六入口的BFS,难点主要是在记录路径打印上,只要再开一个数组,记录一下前驱,最后按顺序打印即可。

 
 
  1 #include <stdio.h>
  2 #include <iostream>
  3 #include <queue>
  4 #include <algorithm>
  5 #include <stack>
  6 using namespace std;
  7 
  8 int vis[110][110];
  9 int a, b, c;
 10 
 11 struct node
 12 {
 13     int x, y, step;
 14 } ;
 15 
 16 struct Path
 17 {
 18     int n, m, path;
 19 } R[110][110];    ///保存前驱,方便打印
 20 
 21 void output(int x, int y)
 22 {
 23     stack<Path> p;
 24     while(!p.empty())
 25         p.pop();
 26     while(!(x == 0 && y == 0))
 27     {
 28         p.push(R[x][y]);
 29         int n_x = R[x][y].n;
 30         int n_y = R[x][y].m;
 31         x = n_x;
 32         y = n_y;
 33     }
 34     while(!p.empty())
 35     {
 36         switch(p.top().path)
 37         {
 38             case 1:printf("FILL(1)\n");break;
 39             case 2:printf("FILL(2)\n");break;
 40             case 3:printf("DROP(1)\n");break;
 41             case 4:printf("DROP(2)\n");break;
 42             case 5:printf("POUR(1,2)\n");break;
 43             case 6:printf("POUR(2,1)\n");break;
 44         }
 45         p.pop();
 46     }
 47 }
 48 
 49 void BFS()
 50 {
 51     queue<node>q;
 52     while(!q.empty())
 53         q.pop();
 54     q.push((node){0, 0, 0});
 55     vis[0][0] = 1;
 56     while(!q.empty())
 57     {
 58         node t = q.front();
 59         q.pop();
 60         if(t.x == c || t.y == c)
 61         {
 62             printf("%d\n", t.step);
 63             output(t.x, t.y);
 64             return ;
 65         }
 66         if(t.x < a && !vis[a][t.y])
 67         {
 68             vis[a][t.y] = 1;
 69             q.push((node){a, t.y, t.step+1});
 70             R[a][t.y] = (Path){t.x, t.y, 1};
 71         }
 72         if(t.y < b && !vis[t.x][b])
 73         {
 74             vis[t.x][b] = 1;
 75             q.push((node){t.x, b, t.step+1});
 76             R[t.x][b] = (Path){t.x, t.y, 2};
 77         }
 78         if(t.x > 0 && !vis[0][t.y])
 79         {
 80             vis[0][t.y] = 1;
 81             q.push((node){0, t.y, t.step+1});
 82             R[0][t.y] = (Path){t.x, t.y, 3};
 83         }
 84         if(t.y > 0 && !vis[t.x][0])
 85         {
 86             vis[t.x][0] = 1;
 87             q.push((node){t.x, 0, t.step+1});
 88             R[t.x][0] = (Path){t.x, t.y, 4};
 89         }
 90         if(t.x > 0 && t.y < b)
 91         {
 92             int k = min(t.x, b-t.y);
 93             if(!vis[t.x-k][t.y+k])
 94             {
 95                 vis[t.x-k][t.y+k] = 1;
 96                 q.push((node){t.x-k, t.y+k, t.step+1});
 97                 R[t.x-k][t.y+k] = (Path){t.x, t.y, 5};
 98             }
 99         }
100         if(t.x < a && t.y > 0)
101         {
102             int k = min(a-t.x, t.y);
103             if(!vis[t.x+k][t.y-k])
104             {
105                 vis[t.x+k][t.y-k] = 1;
106                 q.push((node){t.x+k, t.y-k, t.step+1});
107                 R[t.x+k][t.y-k] = (Path){t.x, t.y, 6};
108             }
109         }
110     }
111     printf("impossible\n");
112 }
113 
114 void Solve()
115 {
116     scanf("%d%d%d", &a, &b, &c);
117     BFS();
118 }
119 
120 int main(void)
121 {
122     Solve();
123 
124     return 0;
125 }
Pots

 

posted @ 2013-12-26 11:06  GLSilence  阅读(173)  评论(0编辑  收藏  举报