ZOJ1005 Jugs
题意:有两个容量互质的容器,需要用这两个容器量出目标重量的水,找到其中一组解。
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的更多相关文章
- poj1066 Jugs
poj1066 Jugs http://poj.org/problem?id=1606 解题思路:本题可以用数学方法解得,最易理解,常规的解法是搜索.直接用接近模拟的广度优先搜索即可过. 给两个容器, ...
- ZOJ 1005:Jugs(思维)
Jugs Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In the movie "Die Har ...
- 【Acm】算法之美—Jugs
题目概述:Jugs In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted wi ...
- Jugs(回溯法)
ZOJ Problem Set - 1005 Jugs Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In ...
- 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)
今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...
- 九度OJ 1147:Jugs(罐子) (模拟、游戏)
时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:243 解决:200 题目描述: In the movie "Die Hard 3", Bruce Willis and ...
- UVA571 - Jugs(数论)
UVA571 - Jugs(数论) 题目链接 题目大意:给你A和B的水杯.给你三种操作:fill X:把X杯里面加满水.empty X:把X杯中的水清空.pour X Y 把X的水倒入Y中直到一方满或 ...
- POJ 1606 Jugs
Jugs Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4280 Accepted: 2533 Special Ju ...
- ZOJ 1005 Jugs(BFS)
Jugs In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with th ...
随机推荐
- C++-POJ3321-Apple Tree[数据结构][树状数组]
树上的单点修改+子树查询 用dfn[u]和num[u]可以把任意子树表示成一段连续区间,此时结合树状数组就好了 #include <set> #include <map> #i ...
- 516,base64的原理及优缺点
优点是可以加密,减少了http请求 缺点是需要消耗cpu进行编解码 适用于小图片 base的体积约为原图的4/3
- 解析python 生产/消费者模型实现过程
1.多线程实现 import threadingimport queueimport logginglogging.basicConfig(level = logging.INFO,format = ...
- gRPC in ASP.NET Core 3.x -- Protocol Buffer(3)更新消息类型
当你第一次定义Protocol Buffer的消息的时候,你肯定会给消息设定一套规则需求.但是随着时间的推进,你的业务可能会发生了变化,与此同时,你的Protocol Buffer消息类型的需求也会随 ...
- js加密(五)产品目录
1. url:http://www.300600900.cn/ 2. target: 3. 简单分析 偶然发现,这个网站的加密,和landChina加密一模一样,js函数的名字都一样...所以,只贴 ...
- [lua]紫猫lua教程-命令宝典-L1-01-05. if判断结构
L1[if]01. 简单的if判断结构 没什么说得 if得基本结构如下 xxx= ) then testlib.traceprint("1-100") ) then testlib ...
- iCCID激活终结,苹果iPhone卡贴机“辉煌”时代落幕
iPhone卡贴机,是一个神奇的存在.所谓的iPhone卡贴机,原本是"有锁机".它们通常是国外运营商的合约机,为了限制使用地域而"上锁",不能直接在国内 ...
- SniperOJ-as fast as you can-Writeup
SniperOJ-as fast as you can-Writeup 题目描述: 打开所给的题目地址 很容易可以看出这是一个简单的爬虫问题,爬去网页的信息并post,但在界面及网页源码中均没发现有价 ...
- Dart语言学习(四)Dart字符串
一.字符串的创建 使用 单引号,双引号 创建字符串 使用 三个引号或双引号 创建多行字符串 使用 r 创建原始 raw 字符串 String str1 = 'Hello';//"" ...
- js加密(八)新浪微博登录
1. url: https://weibo.com/ 2. target: 登录 3. 分析.由于需要填写验证码,本篇只分析破解用户名和密码加密部分,不做验证码识别. 3.1 老规矩,F12,随便输入 ...