You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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)
虽然不能打acm了,但是还是要补一下之前的坑。
直接按着状态广搜,标记这个状态出没出现过,出现过就不要向下扩展了。没出现就向下扩展。
代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#define ll long long
using namespace std;char ss[6][20]={"FILL(2)","FILL(1)","POUR(2,1)","POUR(1,2)","DROP(1)","DROP(2)"};
struct node{int a;int b;string s;node(int x,int y,string z){a=x,b=y,s=z;}
};
int a,b,c;inline void bfs()
{queue<node> q;map<pair<int,int>,bool> mp;q.push(node(0,0,""));mp[make_pair(0,0)]=0;while(q.size()){node bb=q.front();q.pop();if(bb.a==c||bb.b==c){printf("%d\n",bb.s.length());for(int i=0;i<bb.s.length();i++) {int x=bb.s[i]-'0';printf("%s\n",ss[x]);}return ;}if(mp[make_pair(bb.a,bb.b)]) continue;else mp[make_pair(bb.a,bb.b)]=1;if(bb.a<=a) {q.push(node(a,bb.b,bb.s+"1"));int x=a-bb.a;if(bb.b>0&&x){  if(x>=bb.b) q.push(node(bb.a+bb.b,0,bb.s+"2"));else q.push(node(a,bb.b-x,bb.s+"2"));}}if(bb.b<=b) {q.push(node(bb.a,b,bb.s+"0"));int x=b-bb.b;if(bb.a>0&&x){if(x>=bb.a) q.push(node(0,bb.b+bb.a,bb.s+"3"));else q.push(node(bb.a-x,b,bb.s+"3"));}}if(bb.a) q.push(node(0,bb.b,bb.s+"4"));if(bb.b) q.push(node(bb.a,0,bb.s+"5"));}printf("impossible\n");
}
int main()
{while(~scanf("%d%d%d",&a,&b,&c)){bfs();}return 0;
}

喜欢算法,不一定非要打比赛,当个乐趣消遣消遣也是不错的。
努力加油a啊,(o)/~

Pots POJ - 3414(bfs)相关推荐

  1. poj 2415(BFS)

     题意: 有一种游戏,共有三个piece(不妨称为棋子),棋盘是由N个点构成的完全图,边有颜色.每次可以移动一个棋子,移动时必须满足棋子走过的边的颜色和其他两个棋子之间的连边的颜色一致.求把三个棋 ...

  2. Find The Multiple POJ - 1426 (BFS)

    题目大意 给定一个整数,寻找一个只有0,1构成的十进制数使得这个数能够整除这个整数 解法 直接bfs第一位放入1,之后每一位放入1或者0 代码 #include <iostream> #i ...

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

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

  4. 深度优先搜索(DFS)与广度优先搜索(BFS)详解

    原文来自<挑战程序设计竞赛> 深度优先搜索(DFS)和宽度优先搜索(BFS)都是常见的搜索算法.在学习DFS和BFS之前,我们首先得知道递归函数的概念. 1. 递归函数 通俗地讲,一个函数 ...

  5. python扫雷 广度优先_Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper)...

    Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

  6. A - Expanding Rods POJ - 1905(二分)

    A - Expanding Rods POJ - 1905(二分) 题目 Problem Description When a thin rod of length L is heated n deg ...

  7. 三十二、图的创建深度优先遍历(DFS)广度优先遍历(BFS)

    一.图的基本介绍 为什么要有图 前面我们学了线性表和树 线性表局限于一个直接前驱和一个直接后继的关系 树也只能有一个直接前驱也就是父节点 当我们需要表示多对多的关系时, 这里我们就用到了图. 图的举例 ...

  8. Java实现算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)

    对算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)用Java实现其中的伪代码算法,案例也采用算法导论中的图. import java.util.ArrayList; import java ...

  9. 【洛谷】马的遍历--广度优先搜索(BFS)

    题目描述 传送门:https://www.luogu.com.cn/problem/P1443 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意 ...

最新文章

  1. java邮件教程_Java发送Email/邮件
  2. OS / linux / 互斥锁实现原理(futex)
  3. 北京航空航天大学计算机专业培养方案,北京航空航天大学计算机科学与技术专业...
  4. BZOJ3427 Poi2013 Bytecomputer
  5. 前端工程师技能之photoshop巧用系列扩展篇——自动切图
  6. 别再跟我提Excel了!这才是阿里大厂都在用的数据分析神器
  7. 使用yum更新补丁包
  8. Photoshop CS3 Dreamwaver教程集合
  9. 物业公众号推文范例_如何运营社区物业微信公众号
  10. ECS架构 Entitas-CSharp学习之路(三)
  11. CWE-通用弱点枚举简介
  12. 2021-2026年中国畜牧业发展环境分析及投资前景预测报告
  13. 坚持#第369天~知道了惠普打印机和佳能打印机打印不清晰了怎么解决
  14. 浏览器-清理页面中js的缓存
  15. 验证手机号码 (包含166和199)
  16. Esri与欧盟委员会签订许可协议
  17. Python初学心得体会
  18. qc35 说明书_BOSE QC35
  19. Oracle入门笔记(二)——SQL Developer的基本使用
  20. filePath not found of @umijs/renderer-react/node_modules/@types/react

热门文章

  1. IOS15 SVProgressHUD 报UIWindow 无法识别错误
  2. Swift5版本以上#selector报错解决
  3. mysql的联表查询和去重复数据
  4. springboot banner在线生成_SpringBoot系列教程10--小花样之SpringBoot配置自定义Banner
  5. php远程连接403,php中出现“ HTTP 异常 403 - 禁止访问”解决方法 总结
  6. centos7开启tcp6_Centos7下配置IPV6
  7. Android关于Handler发送消息里面的arg1和arg2以及obj和what的用法
  8. html 自动滚动标签,HTML滚动标签(marquee标签)
  9. vc连接mysql 查询_vc连接数据库中查询代码如何写呀 急急急!!!!!!
  10. 计算a b python_你知道Python中a = b和a = a b的结果是不一样的吗?