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,问两条路线是否会有交点,若有则输出交点个数,否出输 ...
随机推荐
- JS类定义方式
// 方法1 对象直接量 var obj1 = { v1 : "", get_v1 : function() { return this.v1; }, set_v1 : funct ...
- Screwturn搭建企业内部wiki
企业内部WIKI搭建 本文所使用的是Screwturn 基于asp.net webform和Sql server的. 仅仅要把本文资源下载下来,直接用IIS部署,然后更改web.config的conn ...
- 使用psftp.exe
使用psftp.exe 点击打开psftp.exe,出现如下图的命令窗口.
- Swift难点-继承中的构造规则实例具体解释
关于继承中的构造规则是一个难点. 假设有问题,请留言问我. 我的Swift新手教程专栏 http://blog.csdn.net/column/details/swfitexperience.html ...
- 搭建solr单机版
solr单机版的搭建 一.solr单机版的搭建 1.运行环境 solr 需要运行在一个Servlet容器中,Solr4.10.3要求jdk使用1.7以上,Solr默认提供Jetty(ja),本教va写 ...
- windows phone (26) ApplicationBar应用程序栏
原文:windows phone (26) ApplicationBar应用程序栏 在应用程序中,如果需要几个按钮或者菜单来执行一些普通的命令,就应该考虑使用ApplicationBar,因为silv ...
- java线程学生进实训室
Instructor: Dr. Simina FlutureCSCI 34 CSCI 34 CSCI 34CSCI 34 0 Summer 201 ummer 201 ummer 201ummer 2 ...
- bootstrap在 刷新页面,tab选择页面不会改变。
您可以直接复制代码 注意在同级别文件夹中引用 相应js 和 css. 实现tab影响 关键看bootstrap的 data-toggle= tab <html lang="en&quo ...
- update和saveOrUpdate具体解释
在Hibernate中,最核心的概念就是对PO的状态管理.一个PO有三种状态: 1.未被持久化的VO 此时就是一个内存对象VO,由JVM管理生命周期 2.已被持久化的PO,而且在Session生 ...
- JAVA 数据权限设计
数据权限设计 前言 在各种系统中.要保证数据对象的安全性以及易操作性,使企业的各业务部门.职能部门可以方便并且高效的协同工作,那么一个好的数据权限管理设计就成为一个关键的问题.尽管企业中各个单元的工作 ...