POJ 3414 Pots

题目链接:POJ 3414

Describe:

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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)

题目大意:

给两个杯子,分别为A升和B升,问能否通过上面三种操作得到C升水。

解题思路:

很明显的bfs,搜索,难点可能是判断情况比较多。

AC代码:

 

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <queue>
 4 using namespace std;
 5 int a,b,c,t1,t2,g[110][110];
 6 string aa[6] = {"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(2,1)","POUR(1,2)"};
 7 struct node
 8 {
 9     int x,y,step;
10     string s; // 存储步骤
11 };
12 int main()
13 {
14     queue<node> q;
15     cin >> a >> b >> c;
16     node st = {0,0,0,""};
17     g[0][0] = 1;
18     q.push(st);
19     while(!q.empty()) // bfs
20     {
21         node tmp = q.front(); q.pop();
22         if(tmp.x == c || tmp.y == c) // 打印结果
23         {
24             cout << tmp.step << endl;
25             for(int i = 0; i < tmp.s.size(); i++)
26                 cout << aa[tmp.s[i]-'0'] << endl;
27             return 0;
28         }                              // 以下分别是几种情况
29         if(tmp.x < a && !g[a][tmp.y])
30             q.push({a,tmp.y,tmp.step+1,tmp.s+"0"}),g[a][tmp.y] = 1;
31         if(tmp.y < b && !g[tmp.x][b])
32             q.push({tmp.x,b,tmp.step+1,tmp.s+"1"}),g[tmp.x][b] = 1;
33         if(tmp.x > 0 && !g[0][tmp.y])
34             q.push({0,tmp.y,tmp.step+1,tmp.s+"2"}),g[0][tmp.x] = 1;
35         if(tmp.y > 0 && !g[tmp.x][0])
36             q.push({tmp.x,0,tmp.step+1,tmp.s+"3"}),g[tmp.x][0] = 1;
37         if(tmp.x < a && tmp.y > 0) // 2向1倒水
38         {
39             int tmp1 = a-tmp.x;
40             if(tmp1 >= tmp.y && !g[tmp.x+tmp.y][0])
41                 q.push({tmp.x+tmp.y,0,tmp.step+1,tmp.s+"4"}),g[tmp.x+tmp.y][0] = 1;
42             if(tmp1 < tmp.y && !g[a][tmp.x+tmp.y-a])
43                 q.push({a,tmp.x+tmp.y-a,tmp.step+1,tmp.s+"4"}),g[a][tmp.x+tmp.y-a] = 1;
44         }
45         if(tmp.x > 0 && tmp.y < b) // 1向2倒水
46         {
47             int tmp1 = b-tmp.y;
48             if(tmp1 >= tmp.x && !g[0][tmp.x+tmp.y])
49                 q.push({0,tmp.x+tmp.y,tmp.step+1,tmp.s+"5"}),g[0][tmp.x+tmp.y] = 1;
50             if(tmp1 < tmp.x && !g[tmp.x+tmp.y-b][b])
51                 q.push({tmp.x+tmp.y-b,b,tmp.step+1,tmp.s+"5"}),g[tmp.x+tmp.y-b][b] = 1;
52         }
53     }
54     cout << "impossible"; // 没有找到
55     return 0;
56 }

 

posted @ 2020-09-02 22:35  不敢说的梦  阅读(204)  评论(0)    收藏  举报