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

有a,b两个瓶,得到体积为c的液体,六种操作

装满1。装满2,倒掉1。倒掉2,由1倒到2,有2倒到1.

记录一下路径。也就是有谁到达的当前状态

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
int a , b , pre ;
char s[20] ;
}p[1000000] , x ;
int flag[20000] , low , top , k ;
char str[6][20] = { "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)" };
int pre[1000000] ;
int main()
{
int a , b , c , sum , i , j ;
k = 0 ;
memset(flag,0,sizeof(flag));
scanf("%d %d %d", &a, &b, &c);
flag[a*100+b] = 1 ;
low = top = 0 ;
p[top].a = 0 ; p[top].b = 0 ;
p[top++].pre = -1 ;
while(low < top)
{
if( p[low].a == c || p[low].b == c )
break;
if( p[low].a < a )
{
p[top].a = a ; p[top].b = p[low].b ;
if( !flag[ p[top].a*100+p[top].b ] )
{
flag[ p[top].a*100+p[top].b ] = 1 ;
strcpy(p[top].s,str[0]);
p[top++].pre = low ;
}
}
if( p[low].b < b )
{
p[top].a = p[low].a ; p[top].b = b ;
if( !flag[ p[top].a*100+p[top].b ] )
{
flag[ p[top].a*100+p[top].b ] = 1 ;
strcpy(p[top].s,str[1]);
p[top++].pre = low ;
}
}
if( p[low].a > 0 )
{
p[top].a = 0 ; p[top].b = p[low].b ;
if( !flag[ p[top].a*100+p[top].b ] )
{
flag[ p[top].a*100+p[top].b ] = 1 ;
strcpy(p[top].s,str[2]);
p[top++].pre = low ;
}
}
if( p[low].b > 0 )
{
p[top].a = p[low].a ; p[top].b = 0 ;
if( !flag[ p[top].a*100+p[top].b ] )
{
flag[ p[top].a*100+p[top].b ] = 1 ;
strcpy(p[top].s,str[3]);
p[top++].pre = low ;
}
}
if( p[low].a > 0 && p[low].b < b )
{
sum = p[low].a + p[low].b ;
if( sum <= b )
{
p[top].a = 0 ; p[top].b = sum ;
}
else
{
p[top].a = sum - b ; p[top].b = b ;
}
if( !flag[ p[top].a*100+p[top].b ] )
{
flag[ p[top].a*100+p[top].b ] = 1 ;
strcpy(p[top].s,str[4]);
p[top++].pre = low ;
}
}
if( p[low].a < a && p[low].b > 0 )
{
sum = p[low].a + p[low].b ;
if( sum <= a )
{
p[top].a = sum ; p[top].b = 0 ;
}
else
{
p[top].a = a ; p[top].b = sum - a ;
}
if( !flag[ p[top].a*100+p[top].b ] )
{
flag[ p[top].a*100+p[top].b ] = 1 ;
strcpy(p[top].s,str[5]);
p[top++].pre = low ;
}
}
low++ ;
}
if( low == top )
printf("impossible\n");
else
{
j = 0 ;
for(i = low ; i != 0 ; i = p[i].pre)
{
pre[j++] = i ;
}
printf("%d\n", j);
for(j -= 1 ; j >= 0 ; j--)
printf("%s\n", p[ pre[j] ].s);
}
return 0;
}

版权声明:转载请注明出处:http://blog.csdn.net/winddreams

poj3414--Pots(bfs,记录路径)的更多相关文章

  1. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  2. Codeforces-A. Shortest path of the king(简单bfs记录路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  3. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  4. (简单) POJ 3414 Pots,BFS+记录路径。

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

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

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

  6. poj 3414 Pots(bfs+输出路径)

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

  7. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  8. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

  9. 迷宫问题(bfs+记录路径)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...

  10. sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )

    路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...

随机推荐

  1. 学习 easyui 之二:jQuery 的 ready 函数和 easyloader 的加载回调函数

    Ready 事件不一定 ready 使用 easyloader 的时候,必须要注意到脚本的加载时机问题,easyloader 会异步加载模块,所以,你使用的模块不一定已经加载了.比如下面的代码. &l ...

  2. 用python调用R做数据分析-准备工作

    0.R的介绍 R是自由软件,不带不论什么担保.在某些条件下你能够将其自由散布,用'license()'或'licence()'来看散布的具体条件. R是个合作计划.有很多人为之做出了贡献,用'cont ...

  3. robot framework 使用四:分层设计和截图以及注意事项

    再说一下眼下的主要环境信息和版本号: 操作系统:win7 64位 python版本号:2.7.6 RIDE版本号:1.2.3 selenium2library:1.5.0 selenium:2.40. ...

  4. SQL SERVER IN参数化处理

    方法一. CREATE TABLE [dbo].[Users] ( Id INTEGER IDENTITY(1, 1) PRIMARY KEY , Name NVARCHAR(50) NOT NULL ...

  5. IOS开发-通知与消息机制

    在多数移动应用中不论什么时候都仅仅能有一个应用程序处于活跃状态.假设其它应用此刻发生了一些用户感兴趣的那么通过通知机制就能够告诉用户此时发生的事情. iOS中通知机制又叫消息机制,其包含两类:一类是本 ...

  6. friend keyword 对于模板 并不只不过友元!!!

    friend是C++中封装的漏网之鱼. C++中的friend同意其它的类或者是函数訪问本类的不论什么成员.甚至是private成员,仅仅要该类声明其为友元. 但是,在有些情况下,并非同意外界訪问类的 ...

  7. Android自己的自动化测试Monkeyrunner和用法示例

    眼下android SDK在配有现成的测试工具monkey 和 monkeyrunner两. 也许我们不看一样的兄弟名字.但事实是完全跑了两个完全不同的工具.在测试的不同区域的应用程序.总体,monk ...

  8. VMware vSphere 服务器虚拟化之十八桌面虚拟化之安装View Composer服务器

                        VMware vSphere 服务器虚拟化之十八桌面虚拟化之安装View Composer服务器      View Compose服务可安装在管理虚拟机的vC ...

  9. 深度分析 Java 的 ClassLoader 机制(源码级别)(转)

    写在前面:Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘读取到内存中,JVM在加载类的时候,都是通过ClassLoa ...

  10. Linux shell用法和技巧(转)

    使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和l技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务.于是,我开始在一个文本文件里记录这些用法,并 ...