2025-02-15 17:04:47

분할정복문제

 

import sys
n=int(sys.stdin.readline().rstrip())
graph=[]
total=[0,0]
for i in range(n):
    graph.append(list(map(int,sys.stdin.readline().rstrip().split())))

def check_all(start_row,start_col,size):
    for i in range(start_row,start_row+size):
        for j in range(start_col,start_col+size):
            if graph[i][j]!=graph[start_row][start_col]:
                return 0
    return 1

def divide(start_row,start_col,size):
    if size==1:
        if graph[start_row][start_col]==1:
            total[1]+=1
        else:
            total[0]+=1
        return
    if check_all(start_row,start_col,size)==1:
        total[graph[start_row][start_col]]+=1
        return
    divide(start_row,start_col,size//2)
    divide(start_row+size//2,start_col,size//2)
    divide(start_row,start_col+size//2,size//2)
    divide(start_row+size//2,start_col+size//2,size//2)
divide(0,0,n)
print(total[0])
print(total[1])

'IT > ps' 카테고리의 다른 글

백준 30804번 과일 탕후루  (0) 2025.02.15
백준 2805번 나무 자르기  (0) 2025.02.15
백준 1541번 잃어버린 괄호  (0) 2025.02.14
백준 1012번 유기농 배추  (0) 2025.02.14
백준 17626번 four squares  (0) 2025.02.14