一个搜索,求bug

cstur4 2012-10-27 08:05:24
from http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1005

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

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 10
int ca, cb, cn;
int mask[1000][1000];
bool dfs(int a, int b, int count){

if(b == cn)
return true;
if(count >= MAX)
return false;
if(mask[a][b])
return false;
mask[a][b] = 1;
if(a<ca && dfs(ca, b, count+1))
printf("fill A\n");
else if(b<cb && dfs(a, cb, count+1))
printf("fill B\n");
else if(a>0 && dfs(0, b, count+1))
printf("empty A\n");
else if(b>0 && dfs(a, 0, count+1))
printf("empty B\n");
else if(a>=cb-b && dfs(a-(cb-b), cb, count+1))
printf("pour A B\n");
else if(a<cb-b && dfs(0, b+a, count+1))
printf("pour A B\n");
else if(b>=ca-a && dfs(ca, b-(ca-a), count+1))
printf("pour B A\n");
else if(b<ca-a && dfs(a+b, 0, count+1))
printf("pour B A\n");
mask[a][b] = 0;
return false;

}
int main(){

freopen("in.txt", "r", stdin);
while(cin>>ca>>cb>>cn){
memset(mask, 0, sizeof(mask));
if(dfs(0, 0, 1))
printf("success\n");
}
fclose(stdin);
}
...全文
180 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cstur4 2012-11-06
  • 打赏
  • 举报
回复
短路年年有,今年特别多。以防后人搜索到该贴,附上代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<memory.h>
#include<algorithm>
using namespace std;
#define MAX 100
int ca, cb, cn;
int mask[1000][1000];
vector<string> op;
bool flag;
bool dfs(int a, int b, int count){

	if(flag)
		return true;
	if(b == cn){
		for(int i=0;i<op.size();++i){
			cout<<op[i]<<endl;
		}
		flag = true;
		return true;
	}
	if(count >= MAX)
		return false;
	if(mask[a][b])
		return false;
	mask[a][b] = 1;

	if(a<ca){
		op.push_back("fill A");
		dfs(ca, b, count+1);
		op.pop_back();
	}
	if(b<cb){
		op.push_back("fill B");
		dfs(a, cb, count+1);
		op.pop_back();
		
	}
	if(a>0){
		op.push_back("empty A");
		dfs(0, b, count+1);
		op.pop_back();
	}
	if(b>0){
		op.push_back("empty B");
		dfs(a, 0, count+1);
		op.pop_back();
	}
	if(a>=cb-b){
		op.push_back("pour A B");
		dfs(a-(cb-b), cb, count+1);
		op.pop_back();
	}
	if(a<cb-b){
		op.push_back("pour A B");
		dfs(0, b+a, count+1);
		op.pop_back();

	}
	if(b>=ca-a){
		op.push_back("pour B A");
		dfs(ca, b-(ca-a), count+1);
		op.pop_back();
	}
	if(b<ca-a){
		op.push_back("pour B A");
		dfs(a+b, 0, count+1);
		op.pop_back();
	}
	mask[a][b] = 0;
	return false;

}
int main(){

	//freopen("in.txt", "r", stdin);
	while(cin>>ca>>cb>>cn){
		memset(mask, 0, sizeof(mask));
		flag = false;
		dfs(0, 0, 1);
		if(flag)
			printf("success\n");
	}
	//fclose(stdin);
}

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧