首页 > 专题系列 > Java解POJ > POJ 1958 Strange Towers of Hanoi [解题报告] Java
2013
11-10

POJ 1958 Strange Towers of Hanoi [解题报告] Java

Strange Towers of Hanoi

问题描述 :

Background

Charlie Darkbrown sits in another one of those boring Computer Science lessons: At the moment the teacher just explains the standard Tower of Hanoi problem, which bores Charlie to death!



The teacher points to the blackboard (Fig. 4) and says: “So here is the problem:
  • There are three towers: A, B and C.
  • There are n disks. The number n is constant while working the puzzle.
  • All disks are different in size.
  • The disks are initially stacked on tower A increasing in size from the top to the bottom.
  • The goal of the puzzle is to transfer all of the disks from tower A to tower C.
  • One disk at a time can be moved from the top of a tower either to an empty tower or to a tower with a larger disk on the top.

So your task is to write a program that calculates the smallest number of disk moves necessary to move all the disks from tower A to C.”

Charlie: “This is incredibly boring—everybody knows that this can be solved using a simple recursion.I deny to code something as simple as this!”

The teacher sighs: “Well, Charlie, let’s think about something for you to do: For you there is a fourth tower D. Calculate the smallest number of disk moves to move all the disks from tower A to tower D using all four towers.”

Charlie looks irritated: “Urgh. . . Well, I don’t know an optimal algorithm for four towers. . . ”

Problem

So the real problem is that problem solving does not belong to the things Charlie is good at. Actually, the only thing Charlie is really good at is “sitting next to someone who can do the job”. And now guess what — exactly! It is you who is sitting next to Charlie, and he is already glaring at you.

Luckily, you know that the following algorithm works for n <= 12: At first k >= 1 disks on tower A are fixed and the remaining n-k disks are moved from tower A to tower B using the algorithm for four towers.Then the remaining k disks from tower A are moved to tower D using the algorithm for three towers. At last the n – k disks from tower B are moved to tower D again using the algorithm for four towers (and thereby not moving any of the k disks already on tower D). Do this for all k 2 ∈{1, …. , n} and find the k with the minimal number of moves.

So for n = 3 and k = 2 you would first move 1 (3-2) disk from tower A to tower B using the algorithm for four towers (one move). Then you would move the remaining two disks from tower A to tower D using the algorithm for three towers (three moves). And the last step would be to move the disk from tower B to tower D using again the algorithm for four towers (another move). Thus the solution for n = 3 and k = 2 is 5 moves. To be sure that this really is the best solution for n = 3 you need to check the other possible values 1 and 3 for k. (But, by the way, 5 is optimal. . . )

输入:

There is no input.

输出:

For each n (1 <= n <= 12) print a single line containing the minimum number of moves to solve the problem for four towers and n disks.

样例输入:

No input.

样例输出:

REFER TO OUTPUT.

解题代码:

//* @author: [email protected]
import java.util.*;
public class Main {

    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        for (int i = 1; i <= 12; i++) {
            System.out.println(move4(i));
        }
    }

    public static int move4(int k) {
        if (k == 1) return 1;
        if (k == 2) return 3;
        if (k == 3) {
            return 5;
        }
        int min = Integer.MAX_VALUE;
        for (int i = 1; i < k; i++) {
            int t = 2 * move4(k - i) + move3(i);
            if (t < min) {
                min = t;
            }
        }
        return min;
    }

    public static int move3(int k) {
        if (k == 0) {
            return 0;
        }
        return move3(k - 1) + move3(k - 1) + 1;
    }
}

  1. 一开始就规定不相邻节点颜色相同,可能得不到最优解。我想个类似的算法,也不确定是否总能得到最优解:先着一个点,随机挑一个相邻点,着第二色,继续随机选一个点,但必须至少有一个边和已着点相邻,着上不同色,当然尽量不增加新色,直到完成。我还找不到反例验证他的错误。。希望LZ也帮想想, 有想法欢迎来邮件。谢谢

  2. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)

  3. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。