POJ-3414 Pots (BFS)

Description

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)

题目分析:打眼一看就是BFS,还是普通的BFS。

代码如下:
  1 # include<iostream>
  2 # include<cstdio>
  3 # include<string>
  4 # include<queue>
  5 # include<vector>
  6 # include<cstring>
  7 # include<algorithm>
  8 using namespace std;
  9 struct node
 10 {
 11     int a,b,t;
 12     vector<string>op;
 13     bool operator < (const node &a) const {
 14         return t>a.t;
 15     }
 16     node & operator = (const node &p) {
 17         a=p.a,b=p.b,t=p.t;
 18         op.clear();
 19         for(int i=0;i<p.op.size();++i)
 20             op.push_back(p.op[i]);
 21         return *this;
 22     }
 23 };
 24 int vis[105][105];
 25 void bfs(int A,int B,int C)
 26 {
 27     priority_queue<node>q;
 28     memset(vis,0,sizeof(vis));
 29     node sta;
 30     sta.a=sta.b=sta.t=0;
 31     sta.op.clear();
 32     vis[0][0]=1;
 33     q.push(sta);
 34     while(!q.empty())
 35     {
 36         node u=q.top();
 37         q.pop();
 38         if(u.a==C||u.b==C){
 39             printf("%d\n",u.t);
 40             for(int i=0;i<u.op.size();++i)
 41                 cout<<u.op[i]<<endl;
 42             return ;
 43         }
 44         if(u.a<A){
 45             node now=u;
 46             now.a=A,now.b=u.b;
 47             if(!vis[now.a][now.b]){
 48                 vis[now.a][now.b];
 49                 now.t=u.t+1;
 50                 now.op.push_back("FILL(1)");
 51                 q.push(now);
 52             }
 53         }
 54         if(u.b<B){
 55             node now=u;
 56             now.a=u.a,now.b=B;
 57             if(!vis[now.a][now.b]){
 58                 vis[now.a][now.b];
 59                 now.t=u.t+1;
 60                 now.op.push_back("FILL(2)");
 61                 q.push(now);
 62             }
 63         }
 64         if(u.a>0){
 65             node now=u;
 66             now.a=0,now.b=u.b;
 67             if(!vis[now.a][now.b]){
 68                 vis[now.a][now.b];
 69                 now.t=u.t+1;
 70                 now.op.push_back("DROP(1)");
 71                 q.push(now);
 72             }
 73         }
 74         if(u.b>0){
 75             node now=u;
 76             now.a=u.a,now.b=0;
 77             if(!vis[now.a][now.b]){
 78                 vis[now.a][now.b];
 79                 now.t=u.t+1;
 80                 now.op.push_back("DROP(2)");
 81                 q.push(now);
 82             }
 83         }
 84         if(u.a<A&&u.b>0){
 85             node now=u;
 86             now.a=min(A,u.a+u.b);
 87             now.b=max(0,u.b-A+u.a);
 88             if(!vis[now.a][now.b]){
 89                 vis[now.a][now.b]=vis[now.b][now.a]=1;
 90                 now.t=u.t+1;
 91                 now.op.push_back("POUR(2,1)");
 92                 q.push(now);
 93             }
 94         }
 95         if(u.a>0&&u.b<B){
 96             node now=u;
 97             now.a=max(0,u.a-B+u.b);
 98             now.b=min(B,u.b+u.a);
 99             if(!vis[now.a][now.b]){
100                 vis[now.a][now.b]=vis[now.b][now.a]=1;
101                 now.t=u.t+1;
102                 now.op.push_back("POUR(1,2)");
103                 q.push(now);
104             }
105         }
106     }
107     printf("impossible\n");
108 }
109 int main()
110 {
111     int A,B,C;
112     scanf("%d%d%d",&A,&B,&C);
113     bfs(A,B,C);
114     return 0;
115 }
时间: 2024-12-17 23:53:01

POJ-3414 Pots (BFS)的相关文章

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

poj 3414 Pots(BFS+递归打印)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10255   Accepted: 4333   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

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

题目链接:http://poj.org/problem?id=3414 题意:两个空杯子倒水,使得任意一个杯子中的水量达到题目要求,有6种操作:装满a,装满b,倒掉a,倒掉b,a->b,b->a. 题解:模拟一下操作,就好了. 1 #include <queue> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7

POJ 3414 Pots(BFS+回溯)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11705   Accepted: 4956   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

poj 3414 Pots (bfs+线索)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

poj 3414 pots (bfs 倒水问题)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int vis[120][120]; int vl,vr; int ok; int key; struct node { int l,r; int fa; int op; }; node str[100000]; int ans[100000]; void bfs() {

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

POJ 1562-Oil Deposits(BFS)

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 6888 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular r

POJ 3414 Pots (经典bfs )

Description 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

poj 3414 Pots【bfs+回溯路径 正向输出】

题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12080   Accepted: 5104   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operation