poj 1606

http://poj.org/problem?id=1606

题意:有两个容量为a,b的被子,用这两个被子量出c的水。

思路:bfs和记录路径

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <queue>
  4 
  5 using namespace std;
  6 struct Node{
  7     int pre,a,b;
  8     int num,f;
  9 }node[2000000];
 10 int pos;
 11 queue<Node>s;
 12 bool mark[500][500];
 13 
 14 void print(int t)
 15 {
 16     switch(t)
 17     {
 18         case 1:
 19             printf("fill A
");
 20             break;
 21         case 2:
 22             printf("fill B
");
 23             break;
 24         case 3:
 25             printf("empty A
");
 26             break;
 27         case 4:
 28             printf("empty B
");
 29             break;
 30         case 5:
 31             printf("pour A B
");
 32             break;
 33         case 6:
 34             printf("pour B A
");
 35             break;
 36         case 7:
 37             printf("success
");
 38         default:
 39             break;
 40     }
 41 }
 42 void put(int t){
 43     if(t!=0)
 44     {
 45         put(node[t].pre);
 46         print(node[t].f);
 47     }
 48 }
 49 
 50 void bfs(int a,int b,int c)
 51 {
 52     while(!s.empty())
 53         s.pop();
 54     node[pos].a = 0;
 55     node[pos].b = 0;
 56     node[pos].pre = 0;
 57     node[pos].num = pos;
 58     mark[0][0] = false;
 59     s.push(node[pos++]);
 60     while(!s.empty())
 61     {
 62         Node tmp = s.front();
 63         s.pop();
 64         if(tmp.b==c)
 65         {
 66             node[pos].f = 7;
 67             node[pos].pre = tmp.num;
 68             put(pos);
 69             return;
 70         }
 71         if(mark[a][tmp.b])
 72         {
 73             node[pos].a = a;
 74             node[pos].b = tmp.b;
 75             node[pos].num = pos;
 76             node[pos].pre = tmp.num;
 77             node[pos].f = 1;
 78             mark[a][tmp.b] = false;
 79             s.push(node[pos++]);
 80         }
 81         if(mark[tmp.a][b])
 82         {
 83             node[pos].a = tmp.a;
 84             node[pos].b = b;
 85             node[pos].num = pos;
 86             node[pos].pre = tmp.num;
 87             node[pos].f = 2;
 88             mark[tmp.a][b] = false;
 89             s.push(node[pos++]);
 90         }
 91         if(mark[0][tmp.b])
 92         {
 93             node[pos].a = 0;
 94             node[pos].b = tmp.b;
 95             node[pos].num = pos;
 96             node[pos].pre = tmp.num;
 97             node[pos].f = 3;
 98             mark[0][tmp.b] = false;
 99             s.push(node[pos++]);
100         }
101         if(mark[tmp.a][0])
102         {
103             node[pos].a = tmp.a;
104             node[pos].b = 0;
105             node[pos].num = pos;
106             node[pos].pre = tmp.num;
107             node[pos].f = 4;
108             mark[tmp.a][0] = false;
109             s.push(node[pos++]);
110         }
111         if(tmp.a>0)
112         {
113             int t = tmp.a+tmp.b;
114             node[pos].a = t-b<0?0:t-b;
115             node[pos].b = t<b?t:b;
116             if(mark[node[pos].a][node[pos].b])
117             {
118                 node[pos].num = pos;
119                 node[pos].pre = tmp.num;
120                 node[pos].f = 5;
121                 mark[node[pos].a][node[pos].b] = false;
122                 s.push(node[pos++]);
123             }
124         }
125         if(tmp.b>0)
126         {
127             int t = tmp.a+tmp.b;
128             node[pos].b = (t-a<0)?0:t-a;
129             node[pos].a = t<a?t:a;
130             if(mark[node[pos].a][node[pos].b])
131             {
132                 node[pos].num = pos;
133                 node[pos].pre = tmp.num;
134                 node[pos].f = 6;
135                 mark[node[pos].a][node[pos].b] = false;
136                 s.push(node[pos++]);
137             }
138         }
139     }
140 }
141 
142 int main()
143 {
144     int a,b,c;
145     while(~scanf("%d%d%d",&a,&b,&c))
146     {
147         memset(mark,true,sizeof(mark));
148         pos = 0;
149         bfs(a,b,c);
150     }
151     return 0;
152 }
原文地址:https://www.cnblogs.com/Tree-dream/p/6657544.html