poj3414--Pots(bfs,记录路径)
| 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:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- 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,记录路径)的更多相关文章
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- 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 ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- (简单) POJ 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- poj 3414 Pots(bfs+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- 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 ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- 迷宫问题(bfs+记录路径)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...
- sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )
路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...
随机推荐
- 理解Git的工作流程(转)
英文原文:Understanding the Git Workflow 如果你不理解Git的设计动机,那你就会处处碰壁.知道足够多的命令和参数后,你就会强行让Git按你想的来工作,而不是按Git自己的 ...
- java正則表達式的坑
java中正則表達式比較有意思,这里列举几个常见的坑 1.[]符号,中括号表示当中的数据都是或的关系 假设[\\w+]是匹配条件 abc能否够匹配的到呢? 首先\\w(注意这里没有中括号)表示a-z ...
- 《Javascript高级程序设计》读书笔记之继承
1.原型链继承 让构造函数的原型对象等于另一个类型的实例,利用原型让一个引用类型继承另一个引用类型的属性和方法 function SuperType() { this.property=true; } ...
- Android 下拉刷新上拉载入效果功能
应用场景: 在App开发中,对于信息的获取与演示.不可能所有将其获取与演示,为了在用户使用中,给予用户以友好.方便的用户体验,以滑动.下拉的效果动态载入数据的要求就会出现. 为此.该效果功能就须要应用 ...
- 用python输出汉字字库
问题1:如果我们知道汉字编码范围是0x4E00到0x9FA5,怎么从十六进制的编码转成人类可读的字呢? 问题2:怎么把unicode编码的字写入文件呢,假设直接用open()的话,会提示Unicode ...
- iframe属性參数
iframe属性參数 当点击一个子页面的链接时, 怎样将还有一个子页面嵌入到当前iframe中 仅仅要给这个iframe命名就能够了. <iframe width=420 height=330 ...
- Cocos2d-x 3.2 Lua演示样例 XMLHttpRequestTest(Http网络请求)
Cocos2d-x 3.2 Lua演示样例 XMLHttpRequestTest(Http网络请求) 本篇博客介绍Cocos2d-x 3.2Lua演示样例中的XMLHttpRequestTes ...
- swift-辞典NSDictionary定义,变化的关键,删/加入关键
// Playground - noun: a place where people can play import UIKit //--------------------------------- ...
- 打开 chm 帮助文件显示空白及解决方法
有个很奇葩的解决方法:把 chm 文件用压缩软件压缩,然后用压缩软打开此压缩包,直接双击压缩包里面的 chm 文件 这虽然解决了问题,但是这不科学…… 分析:直接打开压缩包里面的文件,压缩包的文件是临 ...
- MyEclipse每次修改js有另一种部署
今天在做时间格式显示这块内容的时候.我改动了这个js函数,同一时候凝视的掉了其它的一些替换的方法,可是在浏览器中执行发现效果一点也没有改变.于是debug js,发现浏览器中识别的还是之前未改动的js ...