소스코드

import sys
input = sys.stdin.readline
N = int(input().strip())
graph = [list(map(int, input().strip().split())) for _ in range(N)]
result = [0,0] # 하얀색, 파란색 색종이 개수

def solve(size, x, y):
	global result
	
	# 현재 사이즈에서 정사각형을 만족하는지 확인
	standard_color = graph[x][y]
	is_same = True
	for i in range(x, x + size):
		for j in range(y, y + size):
			color = graph[i][j]
			
			# 기준 색깔과 다른 경우
			if color != standard_color:
				is_same = False
				break
		if not is_same:
			break
	
	# 정사각형 사이즈 내에서 모든 색이 동일한 경우
	if is_same == True:
		result[standard_color] += 1
	else:
		# 모든 색이 동일하지 않은 경우
		half = size // 2
		solve(half, x, y) # 1사분면
		solve(half, x, y + half) # 2사분면
		solve(half, x + half, y) # 3사분면
		solve(half, x + half, y + half) # 4사분면

print('\\n'.join(map(str, result)))
import sys
input = sys.stdin.readline
N = int(input().strip())
graph = [list(map(int, input().strip().split())) for _ in range(N)]
result = [0,0] # 하얀색, 파란색 색종이 개수

def solve(x,y,size,result):
	color = graph[x][y]
	
	# 정사각형 사이즈 내에서 하나라도 색이 맞지 않으면 재귀호출(4분면으로 영역을 나눠서)
	for i in range(x, x + size):
		for j in range(y, y + size):
			if graph[i][j] != color:
				half = size // 2
			
				solve(x,y, half, result)
				solve(x, y + half, result)
				solve(x + half, y, result)
				solve(x + half, y + half, result)
				return
	
	if color == 0:
		result[0] += 1
	else:
		result[1] += 1
		
solve(0,0,N,result)
print(f"{result[0]}\\n{result[1]}")

풀이


복기