Jugs(回溯法)
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.
You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.
A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are
fill A
fill B
empty A
empty B
pour A B
pour B A
success
where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.
You may assume that the input you are given does have a solution.
Input
Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.
Output
Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.
Sample Input
3 5 4
5 7 3
Sample Output
fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success 思路:用二元组(a,b)记录每一次操作后A和B的状态(gallon数),一个状态元组不能重复出现,不然会不断循环。 AC Code:
#include <iostream>
#include <map>
#include <cstdio> using namespace std; const int SZ = ;
int op[SZ];
//op is for "operation". 1:fill A,2:fill B,3:empty A,4:empty B,5:pour A B,6:pour B A,0:success
int ca, cb, n;
map<pair<int, int>, bool> tag; bool DFS(int a, int b, int k) //a和b分别是A和B的gallon数,k是op的下标
{
pair<int, int> p = make_pair(a, b);
if(tag[p]) return false;
tag[p] = true;
if(b == n)
{
op[k] = ;
return true;
}
if(a < ca) //fill A
{
op[k] = ;
if(DFS(ca, b, k + )) return true;
}
if(b < cb) //fill B
{
op[k] = ;
if(DFS(a, cb, k + )) return true;
}
if(a) //empty A
{
op[k] = ;
if(DFS(, b, k + )) return true;
}
if(b) //empty B
{
op[k] = ;
if(DFS(a, , k + )) return true;
}
if(a && b < cb) //pour A B
{
op[k] = ;
int ra, rb;
if(a > cb - b)
{
rb = cb;
ra = a - (cb - b);
}
else
{
ra = ;
rb = b + a;
}
if(DFS(ra, rb, k + )) return true;
}
if(b && a < ca) //pour B A
{
op[k] = ;
int ra, rb;
if(b > ca - a)
{
ra = ca;
rb = b - (ca - a);
}
else
{
rb = ;
ra = b + a;
}
if(DFS(ra, rb, k + )) return true;
}
return false;
} int main()
{
while(scanf("%d %d %d", &ca, &cb, &n) != EOF)
{
tag.clear();
bool flag = DFS(, , );
for(int i = ; op[i]; i++)
{
switch(op[i])
{
case :
puts("fill A");
break;
case :
puts("fill B");
break;
case :
puts("empty A");
break;
case :
puts("empty B");
break;
case :
puts("pour A B");
break;
case :
puts("pour B A");
break;
default:
break;
}
}
puts("success");
}
return ;
}
Jugs(回溯法)的更多相关文章
- 回溯法解决N皇后问题(以四皇后为例)
以4皇后为例,其他的N皇后问题以此类推.所谓4皇后问题就是求解如何在4×4的棋盘上无冲突的摆放4个皇后棋子.在国际象棋中,皇后的移动方式为横竖交叉的,因此在任意一个皇后所在位置的水平.竖直.以及45度 ...
- leetcode_401_Binary Watch_回溯法_java实现
题目: A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bot ...
- uva216 c++回溯法
因为题目要求最多8台电脑,所以可以枚举全排列,然后依次计算距离进行比较,枚举量8!=40320并不大,但这种方法不如回溯法好,当数据再大一些枚举就显得笨拙了,所以这个题我用回溯法做的,回溯有一个好处是 ...
- UVa 129 (回溯法) Krypton Factor
回溯法确实不是很好理解掌握的,学习紫书的代码细细体会. #include <cstdio> ]; int n, L, cnt; int dfs(int cur) { if(cnt++ == ...
- 实现n皇后问题(回溯法)
/*======================================== 功能:实现n皇后问题,这里实现4皇后问题 算法:回溯法 ============================= ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- HDU 2553 n皇后问题(回溯法)
DFS Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 八皇后问题-回溯法(MATLAB)
原创文章,转载请注明:八皇后问题-回溯法(MATLAB) By Lucio.Yang 1.问题描述 八皇后问题是十九世纪著名数学家高斯于1850年提出的.问题是:在8*8的棋盘上摆放8个皇后,使其不能 ...
- 使用回溯法求所有从n个元素中取m个元素的组合
不多说了,直接上代码,代码中有注释,应该不难看懂. #include <stdlib.h> #include <stdio.h> typedef char ELE_TYPE; ...
随机推荐
- FivePlus——团队展示
光耀101 <光耀101>是福州大学数计学院计算机专业推出的中国首部程序猿脱发养成节目.由张栋担任发起人,刘晨瑶.畅畅担任导师. 该节目召集了你猜多少位选手,通过任务.训练.考核,让选 ...
- HDU 5855 Less Time, More profit 最大权闭合子图
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5855 Less Time, More profit Time Limit: 2000/1000 MS ...
- Scrum团队成立及《构建之法》第六、七章读后感
5.Scrum团队成立 5.1 团队名称:喳喳 团队目标:突破渣渣 团队口号:吱吱喳喳 团队照: 5.2 角色分配 产品负责人: 112冯婉莹 Scrum Master: ...
- Ansible基础配置与常用模块使用
环境介绍: Ansible服务端IP:192.168.2.215 Ansible客户端IP:192.168.2.216.192.168.2.218.192.168.2.113 一.创建Ansibl ...
- 图文详解 IntelliJ IDEA 15 创建普通 Java Web 项目
第 1 部分:新建一个 Java Web Application 项目 File -> New -> Project…,请选择 Java EE 这个模块下的 Web Application ...
- Java基于Tomcat Https keytool 自签证书
本文大部分内容系转载,原文地址:https://www.cnblogs.com/littleatp/p/5922362.html keytool 简介 keytool 是java 用于管理密钥和证书的 ...
- ie浏览器升级的正确姿势
一.版本说明 1.当前IE浏览器分为一下几个版本:IE 6,IE 7,IE 8,IE 9,IE 10,IE 11 2.windows最高支持IE版本win xp:IE 8win 7 :IE 11win ...
- Hadoop RPC protocol description--转
原文地址:https://spotify.github.io/snakebite/hadoop_rpc.html Snakebite currently implements the followin ...
- poj1655 Balancing Act求树的重心
Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...
- BZOJ4735 你的生命已如风中残烛 【数学】
题目链接 BZOJ4735 题解 给定一个序列,有的位置为\(w_i - 1\),有的位置为\(-1\),问有多少种排列,使得任意前缀和非负? 我们末尾加上一个\(-1\),就是要保证除了末尾外的前缀 ...