-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path11729.java
35 lines (28 loc) · 886 Bytes
/
11729.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Hanoi {
static StringBuilder answer;
static int K;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
br.close();
K = 0;
answer = new StringBuilder();
recur(N, 1, 2, 3);
System.out.print(K + "\n" + answer.toString());
}
public static void recur(int N, int from, int via, int to) {
if(N == 1) move(from, to);
else {
recur(N - 1, from, to, via);
move(from, to);
recur(N - 1, via, from, to);
}
}
public static void move(int from, int to) {
K++;
answer.append(from + " " + to + "\n");
}
}