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. nginx:配置详细说明

    一.fastcgi param 详情: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径 fast ...

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

    11.3 管理 pgq-queues Skytools 的一个核心组件是pgq.它提供了一个通用排队接口,它可以让您把消息从一个消息提供者传送到一个任意数目的接收者. 现在的问题是:一般来说,一个队列 ...

  3. Error : L6218E: Undefined symbol downloadAddress (referred from nand.o).

    MKD 报错: linking...LCD.axf: Error: L6218E: Undefined symbol EnZK (referred from ht128x64.o).LCD.axf: ...

  4. 转:python webdriver API 之下载文件

    webdriver 允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中.要想下载文件,首选要先确定你所要下载的文件的类型.要识别自动文件的下载类型可以使用 curl ,如图 ...

  5. poj 1176 Party Lamps

    http://poj.org/problem?id=1176 Party Lamps Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

  6. linux第7天 I/O的五种模型, select

    服务器端避免僵尸进程的方法: 1)通过忽略SIGCHLD信号,解决僵尸进程 signal(SIGCHLD, SIG_IGN) 2)通过wait方法,解决僵尸进程 signal(SIGCHLD, han ...

  7. Logic BIST

    Logic BIST is crucial for many applications, in particular for life-critical and mission-critical ap ...

  8. FlexNOC

    arteris公司提供一系列工具,来完成NOC的生成,包括model,netlist,TB,script 生成包括三个阶段: NoC specification Phase:使用FlexArtist ...

  9. python pdb

    pdb 以参数-m pdb启动后,pdb定位到下一步要执行的代码-> s = '0'.输入命令l来查看代码: 输入命令n可以单步执行代码: 任何时候都可以输入命令p 变量名来查看变量: (Pdb ...

  10. [JAVA]在linux中设置JDK环境,ZendStudio,Eclipse

    1.准备JDK安装包 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载对应平台的tar.gz格式压 ...