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; ...
随机推荐
- 团队Alpha冲刺(三)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最 ...
- NULL指针引起的一个linux内核漏洞
NULL指针一般都是应用于有效性检测的,其实这里面有一个约定俗成的规则,就是说无效指针并不一定是 NULL,只是为了简单起见,规则约定只要指针无效了就将之设置为NULL,结果就是NULL这个指针被用来 ...
- 对小组项目alpha发布的评价
第一组:新蜂小组 项目:俄罗斯方块 评论:看见同学玩的时候,感到加速下落时不是很灵敏,没有及成绩的功能,用户的界面仍在修正. 第二组:天天向上 项目:连连看 评论:这个游戏增加了很多好玩的功能,比如更 ...
- Java Map获取key和value 以及String字符串转List方法
一.问题描述 这里描述两个问题: 1.Java Map获取key和value的方法: 2.String字符串转List的方法: 二.解决方法 1.Java Map获取key和value的方法 2. ...
- CentOS卸载系统自带的OpenJDK并安装Sun的JDK的方法
查看目前系统的jdk: rpm -qa | grep jdk 得到的结果: [root@dc-01 java]# rpm -qa | grep jdk java-1.6.0-openjdk-1.6. ...
- 二分图匹配模板(dfs+bfs)
dfs版: bool dfs(int u) { for(int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; if(!vis[v]) { vis[ ...
- Golang的第一个程序-Hello, World !
安装Golang: 1. 下载安装包 https://golang.google.cn/dl/ 我这里使用压缩包,下载后解压到D盘(自定义). 2. 添加环境变量:把解压后的bin目录添加到环境变量中 ...
- TCP的拥塞控制 (三)
1. Multiple Packet Losses Fast Retransmit/Fast Recovery机制可以很好地处理单个packet丢失的问题,但当大量packet同时丢包时(一个RT ...
- Cycle Sort (交换次数最少的排序)
该算法的效率并不高.但是却提供了一个很好的思路.如何让一个序列在最小交换次数下实现有序. Cycle Sort 翻译成中文是 圈排序. 这个圈在于需要交换的数据形成圈. 具体一点: 如: Array ...
- sql语句查询各门课程平均分的最大值
解法一: select courseno,stuno,avg(score) '平均分最高值'--这里是求平均,后面的条件是过滤最大值的 from tablename group by courseno ...