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:什么是linux

    1>.linux是一套作业系统(linux就是核心与呼叫这两层),每一种作业系统都是在他专门的硬体机器上面运行的:linux是一个Open Source的作业系统,具有可移植性 2>.li ...

  2. 理解运算符 || 和 && 及方法

    || 前面为true的话直接返回前面的值,前面为false的话返回后面的值.如下: console.log(0 || 1); console.log(1 || 0); console.log(1 || ...

  3. JSP 使用框架frame

    JSP使用框架放在<head>标签里面.如果放在<body>标签里面,用Tomcat打开,是不会显示的.

  4. [原创]java WEB学习笔记50:文件上传案例

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  5. POJ 2417 Discrete Logging(离散对数-小步大步算法)

    Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...

  6. LED流水灯(二)

    记住看汇编的时候是红在上面 黑色在下面 startup.s 程序 ; MDK跑马灯实验; PRESERVE8               // 字节对齐关键词 ,汇编有8位对齐的要求,要添加 AREA ...

  7. 一个标准的ECharts代码

    <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</t ...

  8. 现在觉得IT还挺有意思

    前两天刚刚接触编程,用的是C#.开始确实枯燥,但是今天的感觉就好多了,还挺有意思.根据老师讲的课程自己编写了小程序,运行起来还不错.在这里分享下. 关于时间安排的小程序: int sj; int aa ...

  9. 09---Net基础加强

    复习 Person类: using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...

  10. RobotFramework 安装配置(二)

    前面已经写了一篇关于RF的安装配置了,那是在做自动化工具调研的时候搭建RF总结的,基于win32的系列软件安装的过程.经过1个月的调研,做成了demo,也大致学RF的使用和python的基础语法,暂时 ...