Pots

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 11885 Accepted: 5025 Special Judge

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)

Source

Northeastern Europe 2002, Western Subregion

以前曾经做过的一道题,今天再做发现没有思路,看来要滚滚原题了,这个题就是把所有的情况分为六种操作,bfs这六种操作

#include <map>
#include <list>
#include <climits>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL unsigned long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout) const int Max = 10010; struct node
{
int a;
int b;
int step;
string str[120];
};
int A,B,C;
bool vis[120][120];
bool BFS()
{
memset(vis,false,sizeof(vis));
node f,s;
f.a=0;
f.b=0;
f.step=0;
queue<node>Q;
Q.push(f);
vis[0][0]=true;
while(!Q.empty())
{
f=Q.front();
// cout<<f.a<<"\t"<<f.b<<endl;
Q.pop();
if(f.a==C||f.b==C)
{
cout<<f.step<<endl;
for(int i=1;i<=f.step;i++)
{
cout<<f.str[i]<<endl;
}
return true;
}
if(f.a==0)
{
s=f;
s.a=A;
s.step++;
s.str[s.step]="FILL(1)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.a<=A&&f.a)
{
s=f;
s.a=0;
s.step++;
s.str[s.step]="DROP(1)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.b<B&&f.a)
{
s=f;
s.step++;
if(s.a+s.b<=B)
{
s.b+=s.a;
s.a=0;
}
else
{
s.a=s.a+s.b-B;
s.b=B;
}
s.str[s.step]="POUR(1,2)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.b==0)
{
s=f;
s.b=B;
s.step++;
s.str[s.step]="FILL(2)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.b<=B&&f.b)
{
s=f;
s.b=0;
s.step++;
s.str[s.step]="DROP(2)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.a<A&&f.b)//这里要特别注意,不要写错顺序
{
s=f;
s.step++;
if(s.a+s.b<=A)
{ s.a+=s.b;
s.b=0;
}
else
{
s.b=s.a+s.b-A;
s.a=A; }
s.str[s.step]="POUR(2,1)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
}
return false;
}
int main()
{
while(~scanf("%d %d %d",&A,&B,&C))
{
if(!BFS())
{
printf("impossible\n");
}
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Pots 分类: 搜索 POJ 2015-08-09 18:38 3人阅读 评论(0) 收藏的更多相关文章

  1. highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏

    /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...

  2. OC基础:数组.字典.集 分类: ios学习 OC 2015-06-18 18:58 47人阅读 评论(0) 收藏

    ==============NSArray(不可变数组)=========== NSArray,继承自NSObject  用来管理(储存)一些有序的对象,不可变数组. 创建一个空数组 NSArray ...

  3. 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...

  4. 摄像头参数查看与调节 分类: C/C++ OpenCV 2014-11-08 18:13 138人阅读 评论(0) 收藏

    cvGetCaptureProperty 获得视频获取结构的属性 double cvGetCaptureProperty( CvCapture* capture, int property_id ); ...

  5. const char*, char const* and char *const 分类: C/C++ OpenCV 2014-11-08 18:10 114人阅读 评论(0) 收藏

    const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目.  事实上这个概念谁都有只是三种声明方式非常相似很容易记混.  Bjarne在他的 ...

  6. android开发之AlertDialog点击按钮之后不消失 分类: android 学习笔记 2015-07-15 18:07 89人阅读 评论(0) 收藏

    最近有这样一个需求,我需要用户在一个弹出框里输入密码来验证,验证成功当然好说,但是如果验证失败则需要把alertdialog的标题改为"密码错误,请重新输入",并且这个alertd ...

  7. OC基础:类和对象 分类: ios学习 OC 2015-06-12 18:55 17人阅读 评论(0) 收藏

    OC:Objective-c     面向对象的c语言,简称obj-c或者OC OC和C的区别 1.OC是C语言的超集,OC是在C语言的基础上结合smalltalk的优点,开发出来的语言.oc兼容所有 ...

  8. 架构师速成6.7-设计开发思路-uml 分类: 架构师速成 2015-07-29 18:25 157人阅读 评论(0) 收藏

    uml是什么东西?统一建模语言,一门语言,是用来进行软件设计的一门语言. 其实一门语言的诞生并不伟大,让大多数人都使用才足够伟大.uml就是一门伟大的语言,因为目前软件设计的唯一语言就是它. UML其 ...

  9. HDU 1272 小希的迷宫(并查集) 分类: 并查集 2015-07-07 23:38 2人阅读 评论(0) 收藏

    Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...

随机推荐

  1. RadioButton 组,ComboBox用法:

    RadioButton 组 final ToggleGroup group = new ToggleGroup(); final RadioButton rb1 = new RadioButton(& ...

  2. PostgreSQL Replication之第十一章 使用Skytools(2)

    11.2 剖析 skytools Skytools 不只是一个单一的脚本,而是一个提供各种不同服务的工具的集合.一旦我们安装了Skytools,更详细地查一下这些组件的细节是有意义的: • londi ...

  3. 使用Mac的AppleScritp调用控制台的方式

    使用Mac的AppleScritp调用 控制台的方法 tell application "Terminal" activate do script "cd Documen ...

  4. 免费VPN 实测可用

    vpngate.net 的镜像站点列表 (更新于 2014-05-18 03:06:00 UTC): http://121.135.220.121:26633/cn/ (Mirror location ...

  5. JAVA JDBC连接 SQLServer2012 连接失败 端口号错误

    SQLServer2012的SQL Sever 网络配置 我有4个 SQLEXPRESS的协议 SQLSERVER2008的协议 MSSQLSERVER的协议 SQLSERVER2012的协议 他们都 ...

  6. Codeforce Round #213 Div2

    哎,弄了半天这次的C还是没出,真是捉急!,还TM的以为前几次只是运气不好!太逗了!

  7. POJ - 2041Unreliable Message

    这里的算法非常简单,就是“模拟”,注意编写每个传令官的算法时先分开测试,放在一起就会混淆. POJ - 2041Unreliable Message Time Limit: 1000MS Memory ...

  8. SqlServer 中如何查看某一个Sql语句是复用了执行计划,还是重新生成了执行计划

    我们知道SqlServer的查询优化器会将所执行的Sql语句的执行计划作缓存,如果后续查询可以复用缓存中的执行计划,那么SqlServer就会为后续查询复用执行计划而不是重新生成一个新的执行计划,因为 ...

  9. remoting方式

    1. WebService跨平台,跨防火墙,但是很抱歉,基于xml速度慢2. RMI(java)/Remoting(.net)平台相关,基于二进制序列化,速度快.3.dcom(com+)spring提 ...

  10. Openstack的error僵尸实例的解决办法

    在我们对集群环境进行各种调整的情况下,很容易产生一些僵尸实例. 僵尸实例主要是没有该主机,但是在dashboard上,数据库中存在,解决办法网络上有的人给出了繁杂的修改数据库的方法,其实按照下面的命令 ...