Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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 AB, 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+输出路径)的更多相关文章

  1. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  2. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  3. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

  4. POJ - 3414 Pots BFS(著名倒水问题升级版)

    Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...

  5. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  6. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  7. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  8. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  9. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

随机推荐

  1. js-XMLHttpRequest 2级

    ###1. XMLHttpRquest 2级 1)   FormData 现代web应用中频繁使用的一项功能就死表单数据的序列化, XMLHttpRquest 2级为此定义了FormData类型 Fo ...

  2. java.util.Collections.copy():列表List浅拷贝

    今天同事问我怎样将一个列表(list1)拷贝到另一个列表(list2),然后修改新的列表(list2)不会影响到旧的列表(list1),想了一想,这是深拷贝啊. 可是,除了循环new还有别的办法吗,想 ...

  3. CentOS7下Nginx搭建反向代理,并使用redis保存session

    1.启动两个tomcat,端口分别为8080,8081 2.配置nginx,vim /usr/local/nginx/conf/nginx.conf 添加如下配置: 3.启动nginx或热加载 启动: ...

  4. python之路--关于线程的一些方法

    一 . 线程的两种创建方式 from threading import Thread # 第一种创建方式 def f1(n): print('%s号线程任务'%n) def f2(n): print( ...

  5. jackson使用问题:mapper.readValue()将JSON字符串转反序列化为对象失败或异常

    问题根源:转化目标实体类的属性要与被转JSON字符串总的字段 一 一对应!字符串里可以少字段,但绝对不能多字段. 先附上我这段出现了问题的源码: // 1.接收并转化相应的参数.需要在pom.xml中 ...

  6. DNS_PROBE_FINISHED_NXDOMAIN & MacOS

    DNS_PROBE_FINISHED_NXDOMAIN 内网 DNS bug 8.8.8.8 8.8.4.4 # new inner Wi-Fi 10.1.3.10 10.1.3.13 Windows ...

  7. tornado.gen.coroutine-协程

    http://blog.csdn.net/seeground/article/details/49488281  

  8. python time模块介绍(日期格式化 时间戳)

    import time # 1.time.time() 用于获取当前时间的时间戳, ticks = time.time() print(ticks) # 1545617668.8195682 浮点数 ...

  9. Bootstrap之表格、表单应用

    代码: <!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml&q ...

  10. Python——POP3邮件协议

    一.POP3协议用于收取邮件 二.POP3协议常用方法 user(login):想服务器发送登录名,并显示服务器的响应,表示服务器正在等待该用户的输入密码 pass_(passwd):在用户使用use ...