Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cmal/data_structures_and_algorith…
Browse files Browse the repository at this point in the history
…ms_in_go
  • Loading branch information
Yu Zhao committed May 10, 2018
2 parents 1657a39 + d0330d5 commit 4ea8d6d
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions ch01/1_35.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// tower of hanoi
// Hanoi

func main() {
TowersOfHanoi(3)
}
package main

func TOHUtil(num int, from string, to string, temp string) {

import (
"fmt"
"os"
"strconv"
)

func main() {
num, _ := strconv.Atoi(os.Args[1])
fmt.Println(num)
TowersOfHanoi(num)
}

func TowersOfHanoi(num int) {
fmt.Println("The sequence of moves involved in the Tower of Hanoi are:")
TOHUtil(num, "A", "C", "B")
}

func TOHUtil(num int, from string, to string, temp string) {
if num == 0 {
return
}
TOHUtil(num - 1, from, temp, to)
fmt.Println("Move disk ", num, " from peg ", from, " to peg ", to)
TOHUtil(num - 1, temp, to, from)
}

0 comments on commit 4ea8d6d

Please sign in to comment.