POJ_3414 Pots 【复杂BFS】
一、题面
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)
二、分析
对于这题,难点不在BFS的思路,难点在于BFS每一次父节点生成孩子结点的时候,情况比较复杂。对于记录路径,仍然需要使用孩子节点标记一个前缀指向父节点,然后用递归的方式实现即可。自己在写代码的时候非常不注意,在生成孩子节点时,对于标记访问的数组,本来应该用=,但我直接复制的判断条件里的==,导致一直RE。谨记!
三、AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = ;
bool visit[MAXN+][MAXN+];
int A, B, C; struct Point
{
int first, second, cnt;
int prev, id; //父节点和操作对象
char op; //操作
}; Point P[MAXN*MAXN + ];
int Cnt, Ans; void Output(int t)
{
if(P[t].prev != -)
{
Ans++;
Output(P[t].prev);
}
if(Ans != -)
{
printf("%d\n", Ans);
Ans = -;
}
if(P[t].op=='F')
{
printf("FILL(%d)\n", P[t].id);
}
else if(P[t].op == 'P')
{
printf("POUR(%d,%d)\n", P[t].id, P[t].id==?:);
}
else if(P[t].op == 'D')
{
printf("DROP(%d)\n", P[t].id);
}
} void BFS()
{
Point t;
int cur;
t.first = , t.second = ;
visit[][] = ;
t.op = '', t.prev = -, t.id = -;
t.cnt = ;
P[] = t;
Cnt = ;
cur = ; while(true)
{
if(cur >= Cnt)
{
printf("impossible\n");
return;
}
Point pt = P[cur++]; if(pt.first == C || pt.second == C)
{
Ans = ;
Output(pt.cnt);
break;
} t.prev = pt.cnt; if(pt.first < A)
{
t.first = A;
t.second = pt.second;
if(visit[t.first][t.second] == )
{
//visit[t.first][t.second] == 1; 刚开始RE的原因
visit[t.first][t.second] = ;
t.op = 'F';
t.id = ;
t.cnt = Cnt;
P[Cnt++] = t; }
} if(pt.second < B)
{
t.first = pt.first;
t.second = B;
if(visit[t.first][t.second] == )
{
visit[t.first][t.second] = ;
t.op = 'F';
t.id = ;
t.cnt = Cnt;
P[Cnt++] = t;
}
} if(pt.first < A && pt.second > )
{
t.first = pt.first + pt.second;
t.second = t.first - A;
if(t.second < )
t.second = ;
else
t.first = A;
if(visit[t.first][t.second] == )
{
visit[t.first][t.second] = ;
t.op = 'P';
t.id = ;
t.cnt = Cnt;
P[Cnt++] = t;
}
} if(pt.second < B && pt.first > )
{
t.second = pt.second + pt.first;
t.first = t.second - B;
if(t.first < )
t.first = ;
else
t.second = B;
if(visit[t.first][t.second] == )
{
visit[t.first][t.second] = ;
t.op = 'P';
t.id = ;
t.cnt = Cnt;
P[Cnt++] = t;
}
} if(pt.first > )
{
t.first = ;
t.second = pt.second;
if(visit[t.first][t.second] == )
{
visit[t.first][t.second] = ;
t.op = 'D';
t.id = ;
t.cnt = Cnt;
P[Cnt++] = t;
}
} if(pt.second > )
{
t.first = pt.first;
t.second = ;
if(visit[t.first][t.second] == )
{
visit[t.first][t.second] = ;
t.op = 'D';
t.id = ;
t.cnt = Cnt;
P[Cnt++] = t;
}
}
}
} int main()
{
while(scanf("%d %d %d", &A, &B, &C)!=EOF)
{
memset(visit, , sizeof(visit));
BFS();
}
return ;
}
POJ_3414 Pots 【复杂BFS】的更多相关文章
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- Pots(BFS)
Pots Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submiss ...
- (简单) POJ 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ-3414 Pots (BFS)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ 3414 Pots(BFS+回溯)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj3414 Pots (BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12198 Accepted: 5147 Special J ...
随机推荐
- 基于jquery 的插件,让IE支持placeholder属性
开发一个项目的时候为了美观和用户体验用到了input标签的placeholder属性,但是这个属性是html5中的,所以低版本的IE浏览器不支持.于是在百度找了一些解决方法,找了好几个都不是那么完美, ...
- unity5.6里Baked Lighting下面几个Lighting Mode的解释
这个似乎是新增的功能,目前在官方文档里还没有解释,但有一个链接指向『草案』, 可是链接在被墙的google doc上,为了方便阅读和备忘,全部贴过来整在一起: 因为原文太长,把总结写在前面吧: 1.四 ...
- Part5核心初始化_lesson1---异常向量表
1.1异常 异常向量: 异常向量表: 代码的编写 start.S文件 gboot.lds链接器脚本文件 makefile工程文件:
- Part6-点亮指路灯_lesson1
1. 2.GPIO 查阅芯片手册:GPIO 代码: 3.外设基地址初始化 打开arm核手册, 基地址为0x70000000,去搜芯片手册6410, 把这个基地址告诉处理器,通过协处理器的cp15, 转 ...
- JPA注解解析
最近学习hibernate注解形式配置POJO类,将注解的解析记下来,以备以后使用. 例1. @Entity@Table(name="user") public class Fl ...
- SVG素材整理(原)
why SVG? 1.矢量的国际图形标准,以后随着浏览器的发展,相信矢量会更多的出现 2.illustartor等多数矢量绘图软件可以导出为这种格式 so SVG现状 基本介绍:http:// ...
- QuotedStr函数
今天学到一个新函数,很有用 QuotedStr(s);// 在s两边加单引号, 这样就不会看着n多的单引号糊涂了...
- ubuntu 下python安装及hello world
//@desn:ubuntu 下python安装及hello world //@desn:码字不宜,转载请注明出处 //@author:张慧源 <turing_zhy@163.com> ...
- 2张图简单分析count(0)与count(*)
以前一直以为count(0)查询效率比count(*)比较高,原因大概是这么认为count(0)只是第一列进行统计,而count(*)所有列放在一起统计(亲,不要误会,这里不是所有列累加哦) 结果真的 ...
- 20165219 2017-2018-2 《Java程序设计》第6周学习总结
20165219 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容小结 第八章 String类 常用方法 public int length() 求字符串长度 pu ...