ZOJ Problem Set - 1005
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(回溯法)的更多相关文章

  1. 回溯法解决N皇后问题(以四皇后为例)

    以4皇后为例,其他的N皇后问题以此类推.所谓4皇后问题就是求解如何在4×4的棋盘上无冲突的摆放4个皇后棋子.在国际象棋中,皇后的移动方式为横竖交叉的,因此在任意一个皇后所在位置的水平.竖直.以及45度 ...

  2. 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 ...

  3. uva216 c++回溯法

    因为题目要求最多8台电脑,所以可以枚举全排列,然后依次计算距离进行比较,枚举量8!=40320并不大,但这种方法不如回溯法好,当数据再大一些枚举就显得笨拙了,所以这个题我用回溯法做的,回溯有一个好处是 ...

  4. UVa 129 (回溯法) Krypton Factor

    回溯法确实不是很好理解掌握的,学习紫书的代码细细体会. #include <cstdio> ]; int n, L, cnt; int dfs(int cur) { if(cnt++ == ...

  5. 实现n皇后问题(回溯法)

    /*======================================== 功能:实现n皇后问题,这里实现4皇后问题 算法:回溯法 ============================= ...

  6. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  7. HDU 2553 n皇后问题(回溯法)

     DFS Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description ...

  8. HDU 1016 Prime Ring Problem (回溯法)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. 八皇后问题-回溯法(MATLAB)

    原创文章,转载请注明:八皇后问题-回溯法(MATLAB) By Lucio.Yang 1.问题描述 八皇后问题是十九世纪著名数学家高斯于1850年提出的.问题是:在8*8的棋盘上摆放8个皇后,使其不能 ...

  10. 使用回溯法求所有从n个元素中取m个元素的组合

    不多说了,直接上代码,代码中有注释,应该不难看懂. #include <stdlib.h> #include <stdio.h> typedef char ELE_TYPE; ...

随机推荐

  1. FivePlus——成果展示

    思路描述:描述对于自己此次任务是如何思考的 这次作业没能帮上什么忙,刚开始还对这次作业有所期待,然而,第一次听他们讨论的时候就??? 之后又去查了一些诸如贪吃蛇类的小游戏,知道大概可以达成什么效果,但 ...

  2. mysql---增删用户

    Mysql创建.删除用户 MySql中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个  ;  表示一个命令语句结束): 1.新建用户 登录MYSQL: @>mysql - ...

  3. mnist测试

    第一步:进入caffe目录 第二步:获取mnist数据集 ./data/mnist/get_mnist.sh 第三步:创建lmdb ./examples/mnist/create_mnist.sh 第 ...

  4. FastReport.Net 无限页高(连续纸小票)

    using System; using System.Collections; using System.Collections.Generic; using System.ComponentMode ...

  5. MySQL专题 2 数据库优化 Slow Query log

    MySQL Server 有四种类型的日志——Error Log.General Query Log.Binary Log 和 Slow Query Log. 第一个是错误日志,记录 mysqld 的 ...

  6. MAC 下用 brew 搭建 PHP 开发环境

    Mac下用brew搭建PHP(LNMP/LAMP)开发环境 Mac下搭建lamp开发环境很容易,有xampp和mamp现成的集成环境.但是集成环境对于经常需要自定义一些配置的开发者来说会非常麻烦,而且 ...

  7. JMeter脚本强化之检查点

    上一篇讲述了对脚本做参数化的两种方法,并对参数化设置结果做了简单的验证,就是通过添加断言.本篇将详细一点介绍怎么使用断言做文本检查,或者叫做设置检查点. 首先来看看下面的三个图,这三个图是用查看结果树 ...

  8. python脚本批量生成50000条插入数据的sql语句

    f = open("xx.txt",'w') for i in range(1,50001): str_i = str(i) realname = "lxs"+ ...

  9. 【loj6342】跳一跳 期望dp

    题目描述 一个人从 $1$ 开始向 $n$ 跳,在 $i$ 时会等概率跳到 $i,i+1,...,n$ 之一.求从 $1$ 跳到 $n$ 的期望步数. $n\le 10^7$ . 题解 期望dp傻逼题 ...

  10. Expect the Expected UVA - 11427(概率dp)

    题意: 每天晚上你都玩纸牌,如果第一次就赢了,就高高兴兴的去睡觉,如果输了就继续玩.假如每盘游戏你获胜的概率都为p,每盘游戏输赢独立.如果当晚你获胜的局数的比例严格大于p时才停止,而且每天晚上最多只能 ...