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. chapter15中使用generator来实现异步化操作的同步化表达的例子

    在p203中作者给了一个例子,我感觉这个例子写的不好,一开始我没有看懂,因为中间有很多细节没有交代,直到看了第二个用generator来实现ajax的例子之后才有所领悟.   所以我把作者给的这个用g ...

  2. python BeautifulSoup的简单使用

    官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/ 参考:https://www.cnblogs.com/yupeng/p/336203 ...

  3. Revit中如何给不同构件着色

    在Revit构件密集,默认的显示模式难以区分不同构件的区别,比如建筑立面有很多不同的机电管道,风管.水管,电缆桥架等,可一个给不同的机电管线添加不同的颜色,以示其区别,如下图所示,完成着色后,各种不同 ...

  4. Spark2.2(三十三):Spark Streaming和Spark Structured Streaming更新broadcast总结(一)

    背景: 需要在spark2.2.0更新broadcast中的内容,网上也搜索了不少文章,都在讲解spark streaming中如何更新,但没有spark structured streaming更新 ...

  5. Spark:几种给Dataset增加列的方式、Dataset删除列、Dataset替换null列

    几种给Dataset增加列的方式 首先创建一个DF对象: scala> spark.version res0: String = .cloudera1 scala> val , , 2.0 ...

  6. Easyui-DataGrid 分页多选框 及 遍历所有选中项

    html <table id='grid' class='easyui-datagrid' style='height:500px' url='Ajax-index.php?module=< ...

  7. java 代码的良好习惯

    有很多书籍提到了代码开发的良好习惯,但是自己看过后,在开发中并不能每次都想起来.在此处开贴做笔记,以后自己开发的代码,必须符合. 不要在一个代码块的开头把局部变量一次性都声明了(这是c语言的做法),而 ...

  8. [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现

    [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现 今天做Android项目的时候要用到图片选择,要实现拍照获取图片和从相册获取图片,并且要求在获取完之后可以裁剪,试了很多方法之 ...

  9. cmake 常用变量和常用环境变量查表手册---整理 .

    一,cmake 变量引用的方式: 前面我们已经提到了,使用${}进行变量的引用.在 IF 等语句中,是直接使用变量名而不通过${}取值 二,cmake 自定义变量的方式: 主要有隐式定义和显式定义两种 ...

  10. pandas数组(pandas Series)-(5)apply方法自定义函数

    有时候需要对 pandas Series 里的值进行一些操作,但是没有内置函数,这时候可以自己写一个函数,使用 pandas Series 的 apply 方法,可以对里面的每个值都调用这个函数,然后 ...