1- import Foundation
2- //int인 from, to, through를 String으로 바꾼 풀이
3- let maxPlate = Int ( readLine ( ) !) !
4- var hanoi = Hanoi ( )
5- hanoi. getMoveWithCount ( biggerPlate: maxPlate, from: " 1 " , to: " 3 " , through: " 2 " )
6- print ( hanoi. count)
7- print ( hanoi. answer)
8- struct Hanoi {
9- var count = 0
10- var answer = " "
11- mutating func getMoveWithCount( biggerPlate: Int , from: String , to: String , through: String ) {
12- if biggerPlate == 1 {
13- answer += " \( from) \( to) \n "
14- count += 1
15- return
16- }
17- getMoveWithCount ( biggerPlate: biggerPlate - 1 , from: from, to: through, through: to)
18- answer += " \( from) \( to) \n "
19- count += 1
20- getMoveWithCount ( biggerPlate: biggerPlate - 1 , from: through, to: to, through: from)
1+ func hanoi( n: Int , from: String , through: String , to: String , history: inout String ) {
2+ count += 1
3+ if n == 1 {
4+ history. write ( " \( from) \( to) \n " )
5+ return
216 }
22- }
7+
8+ hanoi ( n: n- 1 , from: from, through: to, to: through, history: & history)
9+ history. write ( " \( from) \( to) \n " )
10+ hanoi ( n: n- 1 , from: through, through: from, to: to, history: & history)
11+ }
12+
13+ let n = Int ( readLine ( ) !) !
14+ var count = 0
15+ var history = " "
16+ hanoi ( n: n, from: " 1 " , through: " 2 " , to: " 3 " , history: & history)
17+
18+ print ( " \( count) \n \( history) " )
0 commit comments