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,问两条路线是否会有交点,若有则输出交点个数,否出输 ...
随机推荐
- android com.handmark.pulltorefresh 使用技巧
近期使用android com.handmark.pulltorefresh 遇到一些小问题.如今总结一些: 集体使用教程见: http://blog.csdn.net/harvic880925/ar ...
- Windows Phone开发(2):竖立自信,初试锋茫
原文:Windows Phone开发(2):竖立自信,初试锋茫 上一篇文章中,我们聊了一些"大炮"话题,从这篇文章开始,我们一起来学习WP开发吧. 一.我们有哪些装备. 安装完VS ...
- 正确Linux新手很实用20命令
//正确Linux新手很实用20命令 //slwang 2014.4.19 1, ls list directory contents 内容 ls -l //以详情模式(long li ...
- JAVA学习课第二十八届(多线程(七))- 停止-threaded多-threaded面试题
主密钥 /* * wait 和 sleep 差别? * 1.wait能够指定时间也能够不指定 * sleep必须指定时间 * 2.在同步中,对CPU的运行权和锁的处理不同 * wait释放运 ...
- android 中国通信乱码问题
1.要解决中文乱码问题.首先得了解什么是字符编码 计算机要处理各种字符,就须要将字符和二进制内码相应起来,这样的相应关系就是字符编码. 要制定字符编码首先要确定字符集,并将 字符集内的字符排序.然后和 ...
- js淡入淡出
示例: js淡入淡出 原理:更改css不透明数值 知识点: css不透明 filter:alpha(opacity:30); opacity:0.3;} 小技巧: 小于临界值,做加速 大于临界值,做减 ...
- HDU4866 Shooting (要持久段树)
意甲冠军: 给你一些并行x行轴.总是询问坐标x的顶部之前,k一个段高度,.标题是必须在线. 思路: 首先要会可持久化线段树(又称主席树和函数式线段树).不会的能够去做下POJ 2104. 把全部线段高 ...
- Android Application Thread CPU GC Operatiing and OOM Question 0603-随手笔记
面前app当完成测试,没问题,以完成整个老龄化阶段包含数据收发器,关键在 adb shell top -m 5 我发现我的 app pid 占用 CPU是最多的,事实上我想说写一个app是不难,你 ...
- Unity3D 表对象分类中的实现(C#)
// Sort by distance in descending order private void SortTargetsByDistance () { targets.Sort(delegat ...
- HDU2516-取石子游戏
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...