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. Oracle 表空间不足引起的问题及解决方法

    -- 1 向数据库导入数据时报了ORA-01653: unable to extend table错误,网上查了下原因是由于表空间不足引起的: 查询表空间使用情况语句 select a.tablesp ...

  2. hive安装详解

    1.安装MYSQL simon@simon-Lenovo-G400:~$ sudo apt-get install mysql-server simon@simon-Lenovo-G400:~$ su ...

  3. Ubuntu18.04安装mysql5.7

    Ubuntu18.04安装mysql5.7 1.1安装 首先执行下面三条命令: # 安装mysql服务 sudo apt-get install mysql-server # 安装客户端 sudo a ...

  4. php redis常用方法代码例子

    1,connect 描述:实例连接到一个Redis.参数:host: string,port: int返回值:BOOL 成功返回:TRUE;失败返回:FALSE 示例: <?php $redis ...

  5. windows php7 安装redis扩展

    1,首先查看phpinfo 这会决定扩展文件版本(特别注意以php版本的architecture是x86还是64为准,不能以操作系统为准): 2.根据PHP版本号,编译器版本号和CPU架构  一定要根 ...

  6. windos下完全卸载MySQL

    1.停止mysql服务(win+R,输入:services.msc回车) 2.控制面板卸载MySQL 3.cmd下删除MySQL服务:sc delete MySQL 4.删除目录 (1) C:\Pro ...

  7. kubernetes常用命令

    #.查询信息 kubectl get [需要查询的服务]   node 节点componentstatuses 简写 cs 组件状态namespaces 简写 ns 名命空间pod pod信息 添加  ...

  8. Python——glob模块

    一.作用: 找到匹配上特定格式的所有文件和文件夹,跟windows的文件搜索功能差不多 二.三种匹配符 *代表0个或多个字符 ?代表一个字符 []匹配指定范围内的字符,如[0-9]匹配数 三.应用方法 ...

  9. MySQL——安装、配置、启动服务、

    1.环境变量配置 将启动连接,加入环境变量中. mysqld  :启动服务端 msysql -u 用户名 -p 密码 : 启动客户端 2.windows服务:一直在运行中 E:\wupeiqi\mys ...

  10. Cherry.chen window.clipboardData实现剪切板操作总结 (好像只有ie好用)

    window.clipboardData的作用是在页面上将需要的东西复制到剪贴板上,提供了对于预定义的剪贴板格式的访问,以便在编辑操作中使用. 三个方法 (1)clearData(sDataForma ...