| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1511 人关注过本帖, 1 人收藏
标题:jugs,谁来和我一起做,有点难!
只看楼主 加入收藏
xujie3
Rank: 1
来 自:浙江
等 级:新手上路
帖 子:51
专家分:0
注 册:2008-7-22
收藏(1)
 问题点数:0 回复次数:11 
jugs,谁来和我一起做,有点难!
Problem 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
搜索更多相关主题的帖子: jugs 
2008-07-24 20:59
coming
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2008-4-20
收藏
得分:0 
英语啊~~~
2008-07-24 21:20
xujie3
Rank: 1
来 自:浙江
等 级:新手上路
帖 子:51
专家分:0
注 册:2008-7-22
收藏
得分:0 
回复 2# coming 的帖子
题目的准确意思我也不太明白,有谁能解释一下的?
2008-07-24 21:37
病书生
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2007-11-8
收藏
得分:0 
Sample Input
3 5 4
//A水壶的的容量是3加仑,B水壶的容量是5加仑,要让B水壶装4加仑的水


Sample Output
fill B    //装满 B水壶 B=5
pour B A  //B水壶灌入A水壶 A=3,B=2
empty A   //清空A  A=0
pour B A  //B水壶灌入A水壶 A=2,B=0;
fill B    //装满B水壶 A=2,B=5
pour B A  //B水壶灌入A水壶 A=3,B=4
成功
2008-07-24 22:26
xujie3
Rank: 1
来 自:浙江
等 级:新手上路
帖 子:51
专家分:0
注 册:2008-7-22
收藏
得分:0 
回复 4# 病书生 的帖子
知道了,太感谢了,看来接下来就是算法问题了。
2008-07-25 07:42
xujie3
Rank: 1
来 自:浙江
等 级:新手上路
帖 子:51
专家分:0
注 册:2008-7-22
收藏
得分:0 
谁有什么好的解决方案?思路?先谢谢了。
2008-07-25 09:09
carmen0531
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-7-22
收藏
得分:0 
我认为可以用树的原理
用一个6叉树 树杈分别为:fill  A,fill  B, pour A B  ,pour B  A,empty  A,empty  B.然后求满足要求的根结点。(如果能发图就好了,不知道我有没有传达清我的意思。。。)
2008-07-25 11:43
xujie3
Rank: 1
来 自:浙江
等 级:新手上路
帖 子:51
专家分:0
注 册:2008-7-22
收藏
得分:0 
回复 7# carmen0531 的帖子
恩,我会试试的。关于树的应用,因为还没学数据结构,我得先去看书了。谢谢指点,有问题的话能直接问你吗?
2008-07-25 12:29
carmen0531
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-7-22
收藏
得分:0 
当然可以了,但也只能是讨论,我很长时间没写代码了,你的这个题目我想了半天,虽然有思路但还是没写出来。。。。很是郁闷啊。。。
2008-07-25 23:50
unicafree
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-1-11
收藏
得分:0 
确实有点难,还是写不出来
2008-07-26 15:36
快速回复:jugs,谁来和我一起做,有点难!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043267 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved