BOJ - 1992 - 쿼드트리

Updated:

import sys

def recursive(matrix, x1, y1, x2, y2, n):
    if n == 1:
        return matrix[y1][x1]
    a = n // 2

    q1 = recursive(matrix, x1, y1, x1 + a, y1 + a, a)
    q2 = recursive(matrix, x1 + a, y1, x1 + n, y1 + a, a)
    q3 = recursive(matrix, x1, y1 + a, x1 + a, y1 + n, a)
    q4 = recursive(matrix, x1 + a, y1 + a, x1 + n, y1 + n, a)

    if q1 == q2 == q3 == q4 and len(q1) == 1:
        return q1

    return "(" + q1 + q2 + q3 + q4 + ")"

def solution():
    X = int(input())
    matrix = []
    for _ in range(X):
        matrix.append(list(str(sys.stdin.readline().strip())))

    print(recursive(matrix, 0, 0, X, X, X))
solution()

https://www.acmicpc.net/problem/1992

Categories:

Updated:

Leave a comment