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. maven-dependency-plugin插件的使用

    maven-dependency-plugin插件的使用   maven-dependency-plugin是 处理与依赖相关的插件.它有很多可用的goal,大部分是和依赖构建.分析和解决相关的goa ...

  2. Erlang高阶函数

    对于函数式语言来说,函数也想普通的数据类型一样无处不在.函数即可以当成参数进行传递,也可以当成函数的返回值.当我第一次学习函数式编程的时候,我被这样的写法弄的头昏脑涨.下面我举例说明下(例子摘录自Le ...

  3. 微信公共平台开发5 .net

    每次在于微信交互时,都要用到access_token,但是这个值限制的是有时间的,但是access_token,在以后的高级功能里面会经常用到,所以这里不得不这里对前面所讲解的access_token ...

  4. JS写返回上一级

    应产品需求,自己的网站上要有返回上一级的需求,几经周折,做个小总结. (1): $("XX").on("click",function(){      wind ...

  5. Bootstrap-用ICheck插件给CheckBox换新装

    直接来吧! 下面是添加上复选框以后的树形菜单效果: 这样看起来有种驴唇不对马嘴的感觉. 所以就要想办法给这些复选框添加1些样式,让全部界面看起来搭配1些. 通过查询得知,有个叫ICheck的第3方Bo ...

  6. Atitit.jsou html转换纯文本 java c# php

    Atitit.jsou html转换纯文本 java c# php 1. 原理<p> <h> <li><div> 等lable转换为回车1 2. 调用2 ...

  7. object-c中的类目,延展,协议

    协议 协议只有方法的声明(类似于其他编程语言的接口)   协议相当于大家都所遵循的 关键字 @protocol 协议名 <所遵循的协议> 默认NSObject   @end     @pr ...

  8. JDK8 API文档(下载)

    DK API文档 java SE 8 API文档: http://www.oracle.com/technetwork/java/javase/documentation/jdk8-doc-downl ...

  9. 朝花夕拾-android 获取当前手机的内存卡状态和网络连接状态

    序言: 人的一生是一个选择的过程. 如果脚下只有一条路,只要一往无前即可,不用担心走错.即使是错也别无它法.然而人是不安分的,况且安于独木桥的行走,其目的地由于没有蜿蜒曲折去遮挡行路人的视线,一往无前 ...

  10. IOS开发--常用工具类收集整理(Objective-C)(持续更新)

    前言:整理和收集了IOS项目开发常用的工具类,最后也给出了源码下载链接. 这些可复用的工具,一定会给你实际项目开发工作锦上添花,会给你带来大大的工作效率. 重复造轮子的事情,除却自我多练习编码之外,就 ...