Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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 AB, 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<cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include<queue>;
using namespace std;
struct node
{
int av,aw,bv,bw;
int o[],pos;
}s;
bool vis[][];
int f;
node cur,next;
void fill(int i)
{
if(i==)
next.aw=next.av;
else if(i==)
next.bw=next.bv;
}
void drop(int i)
{
if(i==)
next.aw=;
else
next.bw=;
}
void pour(int a,int b)
{
if(a==&&b==){
if(next.aw>next.bv-next.bw)
{
next.aw-=(next.bv-next.bw);
next.bw=next.bv;
}
else
{
next.bw+=next.aw;
next.aw=;
}}
else
{
if(next.bw>next.av-next.aw)
{
next.bw-=(next.av-next.aw);
next.aw=next.av;
}
else
{
next.aw+=next.bw;
next.bw=;
}
}
}
void print(int i)
{
if(i==)
printf("POUR(1,2)\n");
else if(i==)
printf("POUR(2,1)\n");
else if(i==)
printf("FILL(1)\n");
else if(i==)
printf("FILL(2)\n");
else if(i==)
printf("DROP(1)\n");
else if(i==)
printf("DROP(2)\n");
}
bool bfs()
{
memset(vis,false,sizeof(vis));
s.aw=;s.bw=;
s.pos=;
vis[][]=true;
queue<node>Q;
Q.push(s);
while(!Q.empty())
{
cur=Q.front();
Q.pop();
if(f==cur.aw||f==cur.bw)
{
printf("%d\n",cur.pos);
for(int i=;i<cur.pos;i++)
{
print(cur.o[i]);
}
return true;
break;
}
//
if(cur.aw!=&&cur.bw!=cur.bv){
next=cur;
pour(,);
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.bw!=&&cur.aw!=cur.av){
next=cur;
pour(,);
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.aw!=cur.av){
next=cur;
fill();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.bw!=cur.bv){
next=cur;
fill();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.aw!=){
next=cur;
drop();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.bw!=){
next=cur;
drop();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
}
return false;
}
int main()
{
while(scanf("%d%d%d",&s.av,&s.bv,&f)!=EOF)
{
if(!bfs())printf("impossible\n");
}
return ;
}

POJ 3414 Pots(BFS)的更多相关文章

  1. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

  2. poj 3414 Pots(bfs+输出路径)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  3. POJ - 3414 Pots BFS(著名倒水问题升级版)

    Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...

  4. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  5. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  6. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  7. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  8. BFS POJ 3414 Pots

    题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...

  9. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

随机推荐

  1. BDD

    Binding business requirements to .NET code http://www.specflow.org/ 行为驱动开发 BDD:Behavior Driven Devel ...

  2. tcp连接以及网络I/O的几个问题

    这段时间在做一些web方面开发的事情,用的Nginx+fast-cgi,计划深入看一下Nginx的内部实现和架构,以方便理解和调优.后面准备写一篇有关Nginx介绍和深度解析的文章,要深入理解web服 ...

  3. 企业架构与建模之ArchiMate的由来和详述(上)

    终于完成了关于企业架构框架理论的总结,谢谢各位看官的支持,能挺过之前过于理论化的叙述而坚持到现在着实不易,笔者也自愧没有实践经验可以分享,希望日后有兴趣的看官能够不吝赐教.在本系列后面的也是最后一个大 ...

  4. Linux Tweak:交换 Caps_Lock 与 Control_R

    很少使用的Caps_Lok键占据着键盘的黄金位置,不仅如此,它还经常被按错. 于是受到程序员神器HHKB启发(如图) 对于我,Linux程序员 + vimer来说: ESC取代`键,极大的方便了VIM ...

  5. 圆形头像以及一些常见需求形状自定义ImageView组件

    在实际开发中,我们经常会遇到这样的需求,就是无论图片长啥样,我们都要其显示成圆形.圆形加一个边框.矩形加边框,带圆角的矩形等等,lib和demo下载地址:https://github.com/mapl ...

  6. 解决jqplot与jquery-ui导入必要包时的冲突

    解决jqplot与jquery-ui导入必要包时的冲突 对于一个网页中,即要有jqplot的画图,又要有jquery-ui的风格显示! 但在导入必要的包时,出现了问题! 先导入jqplot的必要包: ...

  7. jquery选择器之基本过滤选择器

    <style type="text/css"> /*高亮显示*/ .highlight{ background-color: gray } </style> ...

  8. SQL拼接方法

    smark Beetle可靠.高性能的.Net Socket Tcp通讯组件 另类SQL拼接方法 在编写SQL的时候经常需要对SQL进行拼接,拼接的方式就是直接String+处理,但这种情况有个不好的 ...

  9. 新版C#编译器关于函数闭包

    新版C#编译器关于函数闭包的一处更改   在Visual Basic.NET中,如果你写下类似下面的代码: Public Sub Test() For i = 0 To 100 Dim func =  ...

  10. LigerUI权限系统之用户管理

    用户管理较之前的的组织结构和菜单管理稍显复杂.不管怎样还是先上图吧,再来讲解 左边是组织结构,右边是用户,用户是跟组织机构挂钩的,通过点击左边的组织结构,来刷新右边,加载该组织机构下的用户. 用户管理 ...