Skip to content

Commit 865cab4

Browse files
committed
[Gold V] Title: 하노이 탑 이동 순서, Time: 456 ms, Memory: 47840 KB -BaekjoonHub
1 parent 6899e7a commit 865cab4

4 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [Gold V] 하노이 탑 이동 순서 - 11729
2+
3+
[문제 링크](https://www.acmicpc.net/problem/11729)
4+
5+
### 성능 요약
6+
7+
메모리: 47840 KB, 시간: 456 ms
8+
9+
### 분류
10+
11+
재귀
12+
13+
### 제출 일자
14+
15+
2024년 7월 21일 17:44:27
16+
17+
### 문제 설명
18+
19+
<p>세 개의 장대가 있고 첫 번째 장대에는 반경이 서로 다른 n개의 원판이 쌓여 있다. 각 원판은 반경이 큰 순서대로 쌓여있다. 이제 수도승들이 다음 규칙에 따라 첫 번째 장대에서 세 번째 장대로 옮기려 한다.</p>
20+
21+
<ol>
22+
<li>한 번에 한 개의 원판만을 다른 탑으로 옮길 수 있다.</li>
23+
<li>쌓아 놓은 원판은 항상 위의 것이 아래의 것보다 작아야 한다.</li>
24+
</ol>
25+
26+
<p>이 작업을 수행하는데 필요한 이동 순서를 출력하는 프로그램을 작성하라. 단, 이동 횟수는 최소가 되어야 한다.</p>
27+
28+
<p>아래 그림은 원판이 5개인 경우의 예시이다.</p>
29+
30+
<p style="text-align: center;"><img alt="" src="https://onlinejudgeimages.s3-ap-northeast-1.amazonaws.com/problem/11729/hanoi.png" style="height:200px; width:1050px"></p>
31+
32+
### 입력
33+
34+
<p>첫째 줄에 첫 번째 장대에 쌓인 원판의 개수 N (1 ≤ N ≤ 20)이 주어진다.</p>
35+
36+
### 출력
37+
38+
<p>첫째 줄에 옮긴 횟수 K를 출력한다.</p>
39+
40+
<p>두 번째 줄부터 수행 과정을 출력한다. 두 번째 줄부터 K개의 줄에 걸쳐 두 정수 A B를 빈칸을 사이에 두고 출력하는데, 이는 A번째 탑의 가장 위에 있는 원판을 B번째 탑의 가장 위로 옮긴다는 뜻이다.</p>
41+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
7
2+
1 3
3+
1 2
4+
3 2
5+
1 3
6+
2 1
7+
2 3
8+
1 3
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
import java.io.BufferedWriter;
3+
import java.io.IOException;
4+
import java.io.OutputStreamWriter;
5+
import java.util.Scanner;
6+
7+
8+
public class Main {
9+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
public static void main(String[] args) throws IOException {
11+
12+
Scanner sc = new Scanner(System.in);
13+
int N=sc.nextInt();
14+
System.out.println((int)Math.pow(2, N)-1);
15+
move(N , 1,2,3);
16+
bw.flush();
17+
}
18+
19+
public static void move(int N , int start ,int tmp, int target ) throws IOException {
20+
if (N==0){
21+
return;
22+
}
23+
move(N-1, start, target , tmp );
24+
bw.write(start +" "+ target+"\n" );
25+
move(N-1 ,tmp ,start , target );
26+
}
27+
}

0 commit comments

Comments
 (0)