문제: https://www.acmicpc.net/problem/1992
import java.util.*;
public class Main {
private static final Scanner sc = new Scanner(System.in);
private static char[][] quadTree;
public static void main(String args[]) {
int num = sc.nextInt();
quadTree = new char[num][num];
for (int i=0; i< num; i++) {
quadTree[i] = sc.next().toCharArray();
// 연결된 애들 떼어내기 위해서 그런것
}
solve(0, 0, num);
// quadTree print하기
// for (int i = 0; i < num; i++) {
// System.out.println(Arrays.toString(quadTree[i]));
// }
}
public static boolean same (int x, int y, int n) {
for (int i = x; i < x+n; i++) {
for (int j = y; j < y+n; j++) {
if (quadTree[x][y] != quadTree[i][j]) {
return false;
}
}
}
return true;
}
public static void solve (int x, int y, int n) {
if (same(x, y, n)){
System.out.print(quadTree[x][y]);
} else {
System.out.print("(");
int m = n/2;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
solve(x+m*i, y+m*j, m);
}
}
System.out.print(")");
}
}
}