Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13231   Accepted: 5553   Special Judge

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)
#include"cstdio"
#include"cstring"
#include"queue"
#include"algorithm"
using namespace std;
const int MAXN=;
struct node{
int a,b,op,pre;
node(int ca,int cb,int co,int cp):a(ca),b(cb),op(co),pre(cp){}
node(){}
};
const char* opit[]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
int vis[MAXN][MAXN];
int A,B,C;
node step[MAXN*MAXN];
int cnt;
void print(int now,int ans)//??????
{
node no = step[now];
if(no.pre==-)
{
printf("%d\n",ans);
return ;
}
print(no.pre,ans+);
printf("%s\n",opit[no.op]);
}
void bfs()
{
memset(vis,,sizeof(vis));
cnt=;
queue<node> que;
que.push(node(,,,-));
while(!que.empty())
{
node now = que.front();que.pop();
step[cnt++]=now;
if(now.a==C||now.b==C)
{
print(cnt-,);
return ;
}
int ta,tb;
//第一种操作 FILL(A)
ta=A,tb=now.b;
if(!vis[ta][tb])
{
vis[ta][tb]=;
que.push(node(ta,tb,,cnt-));
} //第二种操作 FILL(B)
ta=now.a,tb=B;
if(!vis[ta][tb])
{
vis[ta][tb]=;
que.push(node(ta,tb,,cnt-));
}
//第三种操作 DROP(A)
ta=,tb=now.b;
if(!vis[ta][tb])
{
vis[ta][tb]=;
que.push(node(ta,tb,,cnt-));
}
//第四种操作 DROP(B)
ta=now.a,tb=;
if(!vis[ta][tb])
{
vis[ta][tb]=;
que.push(node(ta,tb,,cnt-));
}
//第五种操作 POUR(A,B)
ta=now.a-min(B-now.b,now.a);
tb=now.b+min(B-now.b,now.a);
if(!vis[ta][tb])
{
vis[ta][tb]=;
que.push(node(ta,tb,,cnt-));
}
//第六种操作 POUR(B,A)
ta=now.a+min(A-now.a,now.b);
tb=now.b-min(A-now.a,now.b);
if(!vis[ta][tb])
{
vis[ta][tb]=;
que.push(node(ta,tb,,cnt-));
}
}
printf("impossible\n");
}
int main()
{
while(scanf("%d%d%d",&A,&B,&C)!=EOF)
{
bfs();
}
return ;
}

poj3414Pots(倒水BFS)的更多相关文章

  1. hdu1495 倒水bfs

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1495/ 题意:给定三个杯子S,M,N,满足S=M+N,现在要求用最短的次数将S杯中的饮倒平分到两个杯子中.我们首 ...

  2. HDOJ1495(倒水BFS)

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  4. codevs1226倒水问题(Bfs)

    /* 首先建立模型 可以看成是三个水杯 第三个无穷大 (这里看成是201足够了) 最少步数 想到Bfs 维护队列里的状态:要有个步数 还要有v :此时刻三个杯子有多少水 然后倒水:因为没有刻度 所以有 ...

  5. HDU 1495 非常可乐(BFS倒水问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目大意:只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101) ...

  6. HDU 1495 非常可乐【BFS/倒水问题】

    非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  7. CodeVS 1226 倒水问题【DFS/BFS】

    题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水 ...

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

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

  9. BFS(倒水问题) HDU 1495 非常可乐

    题目传送门 /* BFS:倒水问题,当C是奇数时无解.一共有六种情况,只要条件符合就入队,我在当该状态vised时写了continue 结果找了半天才发现bug,泪流满面....(网上找份好看的题解都 ...

随机推荐

  1. 在普通网页中如何调用html5+的plus对象

    <script> //用法: HTML5+的plus对象,必须由click事件触发后,才能在普通网页中使用.所以在没有click的情况下,调用本文件可以解决问题! //在代码中使用: pl ...

  2. Django 视图之CBV

    CBV 所谓的CBV(class base view) 在视图里面,用类的方式来写逻辑 那么对于FBV,CBV有什么优势? CBV(class base views) 就是在视图里使用类处理请求. P ...

  3. python学习-1-列表,元组,字典,集合,文件操作

    1. 列表.元组操作 names = ['Alex',"Tenglan",'Eric'] >>> names[0] >>> names[2] & ...

  4. python基础19 -------面向对象终结篇(介绍python对象中各种内置命令)

    一.isinstance()和issubclass()命令 1.isinstance(对象,类型) 用来判定该对象是不是此类型或者说是该对象是不是此类的对象,返回结果为True和False,如图所示. ...

  5. ubuntu service XXX start启动报start: Rejected send message, 1 matche

    service cron restart命令报错如下: stop: Rejected send message, 1 matched rules; type="method_call&quo ...

  6. php添加或升级扩展模块步骤

    php添加或升级扩展模块步骤: 1).下载对应扩展的稳定版源码包2).进入到解压后的源码包执行: sudo /usr/local/php/bin/phpize //对应安装到哪个php版本 sudo ...

  7. 美团offer

    首先说明我是OP岗,RD的可能没有参考意义.本人985渣本一枚,非计算机.网络相关专业.不得不说美团的面试官给我的感觉很好,首先他们都比较极客,都是各个方向的大牛.虽然根据面试流程必须问我一些与我方向 ...

  8. P2455 [SDOI2006]线性方程组

    P2455 [SDOI2006]线性方程组 真\(\cdot\)高斯消元模板题 由于各种hack数据被造出来~码量突增~,其实也就多了二三十行 将每行系数消到最多有一个非0数 特殊情况: 在过程同时 ...

  9. jquery中篇

    一.attr 返回属性值 返回被选元素的属性值. 语法 $(selector).attr(attribute) 参数 描述 attribute 规定要获取其值的属性. 属性 • 属性 o attr(n ...

  10. vRA Customizing error

    toolsDeployPkg.log An error occurred while customizing VM vwbjvuqtest0751. For details reference the ...