题目:
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
主要意思是:两个容器容量为A,B,他们可以相互倒水,以及把水清空,或者倒满,最后要是B中得到N容量的水(A<B,N<B)
开始是从算法的角度考虑,半天没思路,然后尝试暴力法,一个一个试,那样也是能找到的。多琢磨了几个数据,发现了一个规律,那就是如果N,是奇数,那么一直从A向B倒水会快,反之,就从B向A倒水。不过这个并不是一定能找到答案。可能就是一个趣味题。
java代码如下:
import java.util.Scanner; public class Main { public static class Puzzle { public int A; public int B; public int Ca; public int Cb; public int N; public void emptyA() { this.A = 0; System.out.println("empty A"); } public void emptyB() { this.B = 0; System.out.println("empty B"); } public void fullA() { this.A = this.Ca; System.out.println("fill A"); } public void fullB() { this.B = this.Cb; System.out.println("fill B"); } public void pourA() { int bLeft = this.Cb- this.B; if(this.A <= bLeft) { this.B = this.B + this.A; this.A = 0; } else { this.A = this.A - bLeft; this.B = this.Cb; } System.out.println("pour A B"); } public void pourB() { int aLeft = this.Ca- this.A; if(this.B <= aLeft) { this.A = this.A + this.B; this.B = 0; } else { this.B = this.B - aLeft; this.A = this.Ca; } System.out.println("pour B A"); } } public static void main(String[]args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { Puzzle puzzle = new Puzzle(); String line = sc.nextLine(); String[] strArray = line.split(" "); puzzle.Ca = Integer.parseInt(strArray[0]); puzzle.Cb = Integer.parseInt(strArray[1]); puzzle.N = Integer.parseInt(strArray[2]); pour(puzzle); } } public static int pour(Puzzle puzzle) { if(puzzle.B == puzzle.N) { System.out.println("success"); return 1; } else if(puzzle.A == puzzle.N) { if(puzzle.B != 0) { puzzle.emptyB(); } puzzle.pourA(); System.out.println("success"); return 1; } if(puzzle.N % 2 == 1) { if(puzzle.A == 0) { puzzle.fullA(); } else { puzzle.pourA(); if(puzzle.A != 0 && puzzle.B != 0) { puzzle.emptyB(); } } } else { if(puzzle.B == 0) { puzzle.fullB(); } else { puzzle.pourB(); if(puzzle.A != 0 && puzzle.B != 0) { puzzle.emptyA(); } } } pour(puzzle); return 0; } }