算法分析与设计-大型实验报告样本
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
算法分析大型实验报告
班级:
姓名:
学号:
指导老师:
2011年8月
ZJUT-1005 Jugs1
Special Judge
Time limit: 1 Seconds Memory limit: 32768K
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
1浙江大学,Turing Cup 2001,/show_problem.php?pid=1005
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
【全文翻译】
在电影“虎胆龙威3-纽约大劫案”中,布鲁斯·威利斯和杰里米·艾恩斯遇到这样一个难题:给他们一个3加仑水壶和一个5加仑水壶,要求在5加仑水壶里准确装入4加仑的水。真是个难题呢。
假定两个水壶A和B,供水量不限。可以使用三种方法装水:
(1)给一个水壶装水;
(2)把一个水壶倒空;
(3)从一个水壶倒进另一个水壶。
当从一个水壶倒进另一个水壶时,如果第一个水壶倒空,或者第二个水壶装满就不能再倒了。例如,一个水壶A是5加仑和另一个水壶B是6加仑,水量是8加仑,则从水壶A 倒进水壶B时,让水壶B充满水而水壶A剩3加仑水。
问题由3个参数:Ca,Cb和N,分别表示水壶A和B的容量,目标水量N。解决问题的目标是,给出一系列倒水的步骤,使水壶B中的水量恰好是N。
“pour A B”,表示将水从水壶A倒进水壶B;“success”表示目标已经完成。
我们假定每个输入都有一个解决方案。
输入
输入有多行,每行都是一个难题。每个难题有三个正整数:Ca,Cb和N。假设0 输出 输出是由一系列倒水操作构成的,其目标是实现水壶B中有N加仑的水。最后一行是“success”;从第1列开始输出,行末没有空格。 【算法分析】 本题是“Special Judge”,即答案不是唯一的,在服务器端有专门的程序负责判题。 本题属于著名的“倒水问题”。在题目中已经介绍了“倒水”的规则:水是取之不尽的,可以把一个水壶全部灌满(原先是完全空的),或者把一个水壶全部倒空,或者从一个水壶倒进另一个水壶(当然不能溢出)。 题目中给出了三项假定: (1)只有两个水壶。 实现起来是方便的:我们可以从水壶A或者水壶B开始灌满水,将水壶A的水倒进水