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. linux:磁盘的分割、检验、格式化与挂载

    新增一颗磁碟: 1.对磁碟进行分割,以建立可用的partition 2.对该分割槽partition进行格式化(format),以建立系统可用的filesystem 3.若要仔细点,可对刚刚建立的fi ...

  2. 【Origin】工仕途中

    -脚步翩跹,随蝶起舞,翩翩不知所往 晨起脚步催, 蝴蝶迎面飞; 正是春意浓, 三月好风景. -作于二零一六年三月二十八日

  3. poj 1179 Polygon

    http://poj.org/problem?id=1179 Polygon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  4. Linux 硬盘分区

    Linux系统中的重要概念,一切资源都看做是文件,包括硬件设备. 1. 基本概念 1)MBR:Master Boot Recorder,存放主引导记录,446字节的引导代码. 2)主分区表:存放主分区 ...

  5. .NET: C#: System.Diagnostics

    1. Trace & Debug 理解这两者的区别,Trace有个Listners.Add()非常好用,这里网上有个在ListBox里输出Debug和Trace信息的 using System ...

  6. 从一简单程序看C语言内存分配

    int main13(){ char buf[20]="aaaa"; char buf2[] = "bbbb"; char *p1 = "111111 ...

  7. URAL 1139 City Blocks(数论)

    The blocks in the city of Fishburg are of square form. N avenues running south to north and Mstreets ...

  8. C# 文件读取(二)

    将我的电脑中的文件夹信息显示到TreeView控件上. 方法很多种,下面这种方法添加了我的文档. public partial class Form1 : Form { public Form1() ...

  9. MyEclipse安装插件的三种方法和使用心得

    本文讲解MyEclipse(MyEclipse10)的三种方法,以TestNG为例 Eclipse update site URL:  http://beust.com/eclipse. 一.通过My ...

  10. 【fedora】制作安装u盘

    找一台安装好linux系统的PC,将下载的LiveCD ISO文件复制到硬盘(这样速度快),查看U盘挂载的位置(用磁盘工具),在终端中使用dd命令: $ sudo dd if=<Live ISO ...