Pots

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)

Source

Northeastern Europe 2002, Western Subregion
 
 //2017-02-24
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; struct node
{
int a, b, step, pre, op;
}q[]; int pots[], POT[], book[][];
string option[] = {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"}; void fill(int i)
{
pots[i] = POT[i];
} void drop(int i)
{
pots[i] = ;
} void pour(int i, int j)
{
if(pots[i] > POT[j]-pots[j]){
pots[i] -= (POT[j]-pots[j]);
pots[j] = POT[j];
}else{
pots[j] += pots[i];
pots[i] = ;
}
} void init(int a, int b)
{
pots[] = a;
pots[] = b;
} void push(int pos, int a, int b, int step, int pre, int op)
{
q[pos].a = a;
q[pos].b = b;
q[pos].step = step;
q[pos].pre = pre;
q[pos].op = op;
} void print(int pos)
{
if(q[pos].pre == -)return;
print(q[pos].pre);
cout<<option[q[pos].op]<<endl;
} int min(int a, int b)
{
return a < b ? a : b;
} int main()
{
int C, a, b, step;
while(cin>>POT[]>>POT[]>>C)
{
int head = , tail = ;
q[].a = ;
q[].b = ;
q[].step = ;
q[].pre = -;
memset(book, , sizeof(book));
book[][] = ;
while(head < tail)
{
a = q[head].a;
b = q[head].b;
step = q[head].step; if(a==C || b==C){
cout<<step<<endl;
print(head);
break;
} init(a, b);
fill();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} init(a, b);
fill();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} if(a>){
init(a, b);
drop();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
}
} if(b>){
init(a, b);
drop();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
}
} init(a, b);
pour(, );
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} init(a, b);
pour(, );
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} head++;
}
if(head>=tail)cout<<"impossible"<<endl;
}
return ;
}

POJ3414(KB1-H BFS)的更多相关文章

  1. poj3414 Pots (BFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   Special J ...

  2. POJ-3414 Pots (BFS)

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

  3. poj3414 Pots(BFS)

    题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...

  4. FZU - 2150 bfs [kuangbin带你飞]专题一

    题意:两个人玩很变态的游戏,将一个草坪的某两个点点燃,点燃的草坪可以向上下左右四个方向扩散,问能否将整块草坪上面的草都点燃.如果能,输出最短时间(^_^他们就能玩更变态的游戏了),如果不能,输出-1. ...

  5. UVA-11882 bfs + dfs + 剪枝

    假设当前已经到达(x,y),用bfs判断一下还可以到达的点有maxd个,如果maxd加上当前已经经过的长度小于当前答案的长度就退出,如果相同,就将bfs搜索到的点从大到小排序,如果连最大序列都无法大于 ...

  6. 广度优先(bfs)和深度优先搜索(dfs)的应用实例

    广度优先搜索应用举例:计算网络跳数 图结构在解决许多网络相关的问题时直到了重要的作用. 比如,用来确定在互联网中从一个结点到另一个结点(一个网络到其他网络的网关)的最佳路径.一种建模方法是采用无向图, ...

  7. 邻接矩阵实现图的存储,DFS,BFS遍历

    图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...

  8. ZH奶酪:【数据结构与算法】搜索之BFS

    1.目标 通过本文,希望可以达到以下目标,当遇到任意问题时,可以: 1.很快建立状态空间: 2.提出一个合理算法: 3.简单估计时空性能: 2.搜索分类 2.1.盲目搜索 按照预定的控制策略进行搜索, ...

  9. 扫描线+堆 codevs 2995 楼房

    2995 楼房  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...

  10. 【BZOJ 1054】 [HAOI2008]移动玩具

    Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动 ...

随机推荐

  1. django 使用celery 实现异步任务

    celery 情景:用户发起request,并等待response返回.在本些views中,可能需要执行一段耗时的程序,那么用户就会等待很长时间,造成不好的用户体验,比如发送邮件.手机验证码等. 使用 ...

  2. 四,mysql优化——sql语句优化之索引二

    1,在什么列适合添加索引 (1)较频繁的作为查询条件字段应该添加索引 select * from emp where empid = 2; (2)唯一性太差的字段不适合添加索引,即时频繁作为查询条件. ...

  3. linux下redis安装步骤

    1.官网上下载redis最新包,我下载的是redis-5.0.3.tar.gz,上传至服务器 2.解压缩:tar zxvf redis-5.0.3.tar.gz3.cd redis-5.0.3进入re ...

  4. OS之进程管理---多处理器调度

    引言 之前我们所学习的操作系统进程调度策略的前提条件是单处理器系统下的CPU调度程序.如果系统中存在多个CPU,那么负载分配就成为可能,但是相应的调度问题就会更加复杂. 多处理器调度方法 对于多处理器 ...

  5. 如何使用 AutoWire方式注入 JdbcDaoSupport DataSource

      @Repositorypublic class MyDaoImpl extends JdbcDaoSupport implements MyDao { @Autowired private Dat ...

  6. mysql互为主从实战设置详解及自动化备份(Centos7.2)

    mysql互为主从实战设置详解(Centos7.2) 第一步:mysql配置  my.cnf配置 服务器1 (10.89.10.90) [mysqld]  server-id=1  log-bin=/ ...

  7. syslog之一:Linux syslog日志系统详解

    目录: <syslog之一:Linux syslog日志系统详解> <syslog之二:syslog协议及rsyslog服务全解析> <syslog之三:建立Window ...

  8. Spring Security构建Rest服务-1202-Spring Security OAuth开发APP认证框架之重构3种登录方式

    SpringSecurityOAuth核心源码解析 蓝色表示接口,绿色表示类 1,TokenEndpoint 整个入口点,相当于一个controller,不同的授权模式获取token的地址都是 /oa ...

  9. (01)JVM-内存三大核心区域以及分析

    package org.burning.sport.jvm; /** *  从JVM调用的角度分析Java程序对内存空间的使用, * 当JVM进程启动的时候,会从类加载器路径中找到包含main方法的入 ...

  10. Disconf 学习系列之Disconf 的主要目标

    不多说,直接上干货! 部署极其简单:同一个上线包,无须改动配置,即可在 多个环境中(RD/QA/PRODUCTION) 上线: 部署动态化:更改配置,无需重新打包或重启,即可 实时生效: 统一管理:提 ...