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)
题意:给你两个体积为a,b的空水瓶 问你能不能通过以上6种操作 装到c体积的水
思路:倒水问题+在结构体里开一个数组记录路径
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int v[],c;
bool vis[][];
string way[]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct node{
int a[];
int cnt;
int path[];
};
node t;
void pour(int from,int to){
int temp=t.a[from]+t.a[to];
if(temp>=v[to]) t.a[to]=v[to];
else t.a[to]=temp;
t.a[from]=temp-t.a[to];
}
void fill(int i){
t.a[i]=v[i];
}
void drop(int i){
t.a[i]=;
}
void bfs(){
queue<node> q;
t.a[]=t.a[]=t.cnt=;
q.push(t);
while(!q.empty()){
node temp=q.front();
q.pop();
if(temp.a[]==c||temp.a[]==c){
cout<<temp.cnt<<endl;
for(int i=;i<=temp.cnt;i++)
cout<<way[temp.path[i]]<<endl;
return ;
}
for(int i=;i<;i++){
if(i==){
t=temp;
fill();
t.path[t.cnt+]=;
}else if(i==){
t=temp;
fill();
t.path[t.cnt+]=;
}else if(i==){
t=temp;
drop();
t.path[t.cnt+]=;
}else if(i==){
t=temp;
drop();
t.path[t.cnt+]=;
}else if(i==){
t=temp;
pour(,);
t.path[t.cnt+]=;
}else if(i==){
t=temp;
pour(,);
t.path[t.cnt+]=;
}
if(vis[t.a[]][t.a[]]) continue;
vis[t.a[]][t.a[]]=;
t.cnt++;
q.push(t);
}
}
cout<<"impossible"<<endl;
}
int main(){
ios::sync_with_stdio(false);
while(cin>>v[]>>v[]>>c){
memset(vis,,sizeof(vis));
bfs();
}
return ;
}
poj 3414 Pots(bfs+输出路径)的更多相关文章
- POJ 3414 Pots ( 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://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- POJ - 3414 Pots BFS(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
- POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
- POJ 3414 Pots (BFS/DFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
- poj 3414 Pots bfs+模拟
#include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
随机推荐
- Nginx三部曲(3)SSL
我们将告诉你 Nginx 的运作模式.蕴含的概念,怎样通过调优 Nginx 来提高应用性能,或是如何设置它的启动和运行. 这个教程有三个部分: 基本概念 —— 这部分需要去了解 Nginx 的一些指令 ...
- 通过修改Tomcat配置,解决乱码问题
贴图,问题如下: tomcat使用的默认编码方式是iso8859-1 修改tomcat下的conf/server.xml文件 找到如下代码: <Connector port="8 ...
- PHP5.4.0新特性研究
PHP5.4.0新特性研究 1.内建Web Server 这的确是个好的改进,大大的方便了开发人员.以后开发机不装nginx,httpd也行 cd $PHP_INSTALL_PATH ./bin/ph ...
- Storm原理
zookeeper是对称结构
- Nginx安装- CentOS7
1.确认是否具备安装环境 g++ -v 如果不打印则不具备. 解决办法:联网执行如下命令 yum install gcc yum install gcc-c++ 2.需要材料 pcre-8.37.t ...
- 在IWMS中的分页效果
第一步,你需要在后台修改你所要显示的新闻数目: 第二步,你需要把这段代码加到你需要分页的列表里边 代码: <%=config.TopAd%><asp:Literal id=" ...
- 关于事务回滚,rollback tran到底要不要写?
关于事务回滚,有些不明白,不知道rollback tran在什么时候用. begin tran update 表1 update 表2 commit tran 这种写法,在更新表1或表2时出错,事务会 ...
- 使用电脑adb给Essential Phone刷机 —(官方篇)
用ADB给Essential Phone线刷升级 重要:请确保在刷机前已经解锁,关于解锁教程群里有! 准备 原版boot Twrp boot Magisk卡刷包 到官网下载OTA包 准备好Essent ...
- [oracle] to_date() 与 to_char() 日期和字符串转换
to_date("要转换的字符串","转换的格式") 两个参数的格式必须匹配,否则会报错. 即按照第二个参数的格式解释第一个参数. to_char(日期,& ...
- 思路:当一个表嵌套另一个表时候 只需在dao中引入该mapper即可 进行正常的数据插入 查询 修改等