Skip to content

Commit 0cec7c6

Browse files
committed
[Gold V] Title: 하노이 탑 이동 순서, Time: 128 ms, Memory: 78456 KB -BaekjoonHub
1 parent bd69684 commit 0cec7c6

2 files changed

Lines changed: 19 additions & 23 deletions

File tree

백준/Gold/11729. 하노이 탑 이동 순서/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 87388 KB, 시간: 128 ms
7+
메모리: 78456 KB, 시간: 128 ms
88

99
### 분류
1010

1111
재귀
1212

1313
### 제출 일자
1414

15-
2024년 5월 21일 20:49:05
15+
2025년 6월 26일 00:59:32
1616

1717
### 문제 설명
1818

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
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

Comments
 (0)