boj)11729 - 하노이 탑 이동 순서
2020. 11. 11. 18:37ㆍPS/boj
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
|
import java.io.*;
public class boj_11729 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine());
hanoi(1, 3, n);
System.out.println((int) (Math.pow(2, n) - 1));
System.out.println(sb.toString());
}
public static void hanoi(int from, int to, int n) {
if (n == 1) {
sb.append(from + " " + to + "\n");
return;
}
hanoi(from, 6-from-to, n-1);
sb.append(from + " " + to + "\n");
hanoi(6-from-to, to, n-1);
}
}
|
cs |
- 재귀 너무 어렵다.
- 하노이의 탑 원판 총 이동횟수 : 2^N - 1
'PS > boj' 카테고리의 다른 글
boj)17478 - 재귀함수가 뭔가요? (0) | 2020.11.12 |
---|---|
boj)1074 - Z (0) | 2020.11.12 |
boj)2589 - 보물섬 (0) | 2020.11.11 |
boj)2941 - 크로아티아 알파벳 (0) | 2020.11.05 |
boj)1316 - 그룹 단어 체커 (0) | 2020.11.05 |