목차

색종이 만들기

ps
링크acmicpc.net/…
출처BOJ
문제 번호2630
문제명색종이 만들기
레벨실버 3
분류

기초

시간복잡도O(n^2)
인풋사이즈n<=128
사용한 언어Python
제출기록29200KB / 92ms
최고기록64ms
해결날짜2021/07/27

풀이

코드

"""Solution code for "BOJ 2630. 색종이 만들기".

- Problem link: https://www.acmicpc.net/problem/2630
- Solution link: http://www.teferi.net/ps/problems/boj/2630
"""

WHITE = '0'
BLUE = '1'


def main():
    N = int(input())
    colors = [input().split() for _ in range(N)]
    white_counts = []
    blue_counts = []
    for row in colors:
        white_counts.append([int(col == WHITE) for col in row])
        blue_counts.append([int(col == BLUE) for col in row])

    size = 1
    while size < N:
        delta = [(0, 0), (0, size), (size, 0), (size, size)]
        size *= 2
        for r in range(0, N, size):
            for c in range(0, N, size):
                white = sum(white_counts[r + dr][c + dc] for dr, dc in delta)
                blue = sum(blue_counts[r + dr][c + dc] for dr, dc in delta)
                if white == 4 and blue == 0:
                    white = 1
                elif white == 0 and blue == 4:
                    blue = 1
                white_counts[r][c], blue_counts[r][c] = white, blue

    print(white_counts[0][0])
    print(blue_counts[0][0])


if __name__ == '__main__':
    main()