Beakjoon&프로그래머스/파이썬
[백준/파이썬] 1992번 쿼드트리
현장
2022. 1. 25. 17:34
-코드
def sol(x, y, num):
color = arr[x][y]
for i in range(x, x + num):
for j in range(y, y + num):
if color != arr[i][j]:
print('(', end='')
sol(x, y, num // 2)
sol(x, y + num // 2, num // 2)
sol(x + num // 2, y, num // 2)
sol(x + num // 2, y + num // 2, num // 2)
print(')', end='')
return
if color == 1:
print(1, end='')
else:
print(0, end='')
n = int(input())
arr = [list(map(int, input())) for _ in range(n)]
sol(0, 0, n)
처음에 문제를 이해를 못 해서 돌아가는 원리를 찾아보니 전에 풀었던 분할 정복과 비슷하여 해결할 수 있었습니다.