Program for Tower of Hanoi using Golang

Using Go to Determine the Intersection of Two Linked Lists

The Tower of Hanoi is a classic problem that involves moving a stack of discs from one peg to another, with the following rules: 



1. Only one disc can be moved at a time.
2. A disc cannot be placed on top of a smaller disc.

   The goal is to move the entire stack of discs from the starting peg to the destination peg, following the rules above. 

 The solution to the Tower of Hanoi problem involves using recursion. The idea is to divide the problem into smaller subproblems, solve each of these subproblems, and then combine the solutions to the subproblems to get the solution to the original problem. 

 In the Go program below, we define a function towerOfHanoi that takes four arguments: n: the number of discs in the stack from: the starting peg to: the destination peg aux: the auxiliary peg


        
    package main

import "fmt"

func towerOfHanoi(n int, from, to, aux string) {
	if n == 1 {
		fmt.Println("Move disk 1 from", from, "to", to)
		return
	}
	towerOfHanoi(n-1, from, aux, to)
	fmt.Println("Move disk", n, "from", from, "to", to)
	towerOfHanoi(n-1, aux, to, from)
}

func main() {
	towerOfHanoi(3, "A", "C", "B")
}


The function works as follows: 

  1.  If n is 1, it means there is only one disc in the stack. In this case, we simply print a message indicating that the disc should be moved from the starting peg to the destination peg, and return. 
  2. If n is greater than 1, we make a recursive call to towerOfHanoi, passing n-1 as the value for n, and swapping the values of aux and to. This step moves the top n-1 discs from the starting peg to the auxiliary peg, using the destination peg as an intermediate step.
  3.  After the recursive call, we print a message indicating that the nth disc should be moved from the starting peg to the destination peg. 
  4. Finally, we make another recursive call to towerOfHanoi, passing n-1 as the value for n, aux as the value for from, to as the value for aux, and from as the value for to. This step moves the top n-1 discs from the auxiliary peg to the destination peg, using the starting peg as an intermediate step. 

The base case of the recursion is when n is 1, at which point we simply print the message indicating that the disc should be moved from the starting peg to the destination peg. 
 The recursive calls to towerOfHanoi handle the subproblems of moving the top n-1 discs from the starting peg to the auxiliary peg and from the auxiliary peg to the destination peg. By solving these subproblems, we are ultimately able to solve the original problem of moving the entire stack of n discs from the starting peg to the destination peg. 

To test the program, we call towerOfHanoi with the arguments 3, "A", "C", and "B", which represents a stack of 3 discs on peg A, and the goal of moving the stack to peg C using peg B as an intermediate step. The program should output the following messages:

        
        Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
    
    
These messages represent the steps required to move the stack of 3 discs from peg A to peg C following the rules of the Tower of Hanoi.

Post a Comment

0 Comments