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 ...
随机推荐
- async并发处理
- C#设计模式之7:适配器模式
适配器模式 使用适配器模式的一个重要的点是首先要识别出什么代码(接口)是已经存在的,什么代码(接口)是新的,需要去适配的.适配器的作用是让旧的(现有的)接口能够匹配新的系统(要去适配的). 比如有下面 ...
- spark单击 搭建
http://files.cnblogs.com/files/yxnyd/spark.zip
- python爬虫之scrapy文件下载
我们在写普通脚本的时候,从一个网站拿到一个文件的下载url,然后下载,直接将数据写入文件或者保存下来,但是这个需要我们自己一点一点的写出来,而且反复利用率并不高,为了不重复造轮子,scrapy提供很流 ...
- kdump简单的介绍
kdump是2.6.16之后,内核引入的一种新的内核崩溃现场信息收集工具.当一个内核崩溃后(我们称之为panic),内核会使用kexec(类似于进程的exec,把当前内核换掉)进入到一个干净的备份内核 ...
- C# 中那些常用的工具类(Utility Class)(二)
今天按照这一年来经常用到的那些静态的工具类再来做一次总结,这些小的工具来可以作为自己学习的很好的例子,通过总结这些东西,能够很大程度上梳理自己的知识体系,当然这个是经常用到的,接下来就一个个去分析这些 ...
- SpringBoot 标签之启动
在SpringBoot中入口我们使用: package com.sankuai.qcs.regulation.traffic; import org.springframework.boot.Spri ...
- ES6 & Map & hashMap
ES6 & Map & hashMap 01 two-sum https://leetcode.com/submissions/detail/141732589/ hashMap ht ...
- 老男孩python学习自修【第三天】列表用法
列表的使用: list.append(value) 向列表增加元素 list.insert(index, value) 向列表指定元素插入元素 list.extend(newlist) 用新的列表扩展 ...
- EmpireCMS的使用
1.下载安装empirecms 下载完成后解压将upload目录整体上传到服务器,并更名为empirecms_test 更改目录文件的权限: chmod -R 777 empirecms_test 配 ...