Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lkhyun/202507/24 BOJ G5 하노이 탑 이동 순서.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
```java
import java.util.*;
import java.io.*;

public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int cnt = 0;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws Exception {
int N = Integer.parseInt(br.readLine());
hanoi(N, 1, 2, 3);
bw.write(cnt+"\n");
bw.write(sb.toString());
bw.close();
}
static void hanoi(int n, int from, int temp, int to) throws Exception{
if(n == 0) return;

hanoi(n-1,from, to, temp); //from에서 to로 잠깐 옮겨놓음 맨 아래 빼고
sb.append(from).append(" ").append(to).append("\n"); //그럼 이제 from에서 원래 가고 싶은 to로 보낼 수 있으니 출력
cnt++;
hanoi(n-1,temp,from, to); //그 담에 잠깐 올려뒀던 위에 있는거 다시 to로 올려놔야하니까
}
}
```