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. 网页编程-django前传

    1.js正则表达式  http://www.cnblogs.com/wupeiqi/articles/5602773.html test  - 判断字符串是否符合规定的正则 正则表达式: rep = ...

  2. 为什么在 Java 中用 (low+high)>>>1 代替 (low+high)/2 或 (low+high)>>1 来计算平均值呢?好在哪里?

    >>>与>>是位运算符,只对整型有效(不能用于浮点型).当是整型的时候(low+high)>>1可以代替(low+high)/2.>>>是无 ...

  3. RestTemplate请求

    JSONObject json = new JSONObject(sendParam);HttpHeaders headers = new HttpHeaders();MediaType type = ...

  4. 嵌入式开发之cgic库---cgi库的使用

    很幸运!用C语言写CGI程序还可以有比较简单的方式,那就是我们可以借助使用第三方库CGIC(CGIC是一个功能比较强大的支持CGI开发的标准C库,并支持Linux, Unix 和Windows等多操作 ...

  5. EasyDarwin开源摄像机访问EasyCamera中海康摄像头语音对讲和云台控制转发实现

    转自:http://blog.csdn.net/yanzhuomin/article/details/52887311 EasyCamera中关于摄像头SDK的调用都集中在EasyCameraSour ...

  6. git 的安装

    git在开发中已经成了必备工具了,我们来看看git在各个平台上的安装 1.Linux上安装git $sudo apt-get install git 2.mac上安装 1)homebrew安装git ...

  7. [2017-10-26]Abp系列——DTO入参验证使用方法及经验分享

    本系列目录:Abp介绍和经验分享-目录 声明式的入参验证逻辑 声明式入参验证主要使用了System.ComponentModel.DataAnnotations中提供的各种验证参数的Attribute ...

  8. Linux-正则表达式学习(精)

    正则表达式30分钟入门教程 来园子之前写的一篇正则表达式教程,部分翻译自codeproject的The 30 Minute Regex Tutorial. 由于评论里有过长的URL,所以本页排版比较混 ...

  9. nginx启动不了

    nginx简介 Nginx是一个高性能的HTTP和反向代理服务器. 支持的操作系统众多,windows.linux. MacOS X: 可实现负载均衡: Rewrite功能强大: 电商架构大部分都采用 ...

  10. 随机数生成程序代码( 伪随机<stdlib.h> )

    #include <stdio.h> #include <string> #include <stdlib.h> #include <algorithm> ...