算法

POJ1606总结

Jugs

题目来源

Description

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 

我的代码

这一题不禁让人想起在《锈湖:旅馆》(Rusty Lake: Hotel)中杀死鹿先生的场景,当时给出了容量为10、6、5的三个烧杯和11单位的水,要配出体积为8单位的水。

201755331541485454

当然和这一题还是有些差距的:首先本题只有两个水壶,且允许倒空水壶或者倒满水壶,难度上当然还是没有锈湖的大。

本题就是一个简单的宽搜,只不过要多处理一下记录路径,并且每次有六种可操作的情况,所以实现起来有些繁琐。

需要设置一个数组flag[i][j]记录当a中有i的水且b中有j的水的情况有没有被试探过,如果试探过就不需要再次试探了。

#include<stdio.h>
#include<vector>
#include<stack>
#include<string.h>

using namespace std;

//定义操作步骤
const int fullA=1,fullB=2,emptyA=3,emptyB=4,pourAtoB=5,pourBtoA=6;
const char steps[10][10]={"success","fill A","fill B","empty A","empty B","pour A B","pour B A"};

int a,b,n;
int flag [1000][1000];

struct node{
	int a,b,now,pre;//a,b是两个水壶当前水的体积,now是这一步的操作步骤,pre是上一步的索引
	node(){}
	node(int a,int b,int now,int pre):a(a),b(b),now(now),pre(pre){}
};
int main()
{
	while (scanf("%d%d%d",&a,&b,&n)==3){
		if (n==0){
			printf("%s\n%s\n",steps[emptyB],steps[0]);
			continue;
		}
		memset(flag,0,sizeof(flag));
		int head=0,tail=1;
		int sum,tempa,tempb;
		int end;
		node now;
		vector<node> v;
		stack<int> s;
		v.push_back(node(0,0,0,-1));
		flag[0][0]=1;
		while (head<tail){
			now=v[head];
			//fullA 
			if (flag[a][now.b]==0){
				v.push_back(node(a,now.b,fullA,head));
				flag[a][now.b]=1;
				tail++;
			}
			//fullB
			if (flag[now.a][b]==0){
				v.push_back(node(now.a,b,fullB,head));
				flag[now.a][b]=1;
				if ((v[tail]).b==n){
					end =tail;
					break;
				}
				tail++;
			}
			//emptyA
			if (flag[0][now.b]==0){
				v.push_back(node(0,now.b,emptyA,head));
				flag[0][now.b]=1;
				tail++;
			}
			//emptyB
			if (flag[0][now.b]==0){
				v.push_back(node(now.a,0,emptyB,head));
				flag[now.a][0]=1;
				tail++;
			}
			sum=now.a+now.b;
			//pourAtoB
			{
				if (sum>b){
					tempb=b;
					tempa=sum-b;
				}else{
					tempb=sum;
					tempa=0;
				}
				if (flag[tempa][tempb]==0){
					v.push_back(node(tempa,tempb,pourAtoB,head));
					flag[tempa][tempb]=1;
					if ((v[tail]).b==n){
						end =tail;
						break;
					}
					tail++;
				}
			}
			//pourBtoA
			{
				if (sum>a){
					tempa=a;
					tempb=sum-a;
				}else{
					tempa=sum;
					tempb=0;
				}
				if (flag[tempa][tempb]==0){
					v.push_back(node(tempa,tempb,pourBtoA,head));
					flag[tempa][tempb]=1;
					if ((v[tail]).b==n){
						end =tail;
						break;
					}
					tail++;
				}
			}
			head++;
		}
	
		while((v[end]).pre!=-1){
			s.push((v[end]).now);
			end=(v[end]).pre;
		}
		while(!s.empty()){
			printf("%s\n",steps[s.top()]);
			s.pop();
		}
		printf("%s\n",steps[0]);
	}
	return 0;
}