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所以写的比较差,其实就是广搜,一点点找这个状态可以做哪些操作,然后慢慢搜,可以做的就存起来,然后一直下去,记得存走过的位置

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
//#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
//#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
string op[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};//存放操作,便于输出
//操作中,0是1加水,1是2加水 ,2是1倒水,3是2倒水,4是1倒给2,5是2倒给1
int A,B,C;
int visit[110][110];//用来存走过的状态,免得又走回去
struct water
{
int x,y,bits;
int a[1000];//记录经过的操作
water(){}
};
water ans;//放答案
queue<water>v;//做一个队列
void bfs(water q)
{
water t,tt;
while(!v.empty())
{
t=v.front();
v.pop();
if(t.x ==C||t.y ==C)//走到要求的了可以跳出了
{
ans=t;
return ;
}
if(visit[t.x][t.y])
continue;
else
visit[t.x][t.y]=1;
rep(i,0,6)
{
if(i==0)
{
if(t.x!=A)//判断可以做这个操作吗
{
tt=t;
tt.x=A;
tt.a[t.bits]=0;
tt.bits ++;
v.push(tt);
}
}
else if(i==1)
{
if(t.y!=B)//判断可以做这个操作吗
{
tt=t;
tt.y=B;
tt.a[t.bits]=1;
tt.bits++;
v.push(tt);
}
}
else if(i==2)
{
if(t.x)//判断可以做这个操作吗
{
tt=t;
tt.x=0;
tt.a[t.bits]=2;
tt.bits++;
v.push(tt);
}
}
else if(i==3)
{
if(t.y)//判断可以做这个操作吗
{
tt=t;
tt.y=0;
tt.a[t.bits]=3;
tt.bits++;
v.push(tt);
}
}
else if(i==4)
{
if(t.x)//判断可以做这个操作吗
{
tt=t;
if(t.x+t.y<=B)//判断会有多余的水吗
{
tt.x=0;
tt.y=t.x+t.y;
}else
{
tt.y=B;
tt.x=t.x+t.y-B;
}
tt.a[t.bits]=4;
tt.bits++;
v.push(tt);
}
}
else if(i==5)
{
if(t.y)//判断可以做这个操作吗
{
tt=t;
if(t.x+t.y<=A)//判断会有多余的水吗
{
tt.y=0;
tt.x=t.x+t.y;
}else
{
tt.x=A;
tt.y=t.x+t.y-A;
}
tt.a[t.bits]=5;
tt.bits++;
v.push(tt);
}
}
}
}
}
int main()
{
cin>>A>>B>>C;
water q;//
q.x=0;
q.y=0;
q.bits=0;
mm(visit,0);
mm(q.a,0);
v.push(q); //这些是初始化操作
bfs(q);
if(ans.bits) //最后如果有操作次数就输出,没输出impossible。。之前交了一发发现没加上这个
{
cout<<ans.bits<<endl;
rep(i,0,ans.bits)//输出操作
cout<<op[ans.a[i]]<<endl;
}
else
cout<<"impossible";
return 0;
}

M - Pots的更多相关文章

  1. POJ 3414 Pots

    Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

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

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

  3. Pots 分类: 搜索 POJ 2015-08-09 18:38 3人阅读 评论(0) 收藏

    Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11885 Accepted: 5025 Special Judge D ...

  4. Pots of gold game:看谁拿的钱多

    问题描述: Pots of gold game: Two players A & B. There are pots of gold arranged in a line, each cont ...

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

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

  6. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  7. Pots(bfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8266   Accepted: 3507   Special Judge D ...

  8. POJ 3414 Pots 记录路径的广搜

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

  9. poj 3414 Pots (bfs+线索)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special J ...

  10. POJ 3414 Pots(BFS)

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

随机推荐

  1. CentOS7下FTP的安装与配置

    1.安装vsftpd 1 [root@localhost modules]# yum install -y vsftpd 2.编辑ftp配置文件 1 [root@localhost modules]# ...

  2. 【GPU编解码】GPU硬编码 (转)

    一.OpenCV中的硬编码 OpenCV2.4.6中,已实现利用GPU进行写视频,编码过程由cv::gpu::VideoWriter_GPU完成,其示例程序如下. 1 int main(int arg ...

  3. Swift学习笔记(十四)——字符,常量字符串与变量字符串

    在学习Java过程中,字符串碰到过String和StringBuffer,当中前者是不可变的,不能对字符串进行改动:后者是可变的,能够不断改动. 来到Swift中,对字符串的定义变的更加简单. (1) ...

  4. [Java] HashMap 源码简要分析

    特性 * 允许null作为key/value. * 不保证按照插入的顺序输出.使用hash构造的映射一般来讲是无序的. * 非线程安全. * 内部原理与Hashtable类似.   源码简要分析 pu ...

  5. c/c++ 读入一行不确定个数的整数

    假设有一个文件,文件的每一行包括n个整数,整数之间以一个空格隔开,文件总共有m行,但是事先不知道n,m.如何每次从文件中读取一行整数放到一个数组中. 可以分为两步:1.首先从文件中读入一行字符串,2. ...

  6. 单反毁三代,kindle富一生

    盖kindle 面更香 人皆知有用之用,而莫知无用之用   一个存留记忆的地方:http://i-remember.fr/en   文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论

  7. 使用go语言操作db2

    操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 db2版本: db2_v101_linuxx64_expc 问题描述 怎么使用go语言在CentO ...

  8. LEAPMOTION开发UI专题(1)

    非常 抱歉的是,之前说的LEAP/UI框架设计可能仅仅有两篇 由于个人时间实在是不同意 这个问题假设展开去写的话 那么说写本书都是不为过的 且由于内容改动非常是杂乱 所以我第一篇文章用来介绍LEAP预 ...

  9. 源码版本管理工具 :TFS GIT

    至于svn  ..忽略不计了... 集中式代码管理 CVCS 模式:TFS 分布式代码管理 DVCS 模式:git 两者比较大的差别:tfs 只有一个中央仓储,其他副本都要与中央仓储进行更新.git  ...

  10. 让人非常easy误解的TCP拥塞控制算法

    正文 非常多人会觉得一个好的TCP拥塞控制算法会让连接加速,这样的观点是错误的.恰恰相反,全部的拥塞控制算法都是为了TCP能够在贪婪的时候悬崖勒马,大多数时候.拥塞控制是减少了数据发送的速度. 我在本 ...