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. asp.net Form 认证【转】

    第一部分 如何运用 Form 表单认证 一.        新建一个测试项目 为了更好说明,有必要新建一个测试项目(暂且为“FormTest”吧),包含三张页面足矣(Default.aspx.Logi ...

  2. WordPress更改固定链接出现404的解决方案

    很多站长在玩WordPress的时候,可能会碰到一个问题,就是想把WordPress伪静态,在后台设置好固定链接之后,就会出现文章页面或者所有的页面都出现404错误.解决方法如下: 1,.htacce ...

  3. win7提示“ipconfig不是内部或外部命令”

    进入windows环境变量设置->系统变量,找到path,添加C:\Windows\SysWOW64,或者c:\windows\system32

  4. PopupMenu消失(Dismiss)抓住

    一.在class在implements PopupMenu该监听器类:OnDismissListener, 然后在类需要实现一个方法未实现:onDismiss(PopupMenu menu). 二.参 ...

  5. OSChina底层数据库操作的类(QueryHelper)源代码

    OSChina 使用的是 dbutils 这个JDBC的封装类库来进行数据库操作. 而 QueryHelper 则是在 dbutils 的基础上进行一级简单的封装,提供一些经常使用的数据库操作方法和对 ...

  6. lambda left join .DefaultIfEmpty

    我们知道lambda表达式在Linq to sql 和 Entity framework 中使用join函数可以实现inner join,那么怎么才能在lambda表达式中实现left join呢?秘 ...

  7. log4j 实例 , 浅析

    一.新建log4j.propperties,放在工程的src目录下. #fileAppender log4j.rootCategory = DEBUG,file,consoleAppender log ...

  8. EasyUI基础searchbox&amp;progressbar(搜索框,进度条)

    easyui学习的基本组成部分(八个部分)硕果仅存searchbox和pargressbar.tooltip该,有一点兴奋.本文将偏向searchbox和pargressbar做一个探讨.鉴于双方的内 ...

  9. Test SRM Level Three: LargestCircle, Brute Force

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3005&rd=5858 思路: 如果直接用Brute F ...

  10. group by和order by的错误

    select  u.Col_Name from hs_user u left join ( select tuid,count(*) as 'col_sumtopic' from BBS_Topic ...