题意:有两个容量互质的容器,需要用这两个容器量出目标重量的水,找到其中一组解。
bfs,使得搜索得到的解是步数最少的,遍历前驱法输出路径~
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
struct node {
int a,b;
int pre;
int flag;
}Node[maxn];
int last;
int p;
int ca,cb,n;
char s[][] = {"fill A","fill B","empty A","empty B","pour A B","pour B A"};
unordered_map<int,int> pos;
void dfs (int x) {
if (Node[x].pre==-) return;
dfs (Node[x].pre);
printf ("%s\n",s[Node[x].flag]);
}
void bfs (int a,int b) {
if (p>last) return;
if (a==n||b==n) return;
if (pos[a]!=b+) {
pos[a]=b+;
if (a<ca) {
Node[++last].a=ca;
Node[last].b=b;
Node[last].flag=;
Node[last].pre=p;
}
if (b<cb) {
Node[++last].a=a;
Node[last].b=cb;
Node[last].flag=;
Node[last].pre=p;
}
if (a!=) {
Node[++last].a=a;
Node[last].b=b;
Node[last].flag=;
Node[last].pre=p;
}
if (b!=) {
Node[++last].a=a;
Node[last].b=;
Node[last].flag=;
Node[last].pre=p;
}
if (a!=&&b<cb) {
Node[++last].a=max(,a+b-cb);
Node[last].b=a+b-Node[last].a;
Node[last].flag=;
Node[last].pre=p;
}
if (b!=&&a<ca) {
Node[++last].b=max(,a+b-ca);
Node[last].a=a+b-Node[last].b;
Node[last].flag=;
Node[last].pre=p;
}
}
p++;
bfs(Node[p].a,Node[p].b);
}
int main () {
Node[].pre=-;
while (~scanf("%d %d %d",&ca,&cb,&n)) {
p=;
last=;
pos.clear();
bfs(,);
dfs(p);
printf ("success\n");
}
return ;
}

ZOJ1005 Jugs的更多相关文章

  1. poj1066 Jugs

    poj1066 Jugs http://poj.org/problem?id=1606 解题思路:本题可以用数学方法解得,最易理解,常规的解法是搜索.直接用接近模拟的广度优先搜索即可过. 给两个容器, ...

  2. ZOJ 1005:Jugs(思维)

    Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Har ...

  3. 【Acm】算法之美—Jugs

    题目概述:Jugs In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted wi ...

  4. Jugs(回溯法)

    ZOJ Problem Set - 1005 Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In ...

  5. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  6. 九度OJ 1147:Jugs(罐子) (模拟、游戏)

    时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:243 解决:200 题目描述: In the movie "Die Hard 3", Bruce Willis and ...

  7. UVA571 - Jugs(数论)

    UVA571 - Jugs(数论) 题目链接 题目大意:给你A和B的水杯.给你三种操作:fill X:把X杯里面加满水.empty X:把X杯中的水清空.pour X Y 把X的水倒入Y中直到一方满或 ...

  8. POJ 1606 Jugs

    Jugs Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4280   Accepted: 2533   Special Ju ...

  9. ZOJ 1005 Jugs(BFS)

    Jugs In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with th ...

随机推荐

  1. AcWing 842. 排列数字

    深搜的一道水题 https://www.acwing.com/problem/content/844/   正确输入输出 #include<bits/stdc++.h> using nam ...

  2. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  3. CentOS7网络配置:静态IP和DHCP

    1.打开并编辑网络配置文件:/etc/sysconfig/network-scripts/ifcfg-ens33 [root@localhost network-scripts]# vim ifcfg ...

  4. 小程序websocket用法

    // socket已经连接成功 var socketOpen = false // socket已经调用关闭function var socketClose = false // socket发送的消 ...

  5. maven一直加载2.0.0.M7 的 config server 失败

    之前学习的时候使用F版的SpringBoot管理项目依赖一直好好的,今天不知idea为何抽疯,一直加载失败,各种重启,清除,没用 只能像之前学习注册consul 时将F版的SpringBoot 改为G ...

  6. java的动态绑定和多态

    public class Shape { public void area() { System.out.println("各种形状的面积..."); } public stati ...

  7. 关于AutoCompleteTextView的用法:根据输入的自动匹配关键词

  8. Codeforces Round #604 (Div. 2)D(构造)

    构造,枚举起点,如果一个序列成立,那么将它reverse依然成立,所以两个方向(从小到大或从大到小)没有区别,选定一个方向进行探测,直到探测不到以后回头,只要所给数据能成立,那么能探测进去就能探测出来 ...

  9. py2

    函数相关的 # 生成器相关的# 例1 ll = sum(i for i in range(100000000)) #生成器占资源少 # 例2 def demo(): for i in range(4) ...

  10. HashSet HashMap 源码阅读笔记

    hashcode() 与 equals() 应一起重写,在HashMap 会先调用hash(key.hashcode()) 找到对应的entry数组位置 (一般初始是16,2^x,rehash后会翻倍 ...