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 ≤ i ≤ 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 A, B, 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)

问题简述:就是简单的模拟倒水问题,两个杯子有六种情况:fill(1),fill(2),drop(1),drop(2),pour(1,2),pour(2,1)   用bfs分别讨论入队列即可。一开始一直impossible,没注意到在pour的时候先改变x的值后面改变y值时会有影响,记录路径用一个字符数组记         录。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;const int inf=9999999;
int vis[105][105];
int a,b,c;
int flag;struct node
{int x,y,step;char move[110][10];
};
queue<node>Q;node bfs()
{int t;node tmp,cur;cur.x = 0;cur.y = 0;cur.step = 0;vis[cur.x][cur.y] = 1;Q.push(cur);while(!Q.empty()){cur = Q.front();Q.pop();if (cur.x==c || cur.y==c){flag = 1;return cur;}//fill 1tmp = cur;tmp.x = a;if (vis[tmp.x][tmp.y]==0){vis[tmp.x][tmp.y] = 1;strcpy(tmp.move[tmp.step],"FILL(1)");tmp.step++;Q.push(tmp);} //fill 2tmp = cur;tmp.y = b;if (vis[tmp.x][tmp.y]==0){vis[tmp.x][tmp.y] = 1;strcpy(tmp.move[tmp.step],"FILL(2)");tmp.step++;Q.push(tmp);} //drop 1tmp = cur;tmp.x = 0;if (vis[tmp.x][tmp.y]==0){vis[tmp.x][tmp.y] = 1;strcpy(tmp.move[tmp.step],"DROP(1)");tmp.step++;Q.push(tmp);} //drop 2tmp = cur;tmp.y = 0;if (vis[tmp.x][tmp.y]==0){vis[tmp.x][tmp.y] = 1;strcpy(tmp.move[tmp.step],"DROP(2)");tmp.step++;Q.push(tmp);}//pour(1,2)tmp = cur;if (b-tmp.y>tmp.x){t = tmp.x;tmp.x = 0;tmp.y += t;}else if(b-tmp.y<=tmp.x){tmp.x -= (b-tmp.y);tmp.y = b;}if (vis[tmp.x][tmp.y]==0){vis[tmp.x][tmp.y] = 1;strcpy(tmp.move[tmp.step],"POUR(1,2)");tmp.step++;Q.push(tmp);  }//pour(2,1)tmp = cur;if (a-tmp.x>tmp.y){tmp.x += tmp.y;tmp.y = 0;}else if (a-tmp.x<=tmp.y){t = tmp.x;tmp.x = a;tmp.y -= (a-t);}if (vis[tmp.x][tmp.y]==0){vis[tmp.x][tmp.y] = 1;strcpy(tmp.move[tmp.step],"POUR(2,1)");tmp.step++;Q.push(tmp); }}
} int main()
{while(~scanf("%d%d%d",&a,&b,&c)){node ans;memset(vis,0,sizeof(vis));flag = 0;ans = bfs();if (flag){printf("%d\n",ans.step);for(int i=0; i<ans.step; i++)printf("%s\n",ans.move[i]);}elseprintf("impossible\n");}return 0;
}

Pots (模拟倒水)相关推荐

  1. 365. 水壶问题-暴力模拟倒水过程-递归法

    365. 水壶问题-暴力模拟倒水过程-递归法 有两个水壶,容量分别为 jug1Capacity 和 jug2Capacity 升.水的供应是无限的.确定是否有可能使用这两个壶准确得到 targetCa ...

  2. POJ 3414 Pots(深搜并打印路径)

    POJ 3414 Pots(深搜并打印路径) You are given two pots, having the volume of A and B liters respectively. The ...

  3. ICPC程序设计题解书籍系列之九:罗勇军《算法竞赛入门到进阶》

    罗书<算法竞赛入门到进阶>题目一览 第1章 算法竞赛概述 HDU1000 HDU1089-HDU1096 A+B for Input-Output Practice (I)-(VIII)( ...

  4. 【搜索】搜刷刷题整理

    包含洛谷和vjudgekuangbin题目(慢慢更新中) 洛谷 1.马的遍历 题目链接:https://www.luogu.com.cn/problem/P1443 题意:给你一个n×m的棋盘,然后给 ...

  5. L - 非常可乐——HDU-1495(bfs->优化bfs->数论)

    文章目录 非常可乐 第一种解题方案(最麻烦但最通俗易懂的): 第二种解题方案(优化bfs,减少不必要的计算) 推理: 优化: 第三种解题方案(数论) 非常可乐 原题链接:http://acm.hdu. ...

  6. H - Pots POJ - 3414(两个锅互相倒水)

    H - Pots POJ - 3414 题意: (两个锅互相倒水)希望能通过题目中的几种操作使一个锅中的水量为目标水量 bfs 搜索每一步的每种操作,直到所有操作用完,则impossible,否则输出 ...

  7. Pots 用bfs模拟2杯子体积分别为A,B通过一系列操作把其中一杯子变为C升水

    Pots POJ - 3414 给你两个罐子, 分别有A升和B升的体积 这里开始的时候A和B的杯子都是没有水的 .可以执行以下操作: 1,FILL(i)从水龙头填充锅i (1 ≤ i ≤ 2): 2, ...

  8. 用matlab绘制克莱因瓶,克莱因瓶怎么装水动态四维模拟图 克莱因瓶为什么装不满怎么倒水...

    很多人和小编一样提到数理化就不自觉地发憷,但其实几何学中有很多奇特的现象非常耐人寻味,就连觉得自己是物理小白的人都会产生探究的想法,而今天要说到的克莱因瓶,就因为其独特的造型让人十分好奇,究竟它存在的 ...

  9. POJ3414 Pots —— BFS + 模拟

    题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

最新文章

  1. 终于等到你:CYQ.Data V5系列 (ORM数据层)最新版本开源了
  2. PHP5.6版本“No input file specified”问题
  3. nginx的502问题
  4. php larval开发规范,数据模型 |《 Laravel 项目开发规范 5.5》| Laravel China 社区
  5. 常用的C#正则表达式! [转]
  6. java由谁创建_透析Java本质-谁创建了对象,this是什么
  7. 模拟,贪心,枚举(二)
  8. Win10微软帐户切换不回Administrator本地帐户的解决方法【亲测】
  9. Python基础__函数
  10. FastCGI 工作原理
  11. UIKeyboardType键盘
  12. 烂泥: KVM虚拟机Linux系统增加硬盘
  13. 七个常见队列的简单学习
  14. 浏览器插件Octotree的下载安装
  15. CEIWEI CommMonitor 串口监控精灵v12.0 串口过滤;串口监控;Serial port monitor tools
  16. Android跨进程通信--AIDL原理解析
  17. 基于Android企业员工绩效考评APP的设计与实现
  18. ip经济ip猫:「飞海豚」大圣基因/医修鸽的ValueUP | Chain++
  19. Kettle-时间维度的生成
  20. IBM X3650服务器使用说明

热门文章

  1. Android关于虚拟控件、全面屏及悬浮球机型适配时遇到的问题
  2. 城市编码映射json格式
  3. VRay 3.4 for SketchUp 写实室内渲染的10个小技巧
  4. 手机网络专业测试软件,3个专业网速测试APP,免费无广告
  5. Linux脚本输出99乘法表,利用shell脚本各种循环语句输出九九乘法表
  6. WIN7下搭建CORDOVA环境
  7. java大麦_大麦大 - SegmentFault 思否
  8. [读书笔记] 区块链:从数字货币到信用社会
  9. python tkinter canvas 画心形
  10. 淮阴工学院C语言考试题库,淮阴工学院C语言题库练习题1.ppt