算法笔记练习 8.2 广度优先搜索(BFS) 问题 A: Jugs

算法笔记练习 题解合集

本题链接

题目

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

思路

0. 写在前面

这个题我用广度优先搜索去做,怎么写都 AC 不了。截止我提交的时候,在 codeup 上面的提交数是 649,通过数是丧心病狂的 14,于是我找了一个描述一样的题目,也就是 ZOJ 1005 Jugs,代码一贴瞬间 AC。

猜测 codeup 上的输入数据存在多解的可能性,并且输出数据的思路不是 BFS(就算是 BFS,六种操作的优先级和题面给的顺序肯定也不一样)。总之我把 ZOJ 上 AC 了的代码贴一下,思路也照着 BFS 来写,codeup 就不管了。

1. BFS 的主要思路

如果用 BFS,队列是一定要用的,先把初始情况{0, 0}入队,然后只要队列非空,就访问队首元素,并将队首元素的可能发展情况入队,这里的可能指的是:

  1. 操作前后状态要有变化,比如假设杯子 A 中没有水,empty A就是个非法操作;
  2. 操作后的状态必须是没有出现过的状态,不然就会无限循环,比如起始状态{0, 0}fill A之后的操作不能是empty A。那就必须记录下之前出现过的状态,怎么做呢?看下一小节。

2. 记录操作路径path

因为题目要求输出所有操作的过程,所以就需要在 BFS 过程中,对于每一个状态都要记录所有的历史操作,用映射是最方便的。

用字符'1'到字符'6'来表示六种操作,用字符串表示操作过程。那么映射的定义应该写成map<pair<int, int>, string> path;

用映射的好处是可以很方便地继承队首元素的历史路径,比如对于队首元素top,如果你找到了其后的一个合法操作'2',操作后的状态是after,那么就插入新的映射path[after] = path[top] + '2';

这同时也解决了上一个小节的问题,我怎么知道现在的这个状态now是否出现过呢?在path里查询一下即可:path.count(now) == 0代表没有出现过。

代码

#include <iostream>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

int goal;
pair<int, int> jug;
queue<pair<int, int> > Q;
map<pair<int, int>, string> path;

inline void doSomething(pair<int, int> whatever, pair<int, int> top, char no) {
	Q.push(whatever);
	path[whatever] = path[top] + no;
} 

void BFS(pair<int, int> jug) {
	path.clear(); 
	path[ {0, 0} ] = "";
	while (!Q.empty())
		Q.pop(); 
	Q.push( {0, 0} );
	while (!Q.empty()) {
		if (Q.front().second == goal) {
			string output = path[Q.front()];
			for (auto ch : output) {
				ch == '1' ? puts("fill A") :
				ch == '2' ? puts("fill B") :
				ch == '3' ? puts("empty A") :
				ch == '4' ? puts("empty B") :
				ch == '5' ? puts("pour A B") :
				ch == '6' ? puts("pour B A") : 0;
			} 
			puts("success");
			return;
		} 
		pair<int, int> top = Q.front();
		Q.pop();
		if (top.first < jug.first && path.count( {jug.first, top.second} ) == 0)
			doSomething( {jug.first, top.second}, top, '1');
		if (top.second < jug.second && path.count( {top.first, jug.second} ) == 0)
			doSomething( {top.first, jug.second}, top, '2');
		if (top.first > 0 && path.count( {0, top.second} ) == 0)
			doSomething( {0, top.second}, top, '3');
		if (top.second > 0 && path.count( {top.first, 0} ) == 0)
			doSomething( {top.first, 0}, top, '4');
		if (top.first > 0 && top.second < jug.second) {
			int pour = min(jug.second - top.second, top.first);
			if (path.count( {top.first - pour, top.second + pour} ) == 0)
				doSomething( {top.first - pour, top.second + pour}, top, '5');
		}
		if (top.first < jug.first && top.second > 0) {
			int pour = min(jug.first - top.first, top.second);
			if (path.count( {top.first + pour, top.second - pour} ) == 0)
				doSomething( {top.first + pour, top.second - pour}, top, '6');
		} 
	} 
}

int main() {
	while (scanf("%d%d%d", &jug.first, &jug.second, &goal) != EOF)
		BFS(jug);
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值