Data Structures and Algorithms_Tower of Hanoi

Hanoi Tower
According to legend, in ancient Indian temples, there was a game called Hanoi Tower (Hanoi). The game is played on a copper plate device with three rods (numbered A, B, C), and 64 gold discs are placed on rod A in order from bottom to top and from big to small (as shown in the figure). The goal of the game: move all the gold discs on pole A to pole C, and stack them in the original order. Operating rules: Only one plate can be moved at a time, and the large plate is always on the bottom and the small plate is on the top of the three rods during the movement process. The plate can be placed on any rod A, B, or C during the operation.
Please add a picture description


writing languageSwift

func hanno(num: Int, columnFrom: String, columnTemp: String, columnTo: String) {
    
    
    if num > 0 {
    
    
       // 将n-1个盘子移动到辅助柱子上
       hanno(num: num - 1, columnFrom: columnFrom, columnTemp: columnTo, columnTo: columnTemp)
       // 移动第n个盘子到目标柱子上
       print(columnFrom + "->" + columnTo)
       count += 1
       // 移动剩余的n-1个盘子
       hanno(num: num - 1, columnFrom: columnTemp, columnTemp: columnFrom, columnTo: columnTo)
    }
}

Supongo que te gusta

Origin blog.csdn.net/FlyingKuiKui/article/details/122112394
Recomendado
Clasificación