Pots

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 jis 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。用二维数组b来记录两个杯子的容量。分六步,倒满1、倒满2、倒空1、倒空2、1倒入2、2倒入1。倒入水时要保证不会溢出,所以倒入要分全部倒入和部分倒入两种情况。步骤较多,所以代码啰嗦些。。
#include<stdio.h>
#include<queue>
using namespace std; int b[][],re[];
struct Node{
int x,y,s,c,f,d;
}node[]; int main()
{
int x,y,z,c,f,i;
queue<Node> q;
scanf("%d%d%d",&x,&y,&z);
if(z==) printf("0\n"); //一定注意。。
else{
b[][]=;
node[].x=;
node[].y=;
node[].s=;
node[].c=;
node[].f=;
node[].d=;
q.push(node[]);
c=;f=;
while(q.size()){
for(i=;i<=;i++){
if(i==&&q.front().x<x&&b[x][q.front().y]==){
b[x][q.front().y]=;
node[++c].x=x;
node[c].y=q.front().y;
node[c].s=q.front().s+;
node[c].c=c;
node[c].f=q.front().c;
node[c].d=;
if(node[c].x==z||node[c].y==z){
f=node[c].s;
break;
}
q.push(node[c]);
}
else if(i==&&q.front().y<y&&b[q.front().x][y]==){
b[q.front().x][y]=;
node[++c].x=q.front().x;
node[c].y=y;
node[c].s=q.front().s+;
node[c].c=c;
node[c].f=q.front().c;
node[c].d=;
if(node[c].x==z||node[c].y==z){
f=node[c].s;
break;
}
q.push(node[c]);
}
else if(i==&&q.front().x>&&b[][q.front().y]==){
b[][q.front().y]=;
node[++c].x=;
node[c].y=q.front().y;
node[c].s=q.front().s+;
node[c].c=c;
node[c].f=q.front().c;
node[c].d=;
if(node[c].x==z||node[c].y==z){
f=node[c].s;
break;
}
q.push(node[c]);
}
else if(i==&&q.front().y>&&b[q.front().x][]==){
b[q.front().x][]=;
node[++c].x=q.front().x;
node[c].y=;
node[c].s=q.front().s+;
node[c].c=c;
node[c].f=q.front().c;
node[c].d=;
if(node[c].x==z||node[c].y==z){
f=node[c].s;
break;
}
q.push(node[c]);
}
else if(i==&&q.front().x>&&q.front().y<y){
int tx=q.front().x<y-q.front().y?:q.front().x+q.front().y-y;
int ty=q.front().x<y-q.front().y?q.front().x+q.front().y:y;
if(b[tx][ty]==){
b[tx][ty]=;
node[++c].x=tx;
node[c].y=ty;
node[c].s=q.front().s+;
node[c].c=c;
node[c].f=q.front().c;
node[c].d=;
if(node[c].x==z||node[c].y==z){
f=node[c].s;
break;
}
q.push(node[c]);
}
}
else if(i==&&q.front().x<x&&q.front().y>){
int tx=x-q.front().x<q.front().y?x:q.front().x+q.front().y;
int ty=x-q.front().x<q.front().y?q.front().x+q.front().y-x:;
if(b[tx][ty]==){
b[tx][ty]=;
node[++c].x=tx;
node[c].y=ty;
node[c].s=q.front().s+;
node[c].c=c;
node[c].f=q.front().c;
node[c].d=;
if(node[c].x==z||node[c].y==z){
f=node[c].s;
break;
}
q.push(node[c]);
}
}
}
if(f!=) break;
q.pop();
}
if(f==) printf("impossible\n");
else{
printf("%d\n",f);
for(i=;i<=f;i++){
re[i]=node[c].d;
c=node[c].f;
}
for(i=f;i>=;i--){
if(re[i]==) printf("FILL(1)\n");
else if(re[i]==) printf("FILL(2)\n");
else if(re[i]==) printf("DROP(1)\n");
else if(re[i]==) printf("DROP(2)\n");
else if(re[i]==) printf("POUR(1,2)\n");
else if(re[i]==) printf("POUR(2,1)\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)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  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. 算法排序-lowB三人组

    冒泡排序思路: 选择排序思路: 插入排序思路: 小结: 详细代码解释看下一篇

  2. Vue中data重置问题

    Object.assign() Object.assign()方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. 目标对象有1个,后边可以有多个源对象.注意他只会拷贝源对 ...

  3. Apache JServ Protocol

    ajp_百度百科 https://baike.baidu.com/item/ajp/1187933 AJP(Apache JServ Protocol)是定向包协议.因为性能原因,使用二进制格式来传输 ...

  4. [Java多线程] volatile 关键字正确使用方法

    volatile 变量具有 synchronized 的可见性特性,但是不具备原子特性,即多线程环境中,使用 volatile 关键字的变量仅可以保证不同线程读取变量时,可以读到最新修改的变量值,但是 ...

  5. appium(6)-parts of appium api

    parts of appium api Lock Lock the screen.//锁屏. // java driver.lockScreen(3); // objective c [driver ...

  6. Java 获取当前日期和时间

    原文地址:http://www.blogjava.net/parable-myth/archive/2013/01/17/394364.html 有三种方法: 方法一:用java.util.Date类 ...

  7. Mac终端操作SVN指令

    1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录)   例如:svn checkout svn://192.168.1.1/pro/domain    ...

  8. POJ1006 Biorhythms —— 中国剩余定理

    题目链接:https://vjudge.net/problem/POJ-1006 Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total ...

  9. ModuleNotFoundError: No module named 'numpy.core._multiarray_umath' ImportError: numpy.core.multiarray failed to import

      出现以下错误:可能是因为你的numpy版本太低 更新numpy的版本 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgra ...

  10. BZOJ 1621 [Usaco2008 Open]Roads Around The Farm分岔路口:分治 递归

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1621 题意: 约翰的N(1≤N≤1,000,000,000)只奶牛要出发去探索牧场四周的土 ...