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. 理解js事件冒泡事件委托事件捕获

    js事件冒泡 javascript的事件传播过程中,当事件在一个元素上出发之后,事件会逐级传播给先辈元素,直到document为止,有的浏览器可能到window为止,这就是事件冒泡现象. <di ...

  2. linux下oracle数据库字符集修改

    linux下oracle数据库字符集修改 0.RHEL6.7.oracle11gr2 1.登录oracle.在安装oracle的用户下进入数据库. $ sqlplus / as sysdba 2.查询 ...

  3. day 44 django 学习入门

    前情提要: 终于学到了Django  ...古川小姐姐好流b .....7天学完.....脑壳疼..为了出了这个小火箭.. 详细参考官网. https://www.django.cn/ 中文网站 一: ...

  4. liunx相关指令

    修改网卡命名规范 ​ a 如何进入到救援模式 修改网卡 1.修改配置文件名称 /etc/sysconfig/network-scripts/
 名称为:ifcfg-xxx 2.修改配置文件内的 dev ...

  5. mapreduce基本原理

    场景: 一个大小为100T的文件,统计单词"ERROR"和"INFO"的个数 普通做法 是不是效率太低了? 换个方式 说明: 把100T文件分成100份,一台机 ...

  6. Java的注解相关的命令

    与注解处理器的有关的命令有5个,分别如下: (1)-XprintProcessorInfo 输出有关请求处理程序处理哪些注释的信息 (2)-XprintRounds 输出有关注释处理循环的信息 (3) ...

  7. 【转】Spark:一个高效的分布式计算系统

    原文地址:http://tech.uc.cn/?p=2116 概述 什么是Spark Spark是UC Berkeley AMP lab所开源的类Hadoop MapReduce的通用的并行计算框架, ...

  8. windows 下的bash 环境安装npm

    1.Git下载 node.js下载2.安装 git 和 node.js3.将git\bin node.js安装目录加入环境变量path中4.在D盘下建立目录gitrep    打开Git Bash初始 ...

  9. pycharm的python console报错CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 87, in init self.matchers.remove(self.python_matches) ValueError: list.remove(x): x not in list

    卸载ipython pip uninstall ipython 安装ipython6.2.0 pip install ipython==6.2.0

  10. solr 7.6 安装部署与遇到的问题

    目录 安装 solr 配置solr 到tomcat(关键) 配置依赖包 创建tomcat solr 的 classes 文件 创建 solr 的core 的主目录(也就是存放core的位置) 修改配置 ...