一、题面

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 ≤ i ≤ 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 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】的更多相关文章

  1. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

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

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

  3. poj 3414 Pots (bfs+线索)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special J ...

  4. Pots(BFS)

    Pots Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submiss ...

  5. (简单) POJ 3414 Pots,BFS+记录路径。

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  6. POJ-3414 Pots (BFS)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  7. POJ 3414 Pots(BFS+回溯)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11705   Accepted: 4956   Special J ...

  8. poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  9. poj3414 Pots (BFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   Special J ...

随机推荐

  1. Ajax——jQuery实现

    紧接上文 一.load()方法 load() 方法师jQuery中最为简单和常用的Ajax方法,能载入远程的HTML代码并插入到DOM中.它的机构是:load(url[,data][,callback ...

  2. 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available

    Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of ...

  3. PostBack

    PostBack 字面意义 Post提交 Back回来. 提交回来. 1. AutoPostBack 服务器控件需要设置 AutoPostBack="true" 后才会提交服务器. ...

  4. MVC5中 在更新 Microsoft.Aspnet.Identity 后编译器错误

    环境:vs2013预览版chs,我试着创建vb.net web应用,从对话框中选择MVC和WebAPI.编译ok了.通过NuGet管理器更新了Microsoft.Aspnet.Identity.Cor ...

  5. CString::MakeLower Crash

    记录一下使用CString::MakeLower可能导致的crash的一个问题: 问题重现: int _tmain(int argc, _TCHAR* argv[]){ std::string  sT ...

  6. 微信AES-128-CBC加密解密

    [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var key = "cheaye ...

  7. WPF中在摄像头视频上叠加控件的解决方案

    一.视频呈现 前段时间,在一个wpf的项目中需要实时显示ip摄像头,对此的解决方案想必大家都应该知道很多.在winform中,我们可以将一个控件(一般用panel或者pictruebox)的句柄丢给摄 ...

  8. 「POJ 2699」The Maximum Number of Strong Kings

    题目链接 戳我 \(Describe\) 一场联赛可以表示成一个完全图,点表示参赛选手,任意两点u, v之间有且仅有一条有向边\((u, v)\)或\((v, u)\),表示\(u\)打败\(v\)或 ...

  9. 编码原则实例------c++程序设计原理与实践(进阶篇)

    编码原则: 一般原则 预处理原则 命名和布局原则 类原则 函数和表达式原则 硬实时原则 关键系统原则 (硬实时原则.关键系统原则仅用于硬实时和关键系统程序设计) (严格原则都用一个大写字母R及其编号标 ...

  10. Hibernate学习第三天(2)(多对多关系映射)

    1.1.1      Hibernate多对多关系的配置 1.1.1.1   创建表 l   用户表 CREATE TABLE `sys_user` ( `user_id` bigint(32) NO ...