-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11729.java
More file actions
37 lines (24 loc) · 998 Bytes
/
11729.java
File metadata and controls
37 lines (24 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.*;
public class Main {
public static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
sb.append((int) (Math.pow(2, N) - 1)).append('\n');
Hanoi(N, 1, 2, 3);
System.out.println(sb);
}
public static void Hanoi(int N, int start, int mid, int to) {
if (N == 1) {
sb.append(start + " " + to + "\n");
return;
}
// A -> C로 옮긴다고 가정할 떄,
// STEP 1 : N-1개를 A에서 B로 이동 (= start 지점의 N-1개의 원판을 mid 지점으로 옮긴다.)
Hanoi(N - 1, start, to, mid);
//1개를 A에서 C로 이동 (= start 지점의 N번째 원판을 to지점으로 옮긴다.)
sb.append(start + " " + to + "\n");
//N-1개를 B에서 C로 이동 (= mid 지점의 N-1개의 원판을 to 지점으로 옮긴다.)
Hanoi(N - 1, mid, start, to);
}
}