POJ3414(KB1-H BFS)

Pots

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 ≤ ≤ 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 AB, 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)

Source

Northeastern Europe 2002, Western Subregion

  1 //2017-02-24
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <algorithm>
  6
  7 using namespace std;
  8
  9 struct node
 10 {
 11     int a, b, step, pre, op;
 12 }q[1000];
 13
 14 int pots[3], POT[3], book[105][105];
 15 string option[10] = {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
 16
 17 void fill(int i)
 18 {
 19     pots[i] = POT[i];
 20 }
 21
 22 void drop(int i)
 23 {
 24     pots[i] = 0;
 25 }
 26
 27 void pour(int i, int j)
 28 {
 29     if(pots[i] > POT[j]-pots[j]){
 30         pots[i] -= (POT[j]-pots[j]);
 31         pots[j] = POT[j];
 32     }else{
 33         pots[j] += pots[i];
 34         pots[i] = 0;
 35     }
 36 }
 37
 38 void init(int a, int b)
 39 {
 40     pots[1] = a;
 41     pots[2] = b;
 42 }
 43
 44 void push(int pos, int a, int b, int step, int pre, int op)
 45 {
 46     q[pos].a = a;
 47     q[pos].b = b;
 48     q[pos].step = step;
 49     q[pos].pre = pre;
 50     q[pos].op = op;
 51 }
 52
 53 void print(int pos)
 54 {
 55     if(q[pos].pre == -1)return;
 56     print(q[pos].pre);
 57     cout<<option[q[pos].op]<<endl;
 58 }
 59
 60 int min(int a, int b)
 61 {
 62     return a < b ? a : b;
 63 }
 64
 65 int main()
 66 {
 67     int C, a, b, step;
 68     while(cin>>POT[1]>>POT[2]>>C)
 69     {
 70         int head = 0, tail = 1;
 71         q[0].a = 0;
 72         q[0].b = 0;
 73         q[0].step = 0;
 74         q[0].pre = -1;
 75         memset(book, 0, sizeof(book));
 76         book[0][0] = 1;
 77         while(head < tail)
 78         {
 79             a = q[head].a;
 80             b = q[head].b;
 81             step = q[head].step;
 82
 83             if(a==C || b==C){
 84                 cout<<step<<endl;
 85                 print(head);
 86                 break;
 87             }
 88
 89             init(a, b);
 90             fill(1);
 91             if(!book[pots[1]][pots[2]]){
 92                 book[pots[1]][pots[2]] = 1;
 93                 push(tail, pots[1], pots[2], step+1, head, 1);
 94                 tail++;
 95             }
 96
 97             init(a, b);
 98             fill(2);
 99             if(!book[pots[1]][pots[2]]){
100                 book[pots[1]][pots[2]] = 1;
101                 push(tail, pots[1], pots[2], step+1, head, 2);
102                 tail++;
103             }
104
105             if(a>0){
106                 init(a, b);
107                 drop(1);
108                 if(!book[pots[1]][pots[2]]){
109                     book[pots[1]][pots[2]] = 1;
110                     push(tail, pots[1], pots[2], step+1, head, 3);
111                     tail++;
112                 }
113             }
114
115             if(b>0){
116                 init(a, b);
117                 drop(2);
118                 if(!book[pots[1]][pots[2]]){
119                     book[pots[1]][pots[2]] = 1;
120                     push(tail, pots[1], pots[2], step+1, head, 4);
121                     tail++;
122                 }
123             }
124
125             init(a, b);
126             pour(1, 2);
127             if(!book[pots[1]][pots[2]]){
128                 book[pots[1]][pots[2]] = 1;
129                 push(tail, pots[1], pots[2], step+1, head, 5);
130                 tail++;
131             }
132
133             init(a, b);
134             pour(2, 1);
135             if(!book[pots[1]][pots[2]]){
136                 book[pots[1]][pots[2]] = 1;
137                 push(tail, pots[1], pots[2], step+1, head, 6);
138                 tail++;
139             }
140
141             head++;
142         }
143         if(head>=tail)cout<<"impossible"<<endl;
144     }
145     return 0;
146 }
时间: 2024-12-26 18:19:41

POJ3414(KB1-H BFS)的相关文章

poj3414 Pots(BFS+路径输出)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj1606一样 :http://blog.csdn.net/u012860063/article/details/37772275 Description You are given two pots, having the volume of A and B liters respectively.

poj3414 Pots (BFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   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

poj3414——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 pot i to pot j

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

poj3414 Pots(BFS)

题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其中的一个杯子里的水恰为C升,输出最少步数和操作:如果不能倒到C升,输出“impossible”. 思路 这题与poj1606基本相同,在poj1606的基础上添加了输出最少步数,修改了操作的表示,以及不可能达到目标时输出impossible.将poj1606的代码略作修改即可. 代码 1 #incl

noip2013 华容道

P1979 华容道 131通过 471提交 题目提供者该用户不存在 标签图论搜索/枚举2013NOIp提高组 难度省选/NOI- 提交该题 讨论 题解记录 最新讨论 要注意部分数据存在起点与终… 求助求助求助 题目数据是不是有问题啊 题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 B 玩的华容道与经典的华容道游戏略有不同,游戏规则是这样的: 在一

HDU 搜索练习 Catch him

Catch him Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 797 Accepted Submission(s): 361 Problem Description 在美式足球中,四分卫负责指挥整只球队的进攻战术和跑位,以及给接球员传球的任务.四分卫是一只球队进攻组最重要的球员,而且一般身体都相对比较弱小,所以通常球队会安排5-7名大汉来

hdu 1533 Going Home 最小费用最大流 入门题

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3125    Accepted Submission(s): 1590 Problem Description On a grid map there are n little men and n houses. In each unit time, every

《网络流学习笔记01--HDU3549》

1.网络流初步. 网络流是一个适用范围相当广泛的模型,相关的算法也很多,这里就几天学习网络流的相关知识做一个总结归纳. (1)最大流问题 如图所示,假设你需要把一些物品从结点s(称为源点)运送到结点t(称为汇点),可以从其他结点中转,图(a)中各条有向边的权表示最多能有多少个物品从这条边的起点直接运送到终点,例如图(a)从结点V3到V2最多可以运送9个物品. 图(b)给出了一种可能的最优方案,其中每条边中的第一个数字表示实际运送的物品数量,第二个数字表示题目中的上限, 我们把求解这样的问题称为最

暑假集训day1

其实这是前天的事了.(现在时间回到两天前) 今天的主要内容是最短路和2-SAT 最短路我做了一题:水灾:题目详情见9018-1452 先bfs求出洪水漫延到每一个点的时间. 然后再跑一遍bfs求出最短路即可. #include<iostream> #include<cstdio> #include<cstring> using namespace std; int INF=0x7fffffff; int n,m,sx,sy,dx,dy,h=0,now=0; int t[