POJ-3414 Pots (BFS)
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) 题目分析:打眼一看就是BFS,还是普通的BFS。 代码如下:
# include<iostream>
# include<cstdio>
# include<string>
# include<queue>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int a,b,t;
vector<string>op;
bool operator < (const node &a) const {
return t>a.t;
}
node & operator = (const node &p) {
a=p.a,b=p.b,t=p.t;
op.clear();
for(int i=;i<p.op.size();++i)
op.push_back(p.op[i]);
return *this;
}
};
int vis[][];
void bfs(int A,int B,int C)
{
priority_queue<node>q;
memset(vis,,sizeof(vis));
node sta;
sta.a=sta.b=sta.t=;
sta.op.clear();
vis[][]=;
q.push(sta);
while(!q.empty())
{
node u=q.top();
q.pop();
if(u.a==C||u.b==C){
printf("%d\n",u.t);
for(int i=;i<u.op.size();++i)
cout<<u.op[i]<<endl;
return ;
}
if(u.a<A){
node now=u;
now.a=A,now.b=u.b;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("FILL(1)");
q.push(now);
}
}
if(u.b<B){
node now=u;
now.a=u.a,now.b=B;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("FILL(2)");
q.push(now);
}
}
if(u.a>){
node now=u;
now.a=,now.b=u.b;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("DROP(1)");
q.push(now);
}
}
if(u.b>){
node now=u;
now.a=u.a,now.b=;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("DROP(2)");
q.push(now);
}
}
if(u.a<A&&u.b>){
node now=u;
now.a=min(A,u.a+u.b);
now.b=max(,u.b-A+u.a);
if(!vis[now.a][now.b]){
vis[now.a][now.b]=vis[now.b][now.a]=;
now.t=u.t+;
now.op.push_back("POUR(2,1)");
q.push(now);
}
}
if(u.a>&&u.b<B){
node now=u;
now.a=max(,u.a-B+u.b);
now.b=min(B,u.b+u.a);
if(!vis[now.a][now.b]){
vis[now.a][now.b]=vis[now.b][now.a]=;
now.t=u.t+;
now.op.push_back("POUR(1,2)");
q.push(now);
}
}
}
printf("impossible\n");
}
int main()
{
int A,B,C;
scanf("%d%d%d",&A,&B,&C);
bfs(A,B,C);
return ;
}
POJ-3414 Pots (BFS)的更多相关文章
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- POJ 3414 Pots(BFS+回溯)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- POJ 3414 Pots (dfs,这个代码好长啊QAQ)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ 3414 pots (未解决)
http://poj.org/problem?id=3414 #include <iostream> #include <cstdio> #include <queue& ...
随机推荐
- MyEclipse2014+JDK1.7+Tomcat8.0+Maven3.2 开发环境搭建
1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置环境变量了. ————————————————— ...
- 移动互联网消息推送原理:长连接+心跳机制(MQTT协议)
互联网推送消息的方式很常见,特别是移动互联网上,手机每天都能收到好多推送消息,经过研究发现,这些推送服务的原理都是维护一个长连接(要不不可能达到实时效果),但普通的socket连接对服务器的消耗太大了 ...
- 20145106 java实验二
1)复数类ComplexNumber的属性 m_dRealPart: 实部,代表复数的实数部分 m_dImaginPart: 虚部,代表复数的虚数部分 public class ComplexNumb ...
- 探索Java8:(三)Predicate接口的使用
上一篇学习了下Function接口的使用,本篇我们学习下另一个实用的函数式接口Predicate. Predicate的源码跟Function的很像,我们可以对比这两个来分析下.直接上Predicat ...
- C#工程详解
转:https://www.cnblogs.com/zhaoqingqing/p/5468072.html 前言 写这篇文章的目地是为了让更多的小伙伴对VS生成的工程有一个清晰的认识.在开发过程中,为 ...
- apache2.4配置多个端口对应多个目录
文件 /usr/local/apache/conf/extra/httpd-vhosts.conf 的内容如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 NameVir ...
- JVM堆内存调优
堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操作 ...
- 51nod 1070 Bash游戏 V4
这种博弈题 都是打表找规律 可我连怎么打表都不会 这个是凑任务的吧....以后等脑子好些了 再琢磨吧 就是斐波那契数列中的数 是必败态 #include<bits/stdc++.h> u ...
- [AtCoder ARC101D/ABC107D] Median of Medians
题目链接 题意:给n个数,求出所有子区间的中位数,组成另外一个序列,求出它的中位数 这里的中位数的定义是:将当前区间排序后,设区间长度为m,则中位数为第m/2+1个数 做法:二分+前缀和+树状数组维护 ...
- HDU 6127 Hard challenge(扫描线)
http://acm.hdu.edu.cn/showproblem.php?pid=6127 题意: 有n个点,每个点有一个$(x,y)$坐标和一个权值,任意两点之间都有连线,并且连线的权值为两个顶点 ...