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; ...
随机推荐
- 总结在Visual Studio Code创建Node.js+Express+handlebars项目
一.安装node.js环境. Node.js安装包及源码下载地址为:https://nodejs.org/en/download/ 32 位安装包下载地址 : https://nodejs.org/d ...
- dRMT: Disaggregated Programmable Switching
dRMT: Disaggregated Programmable Switching 2017年SIGCOMM会议上提出的新型可编程交换机架构,对2013年提出的RMT架构存在的问题进行了优化. 主要 ...
- Python学习 - 入门篇2(更新中)
前言 学习渠道:慕课网:Python进阶 记录原因:我只是想边上课边做笔记而已,呵呵哒 食用提示:教程环境基于Python 2.x,有些内容在Python 3.x中已经改变 函数式编程 定义:一种抽象 ...
- 不要USB数据线调试Android开发
不管是过去Eclipse还是现在的Android Studio开发Android,运行或者调试时都会利用USB数据线连接电脑和手机,特别是当现在的手机只有一个Type-c接口,意味着,插上后,啥也干不 ...
- HTML5+规范:Webview的使用详解
一.知识点 Webview模块管理应用窗口界面,实现多窗口的逻辑控制管理操作.通过plus.webview可获取应用界面管理对象. 1.方法 1.1.all: 获取所有Webview窗口 Array[ ...
- 安装libvirt管理套件(C/S架构模式,用户管理kvm虚拟机)
# 1:安装程序包 yum install -y libvirt virt-manager virt-viewer virt-install qemu-kvm # 2:启动libvirtd守护进程 ...
- CentOS yum 安装LAMP PHP5.4版本
CentOS yum 安装LAMP PHP5.4版本 [日期:2015-06-04] 来源:Linux社区 作者:rogerzhanglijie [字体:大 中 小] Linux系统版本:C ...
- sphinx配置 + php
1. 为什么要使用Sphinx 假设你现在运营着一个论坛,论坛数据已经超过100W,很多用户都反映论坛搜索的速度非常慢,那么这时你就可以考虑使用Sphinx了(当然其他的全文检索程序或方法也 ...
- django里的http协议
一个普通的user Begin########## ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__' ...
- 深入理解JVM一配置参数
一.JVM配置参数分为三类参数: 1.跟踪参数 2.堆分配参数 3.栈分配参数 这三类参数分别用于跟踪监控JVM状态,分配堆内存以及分配栈内存. 二.跟踪参数 跟踪参数用于跟踪监控JVM,往往被开发人 ...