Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13545   Accepted: 5717   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)

Source

Northeastern Europe 2002, Western Subregion

Poj3414

题目大意: 有二个水壶,对水壶有三种操作,1)FILL(i),将i水壶的水填满,2)DROP(i),将水壶i中的水全部倒掉,3)POUR(i,j)将水壶i中的水倒到水壶j中,若水壶 j 满了,则 i 剩下的就不倒了,问进行多少步操作,并且怎么操作,输出操作的步骤,两个水壶中的水可以达到C这个水量。如果不可能则输出impossible。初始时两个水壶是空的,没有水。

#include<stdio.h>
#include<string.h> const int maxn = ;
int vis[maxn][maxn]; //标记状态是否入队过
int a,b,c; //容器大小
int step; //最终的步数
int flag; //纪录是否能够成功 /* 状态纪录 */
struct Status{
int k1,k2; //当前水的状态
int op; //当前操作
int step; //纪录步数
int pre; //纪录前一步的下标
}q[maxn*maxn];
int id[maxn*maxn]; //纪录最终操作在队列中的编号
int lastIndex; //最后一个的编号 void bfs()
{
Status now, next; int head, tail;
head = tail = ; q[tail].k1 = ; q[tail].k2 = ;
q[tail].op = ; q[tail].step = ; q[tail].pre = ; tail++; memset(vis,,sizeof(vis));
vis[][] = ; //标记初始状态已入队 while(head < tail) //当队列非空
{
now = q[head]; //取出队首
head++; //弹出队首 if(now.k1 == c || now.k2 == c) //应该不会存在这样的情况, c=0
{
flag = ;
step = now.step;
lastIndex = head-; //纪录最后一步的编号
} for(int i = ; i <= ; i++) //分别遍历 6 种情况
{
if(i == ) //fill(1)
{
next.k1 = a;
next.k2 = now.k2;
}
else if(i == ) //fill(2)
{
next.k1 = now.k1;
next.k2 = b;
}
else if(i == ) //drop(1)
{
next.k1 = ;
next.k2 = now.k2;
}
else if(i == ) // drop(2);
{
next.k1 = now.k1;
next.k2 = ;
}
else if(i == ) //pour(1,2)
{
if(now.k1+now.k2 <= b) //如果不能够装满 b
{
next.k1 = ;
next.k2 = now.k1+now.k2;
}
else //如果能够装满 b
{
next.k1 = now.k1+now.k2-b;
next.k2 = b;
}
}
else if(i == ) // pour(2,1)
{
if(now.k1+now.k2 <= a) //如果不能够装满 a
{
next.k1 = now.k1+now.k2;
next.k2 = ;
}
else //如果能够装满 b
{
next.k1 = a;
next.k2 = now.k1+now.k2-a;
}
} next.op = i; //纪录操作
if(!vis[next.k1][next.k2]) //如果当前状态没有入队过
{
vis[next.k1][next.k2] = ; //标记当前状态入队
next.step = now.step+; //步数 +1
next.pre = head-; //纪录前一步的编号 //q.push(next);
//q[tail] = next; 加入队尾
q[tail].k1 = next.k1; q[tail].k2 = next.k2;
q[tail].op = next.op; q[tail].step = next.step; q[tail].pre = next.pre;
tail++; //队尾延长 if(next.k1 == c || next.k2 == c) //如果达到目标状态
{
flag = ; //标记成功
step = next.step; //纪录总步骤数
lastIndex = tail-; //纪录最后一步在模拟数组中的编号
return;
}
}
}
} } int main()
{
while(scanf("%d%d%d", &a,&b,&c) != EOF)
{
flag = ; //初始化不能成功
step = ; bfs();
if(flag)
{
printf("%d\n", step); id[step] = lastIndex; //最后一步在模拟数组中的编号
for(int i = step-; i >= ; i--)
{
id[i] = q[id[i+]].pre; //向前找前一步骤在模拟数组中的编号
} for(int i = ; i <= step; i++)
{
if(q[id[i]].op == )
printf("FILL(1)\n"); else if(q[id[i]].op == )
printf("FILL(2)\n"); else if(q[id[i]].op == )
printf("DROP(1)\n"); else if(q[id[i]].op == )
printf("DROP(2)\n"); else if(q[id[i]].op == )
printf("POUR(1,2)\n"); else if(q[id[i]].op == )
printf("POUR(2,1)\n");
}
}
else printf("impossible\n");
}
return ;
}

poj3414的更多相关文章

  1. POJ3414 Pots BFS搜素

    题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...

  2. poj3414(bfs)

    题目链接:http://poj.org/problem?id=3414 题意:给你两个容器 A  B 问是否能够经过有限的步骤倒水,得到容量为 C 的水,输出最小的步数,同时输出每一步的操作.如果不能 ...

  3. Poj3414广泛搜索

    <span style="color:#330099;">/* D - D Time Limit:1000MS Memory Limit:65536KB 64bit I ...

  4. POJ-3414.Pots.(BFS + 路径打印)

    这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...

  5. POJ3414—Pots(bfs加回溯)

    http://poj.org/problem?id=3414                                       Pots Time Limit: 1000MS   Memor ...

  6. POJ-3414 Pots (BFS)

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

  7. 快速切题 poj3414 Pots

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10042   Accepted: 4221   Special J ...

  8. POJ3414(KB1-H BFS)

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

  9. poj3414 Pots(BFS)

    题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...

随机推荐

  1. Python-所有特殊方法、魔术方法、钩子

    C.__init__(self[, arg1, ...]) 构造器(带一些可选的参数) C.__new__(self[, arg1, ...]) 构造器(带一些可选的参数)通常用在设置不变数据类型的子 ...

  2. Request to https://bower.herokuapp.com failed with 502

    bower 版本过低,需要升级为最新bower版本, 如果升级版本后依然无法使用,更改.bowerrc配置,如下所示 { "directory": "bower_comp ...

  3. 《C++编程思想》(第二版)第3章 C++中的C(笔记、习题及答案)(二)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  4. Mac 全局变量 ~/.bash_profile 文件不存在的问题

    不存在就新建呗~ $ cd ~/ $ touch .bash_profile $ open -e .bash_profile 然后输入以下内容 # set color的部分是配置iterm2的字体颜色 ...

  5. AlarmManager研究

    1.概述 在Android系统中,闹钟和唤醒功能都是由Alarm Manager Service控制并管理的.我们所熟悉的RTC闹钟以及定时器都和它有莫大的关系.为了便于称呼,我常常也把这个servi ...

  6. cp/scp命令详解

    cp:拷贝命令 用法: cp [参数] source dest cp [参数] source ... directory 说明:将一个档案拷贝至另一个档案,或数个档案拷贝到另一目录 参数: -a 尽可 ...

  7. vs2010 MSDN文档安装方法

    vs2010的MSDN是不能独立安装,必须安装VS2010后才能安装. 安装方法: 1.vs2010的ISO光盘文件中,里面会有个ProductDocumentation文件夹,其实这个就是安装MSD ...

  8. WebRTC编译具体介绍

    WebRTC编译具体介绍--记录+转载 原文地址:http://blog.csdn.net/temotemo/article/details/7056581 WebRTC编译 本人环境: 操作系统:X ...

  9. java.lang.NoSuchMethodError: org.apache.spark.util.ThreadUtils$.newDae

    -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1. ...

  10. Python中集合类型(set)学习小结

    set 是一个无序的元素集合,支持并.交.差及对称差等数学运算, 但由于 set 不记录元素位置,因此不支持索引.分片等类序列的操作. 初始化 复制代码代码如下: s0 = set()d0 = {}s ...