[Monday] count algorithm --- Tower of Hanoi (Hanoi Tower) --- java achieve

(From Baidu Encyclopedia), a French mathematician has written a legend of ancient India: Center of the World Benares (in northern India) holy temple, a brass plate needle stuck three gems. Hindu God Lord Brahma at the time of creation of the world, in which a needle from bottom to top to put on the descending of 64 gold piece, which is called the Tower of Hanoi. Day or night, there is always a monk in these gold pieces move in accordance with the following rules: only one moving one, no matter on which needles, platelets must be large above. The monks predicted that when all the pieces are put gold from Brahma that needle moved to another time on a pin, it will destroy the world in a clap of thunder, the Vatican and towers, temples and beings will also die.

Regardless of how much credibility this legend, if consider the 64 gold pieces, from top to move a needle on another needle, and remain on the small great order. How many times does this need to move? It should be recursive method. Suppose there are n sheets, the number of movements is f (n). Obviously f⑴ = 1, f⑵ = 3, f⑶ = 7, and f (k + 1) = 2 * f (k) +1. Thereafter difficult to prove f (n) = 2 ^ n-1. When n = 64,

f(64)= 2^64-1=18446744073709551615

If once per second, totaling How long does it? A common year has 365 days 31,536,000 seconds, there are 31,622,400 seconds leap year 366 days, an average of 31,556,952 seconds per year, calculate,

18446744073709551615 / 31,556,952 = 584,554,049,253.855 years

This shift indicates that completion of these gold pieces require more than 584.5 billion years, the Earth 4.5 billion years of existence, however, the life expectancy of the solar system is also said to be tens of billions of years. Really 584.5 billion over the years, do not say the solar system and the Milky Way, least of all life on Earth, along with the Vatican towers, temples, etc., have already vanished.

Having a background, look java achieve it.

package com.jandmin.demo.leetcode;

/**
 * 汉诺塔
 *      就是在一块木板上有三个立柱,在柱1上放着三个圆盘,小的在上面,大的在下面(初始状态)。
 *      将在柱1上的三个圆盘移到柱3上面(目标状态)。
 *      条件是:每次只能移动任何一个柱子上面的一个圆盘,但大的圆盘不能放在小的圆盘上。
 * @author: JandMin
 * @create: 2019-06-06 09:38
 **/
public class TowerOfHanoi {
    public static void main(String[] args) {
        // 假设3个柱子分别位 A B C,圆盘初始放在A柱上,移动C柱
        char pillar1 = 'A';
        char pillar2 = 'B';
        char pillar3 = 'C';
        // 初始圆盘数量
        int count = 4;
        move(count,pillar1,pillar2,pillar3);
    }

    /**
     * @Description: 移动圆盘
     * @Date: 2019/6/13
     * @param count 总的圆盘数量
     * @param from 第一个柱子
     * @param middle 第二个柱子
     * @param to 第三个柱子
     * @return: void
     */
    private static void move(int count, char from, char middle, char to) {
        if(count < 1){
            System.out.println("输入有误");
            return;
        }
        if(1 == count){
            print(count,from,to);
        } else {
            move(count-1,from,to,middle);// 2、把第n-1个盘子从 开始柱子 移到 中间柱子
            print(count,from,to);        // 1、把当前盘子从    开始柱子 移到 目标柱子
            move(count-1,middle,from,to);// 3、把第n-1个盘子从 中间柱子 移到 目标柱子
        }
    }

    /**
     * @Description: 打印移动过程
     * @Date: 2019/6/13
     * @param num 第几个圆盘
     * @param from
     * @param to
     * @return: void
     */
    private static void print(int num, char from, char to) {
        System.out.println("第 " + num + " 个圆盘: " + from + " ---> " + to);
    }

}

 

Guess you like

Origin blog.csdn.net/ItRuler/article/details/91824073