-
Notifications
You must be signed in to change notification settings - Fork 1
/
Baekjoon1992.java
73 lines (59 loc) · 1.82 KB
/
Baekjoon1992.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
/**
* https://www.acmicpc.net/problem/1992
* 백준 1992 쿼드트리
*/
public class Baekjoon1992 {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int[][] map;
private static boolean[][] visited;
private static int N = -1;
private static StringBuilder answer = new StringBuilder();
public static void main(String[] args) throws IOException {
N = Integer.parseInt(br.readLine());
map = new int[N][N];
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
String input = br.readLine();
for (int j = 0; j < N; j++) {
map[i][j]=input.charAt(j)-'0';
}
}
quadTree(N, 0, 0);
System.out.println(answer.toString());
}
private static void quadTree(int n, int y, int x) {
if(n==1){
answer.append(map[y][x]);
return;
}
boolean isAllZero =true;
boolean isAllOne =true;
for (int i = y; i < y+n; i++) {
for (int j = x; j < x+n; j++) {
if(map[i][j] == 1){
isAllZero = false;
} else if(map[i][j] == 0){
isAllOne = false;
}
}
}
if(isAllOne){
answer.append(1);
} else if(isAllZero){
answer.append(0);
} else {
answer.append("(");
quadTree(n / 2, y, x);
quadTree(n / 2, y, x + n / 2);
quadTree(n / 2, y + n / 2, x);
quadTree(n / 2, y + n / 2, x + n / 2);
answer.append(")");
}
}
}