|
| 1 | +import java.io.*; |
| 2 | + |
| 3 | +//1<=N<=64 |
| 4 | +//분할정복: 반복해서 작은영역을 탐색할 때는 `재귀`를 생각해보기 |
| 5 | +public class YJ_1992 { |
| 6 | + static char[][] video; |
| 7 | + public static void main(String[] args) throws IOException { |
| 8 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + int n = Integer.parseInt(br.readLine()); |
| 10 | + video = new char[n][n]; |
| 11 | + for(int i=0; i<n; i++){ |
| 12 | + String data = br.readLine(); |
| 13 | + for(int j=0; j<n; j++){ |
| 14 | + video[i][j] = data.charAt(j); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + compress(0,0,n); |
| 19 | + System.out.println(sb.toString()); |
| 20 | + } |
| 21 | + |
| 22 | + static StringBuilder sb = new StringBuilder(); |
| 23 | + static void compress(int x, int y, int size){ |
| 24 | + if(check(x, y, size)){ |
| 25 | + sb.append(video[x][y]); |
| 26 | + return; |
| 27 | + } |
| 28 | + sb.append("("); |
| 29 | + compress(x,y,size/2); //왼쪽위 |
| 30 | + compress(x,y+size/2,size/2); //오른쪽위 |
| 31 | + compress(x+size/2,y,size/2); //왼쪽아래 |
| 32 | + compress(x+size/2,y+size/2,size/2); //오른쪽아래 |
| 33 | + sb.append(")"); |
| 34 | + } |
| 35 | + |
| 36 | + static boolean check(int x, int y, int size){ |
| 37 | + int value = video[x][y]; |
| 38 | + for(int i=x; i<x+size; i++){ |
| 39 | + for(int j=y; j<y+size; j++){ |
| 40 | + if(value != video[i][j]){ //기준(0또는1)과 하나라도 틀리면 분할해야함 |
| 41 | + return false; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + return true; |
| 46 | + } |
| 47 | +} |
0 commit comments