Skip to content

Commit 0631a8a

Browse files
authored
[20250812] BOJ / G3 / 하노이 탑 이동 순서 / 이종환
1 parent 732b587 commit 0631a8a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
```java
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
9+
public class Main {
10+
static StringBuilder sb = new StringBuilder();
11+
static int target,cnt;
12+
13+
public static void main(String[] args) throws IOException {
14+
init();
15+
process();
16+
print();
17+
}
18+
19+
private static void init() throws IOException {
20+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21+
target = Integer.parseInt(br.readLine());
22+
cnt = 0 ;
23+
}
24+
25+
private static void process() throws IOException {
26+
move (1,3,2,target);
27+
}
28+
29+
private static void move(int from, int to, int mid,int size) {
30+
if ( size == 1){
31+
cnt++;
32+
sb.append(from).append(" ").append(to).append("\n");
33+
return;
34+
}
35+
36+
cnt++;
37+
move(from, mid , to, size - 1);
38+
sb.append(from).append(" ").append(to).append("\n");
39+
move(mid, to , from, size - 1);
40+
}
41+
42+
43+
private static void print() {
44+
System.out.println(cnt);
45+
System.out.println(sb.toString());
46+
}
47+
}
48+
```

0 commit comments

Comments
 (0)