-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHanoi.java
More file actions
29 lines (24 loc) · 874 Bytes
/
Copy pathHanoi.java
File metadata and controls
29 lines (24 loc) · 874 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
package algorithm.baek.recursive;
import algorithm.TestCase;
import java.io.*;
import java.text.ParseException;
public class Hanoi implements TestCase {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
@Override
public void test() throws ParseException, IOException {
int n = Integer.parseInt(br.readLine());
bw.write((int) (Math.pow(2, n) - 1) +"\n");
hanoi(n, 1, 2, 3);
bw.close();
}
private void hanoi(int i, int start, int mid, int to) throws IOException {
if (i == 1) {
bw.write(start + " " + to+"\n");
return;
}
hanoi(i - 1, start, to, mid);
bw.write(start + " " + to+"\n");
hanoi(i - 1, mid, start, to);
}
}