boj)1992 - 쿼드트리
2020. 11. 13. 16:21ㆍ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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import java.io.*;
public class boj_1992 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static int[][] map;
static int N;
public static void main(String[] args) throws IOException {
input();
divideAndConquer(0, 0, N);
System.out.println(sb.toString());
}
static void input() throws IOException {
N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i = 0; i < N; i++) {
String s = br.readLine();
for (int j = 0; j < N; j++) {
map[i][j] = s.charAt(j) - '0';
}
}
}
static boolean isSame(int x, int y, int length) {
int val = map[x][y];
for (int i = x; i < x + length; i++) {
for (int j = y; j < y + length; j++) {
if (val != map[i][j]) return false;
}
}
return true;
}
static void divideAndConquer(int x, int y, int length) {
if (isSame(x, y, length)) {
sb.append(map[x][y]);
} else {
int newLength = length/2;
sb.append('(');
divideAndConquer(x, y, newLength);
divideAndConquer(x, y + newLength, newLength);
divideAndConquer(x + newLength, y, newLength);
divideAndConquer(x + newLength, y + newLength, newLength);
sb.append(')');
}
}
}
|
cs |
- 분할정복 / 재귀
- boj 1780 :: 종이의 개수 문제와 유사하여 쉽게 풀었다.
- 규칙, 언제 어떻게 쪼개고, 합칠지 잘 찾자.
- 4분할로 쪼개기
'PS > boj' 카테고리의 다른 글
boj)2447 - 별 찍기 - 10 (0) | 2020.11.14 |
---|---|
boj)16505 - 별 (0) | 2020.11.14 |
boj)1780 - 종이의 개수 (0) | 2020.11.13 |
boj)17478 - 재귀함수가 뭔가요? (0) | 2020.11.12 |
boj)1074 - Z (0) | 2020.11.12 |