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 ...
随机推荐
- Docker01 centos系统安装、centos安装docker、docker安装mongoDB
1 centos系统安装 本博文是基于 centos6.5 的,利用VMware 虚拟机搭建 centos6.5 系统 1.1 centos6.5资源获取 1.2 安装 1.2.1 新建虚拟机 1.2 ...
- 19、SOAP安装,运用与比对结果解释
转载:http://www.dengfeilong.com/post/Soap2.html https://blog.csdn.net/zhu_si_tao/article/details/71108 ...
- 安装sql server 2008重启失败
sql server2008安装时提示重启计算机失败怎么办 安装SQL Server 2008时,经常会遇到这样一个问题,软件提示“重启计算机失败”,如果忽略的话,会给后面的安装带来很大的麻烦,这 ...
- 时间日期控件的处理-Selenium
很多人问时间日期的空间怎么处理,但是时间日期控件各种各样,你可能遇到正常点的像这样: 当然也可能遇到难点的,像这样: 当然,也不排除会遇到变态的,像这样: 呵呵,真要一个个想着怎么去选择,简直是非人类 ...
- HDU 1540 Tunnel Warfare (线段树或set水过)
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好. 用线段 ...
- ios PNG Crush error (PNG图片错误)
我是这么解决的: I had the same problem. How to fix : Open up image with Preview -> File > Export > ...
- BZOJ3223 文艺平衡树(splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- delphi 金额大小写转换函数
{*------------------------------------------------ 金额大小写转换函数 @author 王云盼 @version V1506.01 在delphi7测 ...
- ping包,支持ip录入
@echo off ::等待用户输入需要监控IP set /p ip=Input the IP required to monitor: echo executing...... :start ech ...
- hbuilder h5 原生socket
在网上搜索了很多资料都不行,要么就是不能发送数据,要么就不能接收数据,使用如下的方法可以接收数据,一个一个字节接收: 有部分限制是需要明确知道要接收多少个字节,否则容易出现接收异常.. var tes ...