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:
- 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; 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)
Source
//2017-02-24
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; struct node
{
int a, b, step, pre, op;
}q[]; int pots[], POT[], book[][];
string option[] = {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"}; void fill(int i)
{
pots[i] = POT[i];
} void drop(int i)
{
pots[i] = ;
} void pour(int i, int j)
{
if(pots[i] > POT[j]-pots[j]){
pots[i] -= (POT[j]-pots[j]);
pots[j] = POT[j];
}else{
pots[j] += pots[i];
pots[i] = ;
}
} void init(int a, int b)
{
pots[] = a;
pots[] = b;
} void push(int pos, int a, int b, int step, int pre, int op)
{
q[pos].a = a;
q[pos].b = b;
q[pos].step = step;
q[pos].pre = pre;
q[pos].op = op;
} void print(int pos)
{
if(q[pos].pre == -)return;
print(q[pos].pre);
cout<<option[q[pos].op]<<endl;
} int min(int a, int b)
{
return a < b ? a : b;
} int main()
{
int C, a, b, step;
while(cin>>POT[]>>POT[]>>C)
{
int head = , tail = ;
q[].a = ;
q[].b = ;
q[].step = ;
q[].pre = -;
memset(book, , sizeof(book));
book[][] = ;
while(head < tail)
{
a = q[head].a;
b = q[head].b;
step = q[head].step; if(a==C || b==C){
cout<<step<<endl;
print(head);
break;
} init(a, b);
fill();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} init(a, b);
fill();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} if(a>){
init(a, b);
drop();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
}
} if(b>){
init(a, b);
drop();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
}
} init(a, b);
pour(, );
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} init(a, b);
pour(, );
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} head++;
}
if(head>=tail)cout<<"impossible"<<endl;
}
return ;
}
POJ3414(KB1-H BFS)的更多相关文章
- poj3414 Pots (BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12198 Accepted: 5147 Special J ...
- POJ-3414 Pots (BFS)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj3414 Pots(BFS)
题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...
- FZU - 2150 bfs [kuangbin带你飞]专题一
题意:两个人玩很变态的游戏,将一个草坪的某两个点点燃,点燃的草坪可以向上下左右四个方向扩散,问能否将整块草坪上面的草都点燃.如果能,输出最短时间(^_^他们就能玩更变态的游戏了),如果不能,输出-1. ...
- UVA-11882 bfs + dfs + 剪枝
假设当前已经到达(x,y),用bfs判断一下还可以到达的点有maxd个,如果maxd加上当前已经经过的长度小于当前答案的长度就退出,如果相同,就将bfs搜索到的点从大到小排序,如果连最大序列都无法大于 ...
- 广度优先(bfs)和深度优先搜索(dfs)的应用实例
广度优先搜索应用举例:计算网络跳数 图结构在解决许多网络相关的问题时直到了重要的作用. 比如,用来确定在互联网中从一个结点到另一个结点(一个网络到其他网络的网关)的最佳路径.一种建模方法是采用无向图, ...
- 邻接矩阵实现图的存储,DFS,BFS遍历
图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...
- ZH奶酪:【数据结构与算法】搜索之BFS
1.目标 通过本文,希望可以达到以下目标,当遇到任意问题时,可以: 1.很快建立状态空间: 2.提出一个合理算法: 3.简单估计时空性能: 2.搜索分类 2.1.盲目搜索 按照预定的控制策略进行搜索, ...
- 扫描线+堆 codevs 2995 楼房
2995 楼房 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...
- 【BZOJ 1054】 [HAOI2008]移动玩具
Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动 ...
随机推荐
- 隐藏导航栏,偏移20PX
1:让ViewController在NavigationBar下面开始布局. automaticallyAdjustsScrollViewInsets作用 当Controller上存在唯一一个UISc ...
- Mysql数据库二:表的增删改查
----建表CREATE TABLE emp( id int PRIMARY key auto_increment, name char(10) , birthday DATE , salary FL ...
- [JavaScript] 获取数组中相同元素的个数
/** * 获取数组中相同元素的个数 * @param val 相同的元素 * @param arr 传入数组 */ function getSameNum(val,arr){ processArr ...
- linux的hostname(主机名)修改详解
Linux操作系统的hostname是一个kernel变量,可以通过hostname命令来查看本机的hostname.也可以直接cat /proc/sys/kernel/hostname查看. #ho ...
- jmeter插件之自定义场景图(万能场景设计)
添加扩展插件 自定义线程组:jp@gc - Ultimate Thread Group 此线程组功能强大,可以实现多种场景设置,添加路径如图 参数含义解释 Start Threads Count:当前 ...
- iOS多线程---NSOperation介绍和使用
1. NSOperation实现多线程编程,需要和NSOperationQueue一起使用. (1)先将要执行的操作封装到NSOperation中 (2)将NSOperation对象添加到NSOpe ...
- android view知识点 总结
DecorView : http://www.jianshu.com/p/5aa96683d0dc 安卓事件分发机制: http://blog.csdn.net/guolin_blog/article ...
- 利用Windows资源监视器解决文件夹无法改名无法删除问题
在win7等Windows系统操作文件夹更名.删除时经常会报错,操作无法完成,balabala 这个时候仅凭在用的软件去一个一个找是很难的,即便软件全关了,还有后台进程,,,奔溃吧 好了,现在我们有了 ...
- jboss8+EJB3+MDB Queue
1)在使用jboss8即WildFly进行MDB的试验时首先要在jboss8中配置jms 队列. 我使用的是修改配置文件的方式配置Jms Queue队列. 进入jboss8 安装目录的standalo ...
- java对象流与序列化
Object流,直接把obj写入或读出. 前言: 比如 画图的程序,咣当画一个三角形出来,咣当画一正方形出来.然后存盘,当你下次再打开软件的时候三角形.方块还在原来的位置上.如果用面向对象的思维,三角 ...