|
| 1 | +import java.io.*; |
| 2 | + |
| 3 | +/* |
| 4 | + * 쿼드트리 |
| 5 | + */ |
| 6 | + |
| 7 | +public class DH_1992 { |
| 8 | + static int[][] arr; |
| 9 | + public static void main(String[] args) throws Exception { |
| 10 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + |
| 12 | + int N = Integer.parseInt(br.readLine()); |
| 13 | + arr = new int[N][N]; |
| 14 | + |
| 15 | + for(int r = 0; r < N; r++) { |
| 16 | + String s = br.readLine(); |
| 17 | + |
| 18 | + for(int c = 0; c < N; c++) { |
| 19 | + arr[r][c] = s.charAt(c) - '0'; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + int length = N; |
| 24 | + |
| 25 | + String str = func(0, 0, length); |
| 26 | + System.out.println(str); |
| 27 | + } |
| 28 | + |
| 29 | + static String func(int r, int c, int l) { |
| 30 | + String s = ""; |
| 31 | + |
| 32 | + int sum = 0; |
| 33 | + for(int sr = r; sr < r + l; sr++) { |
| 34 | + for(int sc = c; sc < c + l; sc++) { |
| 35 | + sum += arr[sr][sc]; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if(sum == 0) s += "0"; |
| 40 | + else if(sum == l * l) s += "1"; |
| 41 | + else { |
| 42 | + int nl = l >> 1; |
| 43 | + s += "("; |
| 44 | + s += func(r, c, nl); // 왼쪽 위 |
| 45 | + s += func(r, c + nl, nl); // 오른쪽 위 |
| 46 | + s += func(r + nl, c, nl); // 왼쪽 아래 |
| 47 | + s += func(r + nl, c + nl, nl); // 오른쪽 아래 |
| 48 | + s += ")"; |
| 49 | + } |
| 50 | + return s; |
| 51 | + } |
| 52 | +} |
0 commit comments