문제 출처: https://www.acmicpc.net/problem/1992
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { private static String[] arr; public static void main(String[] args) throws IOException { BufferedReader reader= new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(reader.readLine()); arr = new String[N]; for(int i=0 ; i<N ; i++) arr[i] = reader.readLine(); if(N == 1) System.out.println(arr[0]); else System.out.println(quad_tree(0, N-1, 0, N-1)); } public static String quad_tree(int xLow, int xHigh, int yLow, int yHigh){ StringBuilder str = new StringBuilder(); if(xLow < xHigh && yLow < yHigh){ int xMid = (xLow+xHigh)/2; int yMid = (yLow+yHigh)/2; String one = quad_tree(xLow, xMid, yLow, yMid); String two = quad_tree(xMid+1, xHigh, yLow, yMid); String three = quad_tree(xLow, xMid, yMid+1, yHigh); String four = quad_tree(xMid+1, xHigh, yMid+1, yHigh); if(one.equals("")){ str.append(arr[yLow].charAt(xLow)); str.append(arr[yLow].charAt(xHigh)); str.append(arr[yHigh].charAt(xLow)); str.append(arr[yHigh].charAt(xHigh)); } else { str.append(one + two + three + four); } if(str.toString().equals("0000")) str = new StringBuilder("0"); else if(str.toString().equals("1111")) str = new StringBuilder("1"); else str = new StringBuilder("("+str+")"); } return str.toString(); } } | cs |
'Algorithm' 카테고리의 다른 글
백준 1834번: 나머지와 몫이 같은 수 (0) | 2018.09.22 |
---|---|
백준 1780번: 종이의 개수 (0) | 2018.08.08 |
Divide and Conquer (분할정복법) (0) | 2018.08.07 |
백준 2504번: 괄호의 값 (0) | 2018.08.01 |
백준 1874번: 스택 수열 (0) | 2018.07.23 |