POJ 3414 Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13547   Accepted: 5718   Special Judge

Description

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 ≤ ≤ 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 AB, 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)
 /*和之前的那道倒水题目十分相似,唯一的难点就是输出倒水的方式,我们可以记录每一个搜到的状态的前一状态在队列中的编号和,该状态与前一状态的关系,再递归输出结果即可*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 10010
int head=-,tail=-;
int a,b,c;
bool flag[][]={};
struct node{
int a1,b1,pre,dis,relat;
}que[N];
int bfs()
{
while(head<tail)
{
++head;
if(que[head].a1==c||que[head].b1==c)
{
return head;
}
if(!flag[que[head].a1][])
{
flag[que[head].a1][]=true;
node nex;
nex.a1=que[head].a1;nex.b1=;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(!flag[][que[head].b1])
{
flag[][que[head].b1]=true;
node nex;
nex.a1=;nex.b1=que[head].b1;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(!flag[que[head].a1][b])
{
flag[que[head].a1][b]=true;
node nex;
nex.a1=que[head].a1;nex.b1=b;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(!flag[a][que[head].b1])
{
flag[a][que[head].b1]=true;
node nex;
nex.a1=a;nex.b1=que[head].b1;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].a1>=(b-que[head].b1)&&!flag[que[head].a1-(b-que[head].b1)][b])
{
flag[que[head].a1-(b-que[head].b1)][b]=true;
node nex;
nex.a1=que[head].a1-(b-que[head].b1);nex.b1=b;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].a1<(b-que[head].b1)&&!flag[][que[head].a1+que[head].b1])
{
flag[][que[head].a1+que[head].b1]=true;
node nex;
nex.a1=;nex.b1=que[head].a1+que[head].b1;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].b1>=(a-que[head].a1)&&!flag[a][que[head].b1-(a-que[head].a1)])
{
flag[a][que[head].b1-(a-que[head].a1)]=true;
node nex;
nex.a1=a;nex.b1=que[head].b1-(a-que[head].a1);
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].b1<(a-que[head].a1)&&!flag[que[head].a1+que[head].b1][])
{
flag[que[head].a1+que[head].b1][]=true;
node nex;
nex.a1=que[head].a1+que[head].b1;nex.b1=;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
}
return -;
}
void out(int temp)
{
if(que[que[temp].pre].pre!=-)
out(que[temp].pre);
if(que[temp].relat==)
printf("FILL(1)\n");
else if(que[temp].relat==)
printf("FILL(2)\n");
else if(que[temp].relat==)
printf("DROP(1)\n");
else if(que[temp].relat==)
printf("DROP(2)\n");
else if(que[temp].relat==)
printf("POUR(1,2)\n");
else printf("POUR(2,1)\n");
}
/*
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
*/
int main()
{
scanf("%d%d%d",&a,&b,&c);
++tail;
que[tail].a1=;que[tail].b1=;
que[tail].dis=;que[tail].pre=-;
flag[][]=true;
int temp=bfs();
if(temp==-)
printf("impossible\n");
else {
printf("%d\n",que[head].dis);
out(temp);
}
return ;
}

广搜+输出路径 POJ 3414 Pots的更多相关文章

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

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

  2. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  3. BFS POJ 3414 Pots

    题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...

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

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

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

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

  6. POJ 3414 Pots

    Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  7. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  8. poj 3414 Pots (bfs+线索)

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

  9. PTA 7-33 地下迷宫探索(深搜输出路径)

    地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活的同时,真心钦佩他们的聪明才智. ...

随机推荐

  1. AC自动机(1)

    Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).    ...

  2. 泛函编程(14)-try to map them all

    虽然明白泛函编程风格中最重要的就是对一个管子里的元素进行操作.这个管子就是这么一个东西:F[A],我们说F是一个针对元素A的高阶类型,其实F就是一个装载A类型元素的管子,A类型是相对低阶,或者说是基础 ...

  3. 常用 Git 命令清单(摘录)

    来源:阮一峰的网络日志 网址:http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 我每天使用 Git ,但是很多命令记不住. 一般来 ...

  4. ANSI X9.19 MAC算法

    /// <summary> /// 获取MAC校验字节数据 /// </summary> /// <param name="bankData"> ...

  5. C#中List<T>对象的深度拷贝问题

    一.List<T>对象中的T是值类型的情况(int 类型等) 对于值类型的List直接用以下方法就可以复制: List<T> oldList = new List<T&g ...

  6. Snort - manual 笔记(二)

    1.5 Packet Acquisition Snort 2.9 引入 DAQ 代替直接调用 libpcap . 有两种网卡特性会影响 Snort : "Large Receive Offl ...

  7. English Training Material - 05

    Could I leave a message? Language Checklist Telephoning (1) Introducing yourself Good morning, Arist ...

  8. 使用RDCMan管理SharePoint虚拟机的重复要求验证的问题

    首先,这个软件可以从这里下载: Remote Desktop Connection Manager 同类型的软件还有很多,我没有很多复杂功能的要求,就选择了这款微软官方的,虽然很久都没有更新过了. 为 ...

  9. R语言学习笔记:向量

    向量是R语言最基本的数据类型. 单个数值(标量)其实没有单独的数据类型,它只不过是只有一个元素的向量. x <- c(1, 2, 4, 9) x <- c(x[1:3], 88, x[4] ...

  10. DownloadManager 的使用

    一.基本概念    1.DownloadManager是Android 2.3A (API level 9) 引入的,基于http协议,用于处理长时间下载. 2.DownloadManager对于断点 ...