Skip to content

Commit 77157e7

Browse files
authored
Merge pull request #1329 from AlgorithmWithGod/JHLEE325
[20251106] BOJ / G6 / 하노이 탑 / 이준희
2 parents 8cda35f + 4900c5b commit 77157e7

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+
import java.util.*;
5+
6+
public class Main {
7+
static StringBuilder sb = new StringBuilder();
8+
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
int N = Integer.parseInt(br.readLine());
12+
13+
BigInteger moves = BigInteger.ONE.shiftLeft(N).subtract(BigInteger.ONE);
14+
sb.append(moves).append("\n");
15+
16+
if (N <= 20) {
17+
hanoi(N, 1, 3, 2);
18+
}
19+
20+
System.out.println(sb.toString());
21+
}
22+
23+
static void hanoi(int n, int from, int to, int temp) {
24+
if (n == 1) {
25+
sb.append(from).append(" ").append(to).append("\n");
26+
return;
27+
}
28+
hanoi(n - 1, from, temp, to);
29+
sb.append(from).append(" ").append(to).append("\n");
30+
hanoi(n - 1, temp, to, from);
31+
}
32+
}
33+
```

0 commit comments

Comments
 (0)