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 ...
随机推荐
- 批量添加数据SqlBulkCopy
using System.Data.SqlClient; class Program { static void Main() { string connectionString = GetConne ...
- 使用Cytoscape画PPI网络图
打开Cytoscape软件,根据菜单导入string_interactions.tsv文件 File ----> Import ----> Network from File 会弹出下图对 ...
- ShopNc实例化对象
1.模型 $model_member = Model('member'); 2.接口 require_once BASE_ROOT_PATH.'/member/api/smiMember/action ...
- dev 官网
https://www.devexpress.com/Support/Center/Example/Details/E1343 <%@ Page Language="C#" ...
- mybatis 获得一个map的返回集合
在使用mybatis 查询结果集,有时会有需求返回一个map比如表 id username 1 name1 2 name2 3 name3 希望的查询结果是一个map 并且以id为key 表为实体 ...
- [GO]数组的比较和赋值
package main import "fmt" func main() { //支持比较,只支持==或!=,比较是不是每一个元素都一样,2个数据比较,数据类型要一样 a := ...
- Vivado生成edf文件
https://china.xilinx.com/support/answers/54074.html 综合完成后会跳出个框框,选择open synthesis write_edif module. ...
- (转)通过Javascript得到URL中的参数(query string)
原文地址:http://www.cnblogs.com/season-huang/p/3322561.html 我们知道,"GET"请求中,通常把参数放在URL后面,比如这样htt ...
- 编写高质量代码改善C#程序的157个建议——建议36:使用FCL中的委托声明
建议36:使用FCL中的委托声明 FCL中存在3类这样的委托声明,它们分别是:Action.Func.Predicate.尤其是在它们的泛型版本出来以后,已经能够满足我们在实际编码过程中的大部分需求. ...
- 验证码-WebVcode
验证码的实现 <img src="../Common/WebVcode.aspx" title="看不清?点此更换" alt="看不清?点此更换 ...