[백준/Python]2630_색종이 만들기

2021. 6. 7. 16:03백준 알고리즘

문제

https://www.acmicpc.net/problem/2630

 

2630번: 색종이 만들기

첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.

www.acmicpc.net

풀이

현재 선택된 색종이의 색이 모두 같은 색으로 칠해져 있는지 확인하는 check_color 메서드를 선언합니다.

재귀로 check_color의 결과값이 False인 색종이는 4등분하는 solution 메서드를 선언합니다.

코드

import sys


def check_color(y, x, length):
    color = matrix[y][x]
    for i in range(y, y + length):
        for j in range(x, x + length):
            if matrix[i][j] != color:
                return False
    return True


def solution(y, x, length):
    global white, blue
    color = matrix[y][x]
    if check_color(y, x, length):
        if color == 0:
            white += 1
        elif color == 1:
            blue += 1
        return
    solution(y, x, length // 2)
    solution(y + length // 2, x, length // 2)
    solution(y, x + length // 2, length // 2)
    solution(y + length // 2, x + length // 2, length // 2)


n = int(sys.stdin.readline().rstrip())
matrix = []
white, blue = 0, 0
for _ in range(n):
    matrix.append(list(map(int, sys.stdin.readline().rstrip().split())))
solution(0, 0, n)
print(white)
print(blue)

'백준 알고리즘' 카테고리의 다른 글

[백준/Python]9613_GCD 합  (0) 2021.06.08
[백준/Python]1890_점프  (0) 2021.06.07
[백준/Python]1004_어린 왕자  (0) 2021.06.05
[백준/Python]2293_동전 1  (0) 2021.06.05
[백준/Python]11659_구간 합 구하기 4  (0) 2021.06.04