一、题面

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. Java IO简介

    -------------siwuxie095                 Java IO简介:     IO 也写作"I/O",可理解为 In 和 Out,即 输入 与 输出 ...

  2. Ubuntu下libpcap安装步骤

    第一步,先安装GCC ,一般都会自动安装 sudo apt-get install build-essential 第二步,GNU M4可以从此处ftp.gnu.org/gnu/m4/ 下载 sudo ...

  3. 打印单据,A4纸,每个单据占一个A4纸,两个单据之间不挨着

    打印单据,A4纸,每个单据占一个A4纸,两个单据之间不挨着 <style type="text/css" media="print">.Noprin ...

  4. Django框架 之 Ajax

    Django框架 之 Ajax 浏览目录 AJAX准备知识 AJAX与XML的比较 AJAX简介 jQuery实现的ajax AJAX参数 AJAX请求如何设置csrf_token 序列化 一.AJA ...

  5. 基于django rest framework做认证组件

    先导入要用到的类 from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions ...

  6. Part4_lesson3---U-Boot工作流程分析

    1.程序入口 我们从什么地方去找入口呢,首先是打开顶层目录的makefile文件,在这个文件里面,每一个uboot支持的开发板都有一个配置选项,比如说,搜索smdk2440,结果如下 我们主要关注上图 ...

  7. rest-framework-----视图

    一:基本视图 写一个出版社的增删改查的resful接口 路由: url(r'^publish/$', views.PublishView.as_view()), url(r'^publish/(?P& ...

  8. Date3.19

    1.正则表达式的定义及使用2.Date类的用法3.Calendar类的用法========================================================1正则表达式的 ...

  9. ORCHARD学习教程-介绍

    ORCHARD 是什么? Orchard 是由微软公司创建,基于 ASP.NET MVC 技术的免费开源内容管理系统: 可用于建设博客.新闻门户.企业门户.行业网站门户等各种网站 简单易用的后台界面 ...

  10. 随便写个bat存档

    @echo off @COLOR @echo ------------切换Hosts环境--------------- :Again @set /p choice="切换模式:A:应用环境, ...