广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 13547 | Accepted: 5718 | 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)
/*和之前的那道倒水题目十分相似,唯一的难点就是输出倒水的方式,我们可以记录每一个搜到的状态的前一状态在队列中的编号和,该状态与前一状态的关系,再递归输出结果即可*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 10010
int head=-,tail=-;
int a,b,c;
bool flag[][]={};
struct node{
int a1,b1,pre,dis,relat;
}que[N];
int bfs()
{
while(head<tail)
{
++head;
if(que[head].a1==c||que[head].b1==c)
{
return head;
}
if(!flag[que[head].a1][])
{
flag[que[head].a1][]=true;
node nex;
nex.a1=que[head].a1;nex.b1=;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(!flag[][que[head].b1])
{
flag[][que[head].b1]=true;
node nex;
nex.a1=;nex.b1=que[head].b1;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(!flag[que[head].a1][b])
{
flag[que[head].a1][b]=true;
node nex;
nex.a1=que[head].a1;nex.b1=b;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(!flag[a][que[head].b1])
{
flag[a][que[head].b1]=true;
node nex;
nex.a1=a;nex.b1=que[head].b1;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].a1>=(b-que[head].b1)&&!flag[que[head].a1-(b-que[head].b1)][b])
{
flag[que[head].a1-(b-que[head].b1)][b]=true;
node nex;
nex.a1=que[head].a1-(b-que[head].b1);nex.b1=b;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].a1<(b-que[head].b1)&&!flag[][que[head].a1+que[head].b1])
{
flag[][que[head].a1+que[head].b1]=true;
node nex;
nex.a1=;nex.b1=que[head].a1+que[head].b1;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].b1>=(a-que[head].a1)&&!flag[a][que[head].b1-(a-que[head].a1)])
{
flag[a][que[head].b1-(a-que[head].a1)]=true;
node nex;
nex.a1=a;nex.b1=que[head].b1-(a-que[head].a1);
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].b1<(a-que[head].a1)&&!flag[que[head].a1+que[head].b1][])
{
flag[que[head].a1+que[head].b1][]=true;
node nex;
nex.a1=que[head].a1+que[head].b1;nex.b1=;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
}
return -;
}
void out(int temp)
{
if(que[que[temp].pre].pre!=-)
out(que[temp].pre);
if(que[temp].relat==)
printf("FILL(1)\n");
else if(que[temp].relat==)
printf("FILL(2)\n");
else if(que[temp].relat==)
printf("DROP(1)\n");
else if(que[temp].relat==)
printf("DROP(2)\n");
else if(que[temp].relat==)
printf("POUR(1,2)\n");
else printf("POUR(2,1)\n");
}
/*
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
*/
int main()
{
scanf("%d%d%d",&a,&b,&c);
++tail;
que[tail].a1=;que[tail].b1=;
que[tail].dis=;que[tail].pre=-;
flag[][]=true;
int temp=bfs();
if(temp==-)
printf("impossible\n");
else {
printf("%d\n",que[head].dis);
out(temp);
}
return ;
}
广搜+输出路径 POJ 3414 Pots的更多相关文章
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- POJ 3414 Pots
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- PTA 7-33 地下迷宫探索(深搜输出路径)
地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活的同时,真心钦佩他们的聪明才智. ...
随机推荐
- 解决My eclipse 工程发布时端口占用问题
如果运行后如图的错,需要进行如下操作来解决: a:打开cmd,输入netstat -ano 找到本地地址为8080的最后一项的数字,这个数字就是端口号. b:再输入taskkill /t /pid 端 ...
- Linux Shell系列教程之(十四) Shell Select教程
本文是Linux Shell系列教程的第(十四)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上一篇文章:Linux Shell系列教程之(十三)Shell分支语句case ...
- Play 内置模板标签(1.2.3版本)http://www.anool.net/?p=617
a标签: 用来插入一个连接到控制器方法的html link.如下: #{a @Application.logout()}Disconnect#{/a}模板内容被解析后变成: <a href=&q ...
- 任意类型转换为IntPtr
之前,将数组.结构体等转换为IntPtr使用的是Marshal.Copy().Marshal.StructureToPtr(),但是有个问题自定义的结构体数组没法这样转化,一般网上给出的解决方法就是通 ...
- touch触摸事件
事件对象 事件对象是用来记录一些事件发生时的相关信息的对象.事件对象只有事件发生时才会产生,并且只能是事件处理函数内部访问,在所有事件处理函数运行结束后,事件对象就被销毁! W3C DOM把事件对象作 ...
- WebActivatorEx 注入时的使用
WebActivator类库提供了3种功能,允许分别在Application_Start初始化之前,之后以及ShutDown的时候,分别执行指定的代码,并且允许多次指定.示例如下: [assembly ...
- SCOM Visio监控 与sharepoint 2010 整合
激活sharepoint 2010的企业版网站功能 在sharepoint 前端服务器安装:OpsMgrDataModule.msi,安装好后可以看到如下东东: 在Sharepoint前端服务器中启动 ...
- sudo gem install cocoapods 没反应问题
1. 尝试更新 sudo gem update --system 2. 查看安装详细 sudo gem install cocoapods -V 3.详细使用有个链接 http://blog.csdn ...
- 据说是百度ios面试题
百度面试题: 一面:知识点 Objective C runtime library: Objective C的对象模型,Block的底层实现结构,消息发送,消息转发,内存管理 CoreData : ...
- android内存泄露调试,Heap,MAT
三.内存监测工具 DDMS --> Heap 无论怎么小心,想完全避免bad code是不可能的,此时就需要一些工具来帮助我们检查代码中是否存在会造成内存泄漏的地方.Android tools中 ...