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& ...
随机推荐
- SpringBoot集成Socket服务后打包(war包)启动时如何启动Socket服务(web应用外部tomcat启动)
1.首先知道SpringBoot打包为jar和war包是不一样的(只讨论SpringBoot环境下web应用打包) 1.1.jar和war包的打开方式不一样,虽然都依赖java环境,但是j ...
- CentOS随笔 - 4.CentOS7安装MySql 5.5.60(下载 tar 方式安装)
前言 转帖请注明出处: http://www.cnblogs.com/Troy-Lv5/ 由于公司也有php+mysql的项目, 所以今天也把Mysql装了一遍. 为了与以前的程序和数据库兼容, 这次 ...
- 手撕vue-cli配置文件——check-versions.js篇
check-versions.js,vue-cli中检查版本的js文件. 'use strict' const chalk = require('chalk') const semver = requ ...
- mysql 触发器 trigger用法 two (稍微复杂的)
触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...
- 使用wireshark分析tcp/ip报文之报文头
以太网报文的结构如下: 其中,以太网的帧头: 14 Bytes:MAC目的地址48bit(6B),MAC源地址48bit(6B),Type域2B,一共14B. IP头部: TCP头部: http:// ...
- Java查找算法之二分查找
二分查找是一种查询效率非常高的查找算法.又称折半查找. 一.算法思想 有序的序列,每次都是以序列的中间位置的数来与待查找的关键字进行比较,每次缩小一半的查找范围,直到匹配成功. 一个情景:将表中间位置 ...
- ubuntu16.04下内核模块解析
一.环境如下: 1.1内核版本: jello@jello:~$ uname -a Linux jello 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19: ...
- 打印图形|2014年蓝桥杯B组题解析第五题-fishers
打印图形 小明在X星球的城堡中发现了如下图形和文字: rank=3 rank=5 rank = 6 小明开动脑筋,编写了如下的程序,实现该图形的打印. 答案:f(a, rank-1, row, col ...
- mysql数据库分库分表(Sharding)
mysql数据库切分 前言 通过MySQLReplication功能所实现的扩展总是会受到数据库大小的限制.一旦数据库过于庞大,尤其是当写入过于频繁,非常难由一台主机支撑的时候,我们还是会面临到扩展瓶 ...
- nginx缓存功能的设置
首先用的缓存是proxy_cache. 在http段里加入下列几句: [plain] view plain copy proxy_connect_timeout 5; proxy_read_tim ...