[Recursive Divide and Conquer] GO Language Implementation of the Tower of Hanoi

 

 

  • Personal homepage: My homepage
  • Motto: Tian Xingjian, the gentleman strives for self-improvement;

Table of contents

foreword

Problem Description

problem analysis

question extension

Algorithm implementation

end


foreword

This paper uses the recursive divide-and-conquer idea to decompose the classic problem Tower of Hanoi, and uses the GO language for coding verification.


Problem Description

The Tower of Hanoi, also known as the Tower of Hanoi, is an educational toy that originated from an ancient Indian legend. When Brahma created the world, he made three diamond pillars. On one pillar, 64 gold discs were stacked in order of size from bottom to top. Brahma ordered Brahmin to rearrange the discs on another pillar in order of size from below. And it is stipulated that the disk cannot be enlarged on the small disk, and only one disk can be moved between the three pillars at a time.

There are two rules you should be aware of when moving:

(1) Only one plate can be moved at a time

(2) The large plate cannot be pressed on the small plate

problem analysis

Suppose there are three pillars named A, B, C, and n disks are initially on pillar A, and they need to be moved to pillar B with the help of pillar C. The disks are numbered from large to small as n, n - 1, ..., 2 , 1.

Let's break down the subproblems now:

(1) If you want to move the largest n discs to the B column, you must first move the n - 1,...,1 discs to the C column; so first complete n-1 discs from A to C the movement of B;

(2) Move the nth disc left on the A column to the B column; this completes the movement of the largest disc, and it will no longer move;

(3) Now the situation is that n-1, ..., 1 disks are on the C-pillar, the problem becomes to move the n-1, ..., 1 disks from the C-pillar to the B-pillar, with the aid of the A-pillar ;

until all the discs move to the B-pillar;

question extension

How many steps does it take to complete it? Is there a way to do it in fewer steps? Let's take a look below.

Assuming there are n pieces, the number of moves is f(n). In this way, f(1)=1, f(2)=3, f(3)=7, and f(k+1)=2*f(k)+1. It is not difficult to prove f(n)=2n-1 afterwards.

When n=64, 1,844,674,407,370,955,1615 moving steps are required. How long will it take if it is once every second? There are 31536000 seconds in 365 days in a normal year, 31622400 seconds in 366 days in a leap year, an average of 31557600 seconds per year, calculate: 18446744073709551615 seconds

It will take more than 584.542 billion years to remove these gold pieces in ancient legends, and almost everything will disappear.

Algorithm implementation

package main

import (
        "fmt"
)


var count = 0

func Move(n int, A string, B string) {
        count ++

        fmt.Println("move ",n," from ", A, " to ",B)
}

func Hanoi(n int, A string, B string, C string) {
        if( n > 0) {
                Hanoi(n - 1, A, C, B)
                Move(n, A, B)
                Hanoi(n - 1, C, B, A)
        }
}


func main() {

        n := 3
        Hanoi(n, "A", "B", "C")

        fmt.Println("hanoi A->B, f(",n,")=",count)
}


end

Author email: [email protected]
If there are any mistakes or omissions, please point them out and learn from each other.

Note: Do not reprint without consent!

Guess you like

Origin blog.csdn.net/senllang/article/details/129660382