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. IIS理解

    WEB开发基础 1IIS原理 IIS的本质其实就是一个sorket的服务器,浏览器就是一个sorket的客户端,浏览器发送请求信息给IIS,IIS返回信息给浏览器显示,就这么简单. 1http.sys ...

  2. Gson解析的小例子

    最近解析些复杂的节点数据解析,用安卓自带的json解析比较麻烦所以只能用Gson解析,所以从网上下了点demo来看看 http://blog.csdn.net/tkwxty/article/detai ...

  3. JPA(7) spring-data-jpa

    对于不是使用spring管理的项目,我们就自己创建对象使用:大概的思路就是①创建dao接口②实现该接口,并且编写逻辑: Dao: public interface StudentDao { publi ...

  4. JMS学习(二)- JMS Message Model 组成介绍及消息头详解

    一.前言 从本文起依次详细介绍JMS中的一些重要的概念,主要参考了官方的JMS1.1的文档,该文档很老了,是02年的,那年,JAVA还没有被Oracle收购..本文主要介绍Message及其相关概念, ...

  5. [PHP] PHP请求Socket接口测试

    使用php读取socket接口的数据,通过php传递请求方法和请求参数,得到返回结果 PHP文件: <?php class Test{ const IP='127.0.0.1'; const p ...

  6. php中的引用类型和值类型

    PHP中的四种简单类型和复杂类型array都是值类型.同类型间赋值传递的是值,即创建一个副本给新变量. 例如: $int1 = 123; $int2 = $int1;//直接传递的是值,只是做了一个叫 ...

  7. 细说Mysql四种安装方法及自动化部署

    一.简介 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据存储在文件中,但是 ...

  8. SharePoint 2013 隐藏部分Ribbon菜单

    SharePoint的使用中,因为用户经常不愿意看到那些不经常使用的操作,我们经常需要定制Ribbon菜单, 更多时候不是隐藏所有,而是隐藏掉我们不需要的那些:下面,我们一列表为例,简单介绍下如何部分 ...

  9. 参加2013中国大数据技术大会(BDTC2013)

    2013年12月5日-6日参加了为期两天的2013中国大数据技术大会(Big Data Technology Conference, BDTC2013),本期会议主题是:“应用驱动的架构与技术 ”.大 ...

  10. Android实战--短信发送器

    首先设计界面 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...