Skip to content

Commit c809347

Browse files
authored
[20250724] BOJ / G5 / 하노이 탑 이동 순서 / 이강현
1 parent a04a9d5 commit c809347

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static int cnt = 0;
9+
static StringBuilder sb = new StringBuilder();
10+
public static void main(String[] args) throws Exception {
11+
int N = Integer.parseInt(br.readLine());
12+
hanoi(N, 1, 2, 3);
13+
bw.write(cnt+"\n");
14+
bw.write(sb.toString());
15+
bw.close();
16+
}
17+
static void hanoi(int n, int from, int temp, int to) throws Exception{
18+
if(n == 0) return;
19+
20+
hanoi(n-1,from, to, temp); //from에서 to로 잠깐 옮겨놓음 맨 아래 빼고
21+
sb.append(from).append(" ").append(to).append("\n"); //그럼 이제 from에서 원래 가고 싶은 to로 보낼 수 있으니 출력
22+
cnt++;
23+
hanoi(n-1,temp,from, to); //그 담에 잠깐 올려뒀던 위에 있는거 다시 to로 올려놔야하니까
24+
}
25+
}
26+
```

0 commit comments

Comments
 (0)