Skip to content

Commit c4fc8bd

Browse files
authored
[20251106] BOJ / G5 / 하노이 탑 / 설진영
1 parent cfe6a6f commit c4fc8bd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
```java
2+
import java.io.*;
3+
import java.math.BigInteger;
4+
5+
public class Main {
6+
static StringBuilder sb = new StringBuilder();
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
int n = Integer.parseInt(br.readLine());
11+
12+
BigInteger moveCount = BigInteger.TWO.pow(n).subtract(BigInteger.ONE);
13+
sb.append(moveCount).append('\n');
14+
15+
if (n <= 20) {
16+
hanoi(n, 1, 3, 2);
17+
}
18+
19+
System.out.print(sb);
20+
}
21+
22+
static void hanoi(int n, int from, int to, int aux) {
23+
if (n == 1) {
24+
sb.append(from).append(' ').append(to).append('\n');
25+
return;
26+
}
27+
28+
hanoi(n - 1, from, aux, to);
29+
sb.append(from).append(' ').append(to).append('\n');
30+
hanoi(n - 1, aux, to, from);
31+
}
32+
}
33+
```

0 commit comments

Comments
 (0)