-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_11729.java
More file actions
47 lines (43 loc) Β· 1.01 KB
/
_11729.java
File metadata and controls
47 lines (43 loc) Β· 1.01 KB
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
38
39
40
41
42
43
44
45
46
47
package backjoon;
// https://www.acmicpc.net/problem/11729
// νλ
Έμ΄ ν μ΄λ μμ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _11729 {
// memory 96080 runtime 360
static StringBuilder sb = new StringBuilder();
static int k = 0; //νμ
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Hanoi(N,1,2,3);
System.out.println(k);
System.out.println(sb);
}
static void Hanoi(int n,int from,int by,int to) {
k++;
// μνμ΄ 1κ°μΌ λ
if(n==1) {
sb.append(from+" "+to+"\n");
return;
} else { // μνμ΄ 1κ°κ° μλ λ
Hanoi(n-1,from,to,by); // n-1μ ν ν, 1->3->2λ‘ μνμ μ λ¬
sb.append(from+" "+to+"\n");
Hanoi(n-1,by,from,to); // n-1μ ν ν, 2->1->3μΌλ‘ μνμ μ λ¬
}
}
}
/*
input
3
output
7
1 3
1 2
3 2
1 3
2 1
2 3
1 3
*/