코딩테스트 준비
재귀, 분할정복 - 백준 쿼드트리
언어 수집가
2021. 2. 25. 12:46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
n = int(input()) | |
arr = [] | |
for i in range(n): | |
arr.append(list(map(int, sys.stdin.readline().rstrip()))) | |
def quad_tree(v, x, y): | |
if v == 1: | |
return arr[x][y] | |
else: | |
a = quad_tree(v//2, x, y) | |
b = quad_tree(v//2, x, y + v//2) | |
c = quad_tree(v//2, x + v//2, y) | |
d = quad_tree(v//2, x + v//2, y + v//2) | |
if a == b == c == d and len(str(a)) == 1: | |
return str(a) | |
else: | |
return '(' + str(a) + str(b) + str(c) + str(d) + ')' | |
print(quad_tree(n, 0, 0)) |
if a == b == c == d and len(str(a)) == 1: 에서 len(str(a)) == 1 처리를 해주지 않으면
00110011
00110011
00110011
00110011
00110011
00110011
00110011
00110011와
0011
0011
0011
0011을 입력했을 때 값이 똑같이 (0101)로 나온다.