백준 2630 색종이 만들기

김민영·2023년 1월 16일
0

알고리즘

목록 보기
75/125

과정

  • 재귀 사용하기
  • 4등분 한 종이에서 색이 전부 같지 않으면, 다시 4등분해서 색을 확인한다.
  • 색이 전부 같으면 색깔 추가해준다.
  • 개수만 확인하면 되니 전역 변수에 카운트 하고 return을 따로 주지 않았다.
import sys
input = sys.stdin.readline

N = int(input())
graph = [list(map(int, input().split())) for _ in range(N)]

white = 0
blue = 0

def paper(x, y, N):
    global white, blue

    isSame = True  # 색깔 같은지 다른지 플래그
    color = graph[y][x]

    # 첫 번쨰와 색이 다른게 있으면, 플래그 바꿈
    for i in range(N):
        for j in range(N):
            if graph[y+j][x+i] != color:
                isSame = False
                break
        if not isSame:
            break

    if isSame:
        if color == 1:
            blue += 1
        else:
            white += 1
    else:
        paper(x, y, N//2)
        paper(x, y + N//2, N//2)
        paper(x + N//2, y, N//2)
        paper(x + N//2, y + N//2, N//2)

paper(0, 0, N)
print(white)
print(blue)
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글