Skip to content

Commit 35006c7

Browse files
committed
백준_11729
1 parent 26b9f35 commit 35006c7

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
void hanoi(int n, int from, int to, int other) {
6+
if (n == 1) {
7+
cout << from << ' ' << to << '\n';
8+
return;
9+
}
10+
11+
hanoi(n - 1, from, other, to); // n-1개를 보조 원판으로
12+
cout << from << ' ' << to << '\n'; // 제일 큰원판 이동
13+
hanoi(n - 1, other, to, from); // 보조 기둥의 원판을 목표 기둥으로 이동동
14+
}
15+
16+
int main(void) {
17+
int n;
18+
cin >> n;
19+
20+
cout << (1 << n) - 1 << '\n'; // 총 이동 횟수: 2^n - 1
21+
hanoi(n, 1, 3, 2); // 1번 기둥에서 3번 기둥으로 이동
22+
23+
return 0;
24+
}

0 commit comments

Comments
 (0)