POJ 3414 Pots 记录路径的广搜
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是容量目标。 能够有三种操作 充满随意一瓶。倒空随意一瓶,将随意一瓶倒入还有一瓶(能剩下但不能溢出);求随意一瓶的体积
达到目标体积所须要的最小操作数。并依此输出该操作。
代码:#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int m,n,k;
int vis[105][105];
char opr[20][20]= {" " , "FILL(1)" , "FILL(2)" , "DROP(1)" , "DROP(2)" , "POUR(1,2)" , "POUR(2,1)" }; //共6种操作
struct node
{
int x,y,step;
int w[200]; //用来记录路径的数组数组
};
void bfs( )
{
int i,j;
int kx,ky;
memset(vis,0,sizeof(vis));
queue<node>q;
node now,next;
now.x=0,now.y=0,now.step=0;
q.push(now);
vis[0][0]=1;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==k||now.y==k)
{
cout<<now.step<<endl;
for( i=1; i<=now.step; i++)
{
cout<<opr[now.w[i]]<<endl;
}
return;
}
for(i=0; i<6; i++) // 共六种操作
{
if(i==0)
{
next.y=now.y;
next.x=m;
now.w[now.step+1]=1; //随时更新
}
else if(i==1)
{
next.x=now.x;
next.y=n;
now.w[now.step+1]=2;
}
else if(i==2)
{
next.y=now.y;
next.x=0;
now.w[now.step+1]=3;
}
else if(i==3)
{
next.x=now.x;
next.y=0;
now.w[now.step+1]=4;
}
else if(i==4)
{
if(n-now.y>now.x) //每种pour 应有两种情况
{
next.x=0;
next.y=now.x+now.y;
}
else
{
next.y=n;
next.x=now.x-n+now.y;
}
now.w[now.step+1]=5;
}
else if(i==5)
{
if(m-now.x>now.y)
{
next.y=0;
next.x=now.x+now.y;
}
else
{
next.x=m;
next.y=now.y-m+now.x;
}
now.w[now.step+1]=6;
}
if(vis[next.x][next.y]==1)
continue;
vis[next.x][next.y]=1;
next.step=now.step+1;
for(j=1; j<=next.step; j++) //记录之前的行动
{
next.w[j]=now.w[j];
}
q.push(next);
}
}
cout<<"impossible"<<endl; //不要忘了这个情况
return;
}
int main()
{
int i;
while(cin>>m>>n>>k)
{
bfs();
}
return 0;
}通过自己对队列的理解成功写出了代码 还是挺开心的。
。。
POJ 3414 Pots 记录路径的广搜的更多相关文章
- POJ 3984 迷宫问题 记录路径的广搜
主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
- 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 (输出路径的广搜)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- (简单) 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+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
随机推荐
- Linux - 输入输出流程序 代码(C)
输入输出流程序 代码(C) 本文地址:http://blog.csdn.net/caroline_wendy 使用输入输出流,控制文件流. STDIN_FILENO,STDOUT_FILENO代表标准 ...
- 修改OpenSSL默认编译出的动态库文件名称
在 Windows 平台上调用动态链接库 dll 文件时,有两种方式:a) 隐式的加载时链接:使用 *.lib (导入库)文件,在 IDE 的链接器相关设置中加入导入库 lib 文件的名称,或在程序中 ...
- Spring MVC 学习笔记 json格式的输入和输出
Spring mvc处理json需要使用jackson的类库,因此为支持json格式的输入输出需要先修改pom.xml增加jackson包的引用 <!-- json --> <dep ...
- Jenkins快速搭建持续集成
基于Jenkins快速搭建持续集成环境 Jenkins+tortoisesvn+MSBuild做到持续集成 附Jenkins的使用:http://www.infoq.com/cn/articles/M ...
- 在Ubuntu 12.04下编译qtiplot
不在windows下,再加上不想用盗版,所以需要一个origin的替代品——qtiplot.虽然我非常抵抗用这种不停点来点去的软件,用R的ggplot2画图多好啊,高效.优雅.漂亮,但是终抵不过老板一 ...
- perl 循环类选择器 ,爬取内容
jrhmpt01:/root/lwp/0526# cat 0526.txt <div class="TXD_sy_title"><span class=" ...
- socket为send和recv设置超时时间
linux和windows下用setsockopt设置SO_SNDTIMEO,SO_RCVTIMEO的参数的一点区别 UDP的socket在某些情况:如对方关闭时,本地可能sendto不出去数据,然后 ...
- Win7安装vs2010失败
提示: ------------------------------------------------------------------------------------------------ ...
- C 语言资源大全中文版
C 语言资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-c 是 koz.ross 发起维护的 C 语言资源列表,内容包括了: ...
- c#中,DataTable 过滤重复行
虽然网上有很多DataTable过滤重复行的方法,但是本菜还是认为自己写的这个方法最靠谱,这里的参数是传递的DataTable值,返回的是一个已经过滤相同字段StuId,ExamNum的DataTab ...