一个搜索,求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);
}
...全文
172 1 打赏 收藏 转发到动态 举报
写回复
用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);
}
最新2010-11-20日发上来的,开源团购源码 ,团购网站源码,是商业版的,有安装说明,功能是目前团购网站程序最齐全 的,官方网站 www.cmstuan.com 开源团购后台功能相关说明与升级历史 1、用户界面:支持前台无刷新选择新界面; 2、系统扩展:完美整合UCenter插件,可与其他系统同步注册登录、统一头像; 3、系统扩展:集成GoogleMap,可后台设置、标记商家所在区域; 4、邮件验证:支持验证Email真实性后才可以登录等(后台系统定义中设置); 5、邮件通知:支持团购成功时自动Email通知等; 6、邮件通知:支持用户Email订阅、登录取消订阅等; 7、推广营销:支持用户前台获取推荐连接,并邀请好友购买、返现; 8、支付接口:默认集成支付宝和财付通接口;可后台设置,或添加银行转账信息等; 9、团购流程:支持生成唯一订单号(方便沟通)、可切换不同的支付方式; 10、自动开通:满足团购要时,系统自动开通相关订单、生成优惠券和随机密码; 11、帐户充值:可分批对账户进行充值再开通订单,方便对支付有限额的用户; 12、财务明细:全程记录资金的动作,即使直接开通订单,系统也会自动先充值再扣款; 13、放心消费:用户可在前台我的团购中看到每一笔资金信息和相关的备注; 14、消费保障:优惠券的消费需要密码,密码仅用户自己可见; 15、安全消费:即使知道了优惠券和密码,也只有对应的商家才可以消费; 16、合作商家:可前台登陆、查看自己的产品、优化券,并Email提醒用户消费; 17、产品消费:商家登陆后,输入优惠券和对应密码才可以消费(将优惠券变成已消费); 18、在线沟通:前台可在线提问、意见反馈和商务合作; 19、网站优化:可设置开启伪静态,Apache下系统支持自动创建伪静态规则; 20、网站优化:后台可开启Gzip压缩输出内容,加快页面打开速度; 21、网站安全:后台可设置过滤关键词内容,防止用户留言中包括违法内容; 22、IP管理 :可禁止指定IP的用户访问网站或者后台; 23、友情链接:支持后台添加文字或者图片友情链接; 24、关于我们:管理员可后台设置关于我们、联系我们、加入我们等信息; 25、互动管理:管理员后台可以回答问题,答复后的问题会在前台显示; 26、城市管理:可任意添加或修改城市、开启或关闭前台显示,可做地图标记; 27、商家管理:可列表查看商家旗下的产品数量、成功订单数和营业总额; 28、商家管理:可通过关键词查找商家,或直接查看某城市下的所有商家; 29、产品管理:添加产品时,支持选择城市自动选择其下商家,减少管理员操作; 30、产品管理:可列表查看团购产品的时间、价格、购买数、团购状态等; 31、产品管理:可一键查看成功团购产品的相关购买人、数量、时间和订单号; 32、订单管理:可列表查看每一订单的价格、购买数量、时间、购买人、支付状态等; 33、订单管理:对未支付的订单,可一键操作对该用户进行Email催款; 34、订单管理:对银行转账的订单,管理员可后台填写相关信息后,手工开通; 35、订单管理:可通过用户名、订单号、支付状态等查询订单; 36、订单管理:团购没达到指定人数,管理员可确认团购失败,并退款给用户; 37、团购券管理:可列表查看优惠券拥有人、到期时间,并发送Email到期提醒; 38、团购券管理:可通过团购券号、到期时间、使用状态等查看团购券; 39、返利管理:可列表查看购买人、时间、登陆IP、所购产品、其邀请人和登陆IP等 ; 40、返利管理:支持后台逐一管理满足返利的订单,并给用户返利到账户; 41、邮件订阅:可设置使用PHP的mail函数还是smtp发送邮件; 42、邮件订阅:可自由添加邮件通知内容,并选择邮件的发送区域; 43、邮件订阅:邮件均保存在发送队列中由计划任务控制发送,避免造成服务器堵塞; 44、计划任务:可添加计划任务,并自由设置计划任务的执行时间; 45、站长工具:可开启蜘蛛网页爬行统计,有效掌控搜索引擎对网站的友好性; 46、站长工具:可查看本站的Alexa排名、搜索引擎收录、网站近日收录、交换友情链接; 47、站长工具:可分卷备份数据库、在线打包下载、数据库优化、在线恢复等; 48、用户权限:可任意组合设置用户角色,设置不同权限的管理级别,方便多用户管理; 49、用户管理:可列表查看用户的角色、账号余额和总消费金额等; 50、用户管理:可编辑用户的信息,并查看指定用户的完整消费记录。 2010年9月7日00:01发布 1、增加Google地图功能 2010年9月13日发布 1、修改部分小BUG 2010年10月31日正式

33,008

社区成员

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

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