题目链接:http://poj.org/problem?id=3414

Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18550   Accepted: 7843   Special Judge

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

题解:

类似的题目,也是有关几个容器倒来倒去,然后求一个目标量:http://blog.csdn.net/dolfamingo/article/details/77804030

此题不过是多了个路径输出,队列把STL换成数组就可以了,不细述。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node
{
//con[i]为容器i当前的量;op存操作的信息;pre为上一步所在队列的下标(为了输出路径)
int con[], op[], step, pre;
};
int vis[MAXN][MAXN], vol[]; //vol[0]、vol[1]为两个容器的容量, vol[3]为目标量 node que[]; //由于要输出路径,就要用数组来作队列了
int front, rear;
int bfs()
{
ms(vis,);
front = rear = ; node now, tmp;
now.con[] = now.con[] = ;
now.step = ;
vis[][] = ;
que[rear++] = now; while(front!=rear)
{
now = que[front++];
if(now.con[]==vol[] || now.con[]==vol[])
return front-; for(int i = ; i<; i++)
{
tmp = now; //操作1:FILL
tmp.con[i] = vol[i];
if(!vis[tmp.con[]][tmp.con[]])
{
vis[tmp.con[]][tmp.con[]] = ;
tmp.op[] = ; tmp.op[] = i;
tmp.step = now.step+;
tmp.pre = front-;
que[rear++] = tmp;
} tmp = now; //操作2:DROP
tmp.con[i] = ;
if(!vis[tmp.con[]][tmp.con[]])
{
vis[tmp.con[]][tmp.con[]] = ;
tmp.op[] = ; tmp.op[] = i;
tmp.step = now.step+;
tmp.pre = front-;
que[rear++] = tmp;
} tmp = now; //操作三POUR
int j = !i; //另外一个容器
int pour = min(tmp.con[i], vol[j]-tmp.con[j]);
tmp.con[j] += pour;
tmp.con[i] -= pour;
if(!vis[tmp.con[]][tmp.con[]])
{
vis[tmp.con[]][tmp.con[]] = ;
tmp.op[] = ; tmp.op[] = i; tmp.op[] = j;
tmp.step = now.step+;
tmp.pre = front-;
que[rear++] = tmp;
}
}
}
return -;
} void Print(int i) //输出路径
{
if(que[i].pre!=)
Print(que[i].pre); if(que[i].op[]==)
printf("FILL(%d)\n", que[i].op[]+);
else if(que[i].op[]==)
printf("DROP(%d)\n", que[i].op[]+);
else
printf("POUR(%d,%d)\n", que[i].op[]+, que[i].op[]+);
} int main()
{
while(scanf("%d%d%d",&vol[], &vol[], &vol[])!=EOF)
{
int i = bfs();
if(i==-)
puts("impossible");
else
{
printf("%d\n",que[i].step);
Print(i);
}
}
}

POJ3414 Pots —— BFS + 模拟的更多相关文章

  1. POJ3414—Pots(bfs加回溯)

    http://poj.org/problem?id=3414                                       Pots Time Limit: 1000MS   Memor ...

  2. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  3. POJ-3414.Pots.(BFS + 路径打印)

    这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...

  4. POJ3414 Pots BFS搜素

    题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...

  5. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  6. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

  7. hdu_1495_非常可乐(bfs模拟)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:不解释 题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长 ...

  8. Hdu 5336 XYZ and Drops (bfs 模拟)

    题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...

  9. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

随机推荐

  1. ElasticSearch 多索引

    1.用逗号将索引隔开,如: $ curl -XPOST http://localhost:9200/aaa,website/_search/ { "took": 1, " ...

  2. 清除svn检出导致的所有文件的问号

    问题:将svn项目检出到桌面后,桌面的图标都有问号了,怎么消除这一大堆问号? 解决方案:(1)新建一个a.txt文件,把这行代码复制进去for /r . %%a in (.) do @if exist ...

  3. cobbler api接口开发测试实例

    条件1:必须搭建好cobbler服务,并且可以通过web访问:http://cobbler_ip/cobbler_web 测试可以打开.然后再用以下命令测试. #!/opt/python3/bin/p ...

  4. P1067 多项式输出 (模拟)

    题目描述 一元nn次多项式可用如下的表达式表示: 其中,a_i x^i 称为i次项,ai​ 称为i次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: 多项式中自变量 ...

  5. 关于RPi.GPIO、BCM2835 c library、WiringPi、Gertboard

    1.RPi.GPIO//RPi.GPIO-0.5.5.tar.gz 开发者:python官网:https://www.python.org/ 官网:https://pypi.python.org/py ...

  6. LeetCode第一题以及时间复杂度的计算

    问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: I ...

  7. Hadoop HDFS 常用命名

    HDFS命令基本格式:hadoop fs -cmd < args > ls 命令hadoop fs -ls / 列出hdfs文件系统根目录下的目录和文件 hadoop fs -ls -R ...

  8. jquery 实现鼠标点击div盒子移动功能

    // Start 窗口的拖动 var _move=false; //移动标记 var _x,_y; //鼠标离控件左上角的相对位置 $(document).ready(function(){ $(&q ...

  9. [反汇编练习] 160个CrackMe之023

    [反汇编练习] 160个CrackMe之023. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  10. SolidEdge 工程图中如何绘制中断视图

    右击长条形的视图,点击新增断裂线,然后绘制两个断点   点击完成之后效果如下图所示   如果要修改断裂视图的样式,则选中这个视图,在左键单击,然后点击这个按钮取消显示断裂视图   然后左键单击断裂视图 ...