"Problem description"
There is a Vatican tower called Hanoi, there are three pillars A, B and C on the Hanoi Tower, there are several discs on column A, all discs of different sizes, and small in the large under, as shown:
Move all the discs on column A with pillar B to column C, move the process to move only one disc at a time, and still be small after the large in the lower, as shown:
The moving process of all discs.
"Design Ideas"
When column A has 1 discs, it is only necessary to move the disc from column A to column C.
When the column A has 2 discs, first move the disc above the column A from pillar A to column B, and then move the disc under Column A from column A to column C, and finally move the disk of column B from pillar B to column C.
When column A has 3 discs, the two discs above the column A are moved from pillar A to pillar B, and then the bottom disc of pillar A is moved from column A to column C, and finally the two discs of Pillar B are moved from pillar B to pillar C with column A.
In general, when column A has n disks, from top to bottom numbered 1, 2, 3, ...., N, first the column A above the number 1 to n-1 of the disk from pillar A through column C to the column B, and then the column a the bottom of the number of n is moved from column A to column C, and finally the column B of N- 1 discs are moved from pillar B to pillar C with column A.
The problem is to move the n discs of pillar A through column B to column C. Combined with the general steps above, it's easy to think of recursion. Assuming that the recursive function Playhanoitower (n, A, B, C) is used to move n discs from pillar A to column C, the function move (n, a, c) is used to move a disk numbered n from column A to column C, the general steps above can be expressed as:
1. When n = 1 o'clock, call Move (1, A, C) to move the disc from column A to column C.
2. When n > 1 o'clock,
1) Call Playhanoitower (N-1, a,c, b) and move the disk with column A above the number 1 to n-1 from pillar A to column B by Pillar C;
2) call Move (n, A, C) to move the disc numbered N on column A from column A to column C;
3) Call Playhanoitower (n-1, B, A, C) and move the n-1 disc of pillar B from pillar B to column C.
"Swift Programming Implementation"
var count = 1
Move the N-numbered disc from the post to the pillar to
func Move (n: Int, from: Character, to: Character) {
Print("section\(Count) Step: Move\(N) Number disc,\( from)--->\( to)")
Count + = 1
}
Move n discs from pillars to pillars Dependon
func playhanoitower (n: Int, from: Character, Dependon: Character , to: Character ) {
//Only one disc with number 1
ifn = = 1 {
//The disc numbered 1 is moved from the pillar to the pillar to
Move(n: 1, From:from, to:to)
} else {
//Move the topmost n-1 disc from the pillar from the column to the Pillar Dependon
playhanoitower(N- 1, from:from,dependon:to, To:dependon)
//The disk numbered n is moved from the pillar to the pillar to
Move(n, From:from, to:to)
//The topmost n-1 disc is moved from pillar Dependon to pillar to
playhanoitower(N- 1, From:dependon,dependon:from, to:to)
}
}
Playhanoitower(n:5, from:Character("A"), Dependon:Character("B") to:Character("C"))
"Run Results"
1th step: Move the 1th-disc, A--->c
2nd Step: Move the 2nd-disc, A--->b
3rd Step: Move the 1th-disc, C--->b
4th Step: Move the 3rd-disc, A--->c
5th: Move disc 1th, B--->a
6th: Move disc 2nd, B--->c
7th step: Move the 1th-disc, A--->c
8th step: Move the 4th-disc, A--->b
9th step: Move the 1th-disc, C--->b
10th Step: Move the 2nd-disc, C--->a
11th: Move disc 1th, B--->a
12th Step: Move the 3rd-disc, C--->b
13th step: Move the 1th-disc, A--->c
14th Step: Move the 2nd-disc, A--->b
15th step: Move the 1th-disc, C--->b
16th Step: Move the 5th-disc, A--->c
17th: Move disc 1th, B--->a
18th: Move disc 2nd, B--->c
19th step: Move the 1th-disc, A--->c
20th: Move disc 3rd, B--->a
21st Step: Move the 1th-disc, C--->b
22nd Step: Move the 2nd-disc, C--->a
23rd: Move disc 1th, B--->a
24th: Move disc 4th, B--->c
25th Step: Move the 1th-disc, A--->c
26th Step: Move the 2nd-disc, A--->b
27th Step: Move the 1th-disc, C--->b
28th Step: Move the 3rd-disc, A--->c
29th: Move disc 1th, B--->a
30th: Move disc 2nd, B--->c
31st Step: Move the 1th-disc, A--->c
For more swift fun stories, see the 51CTO Academy Video: "The case of the Super Brother Video auditorium to conquer Swift (2nd season: Fun case)"
This article from "Super elder brother Video lecture" blog, declined reprint!
Swift Fun case of the Nottingham Tower