[백준 2630] 색종이 만들기

코뉴·2021년 8월 13일
0

백준🍳

목록 보기
52/149

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

🥚문제


🥚입력/출력


🍳코드

from sys import stdin
input = stdin.readline

# 하얀색 색종이의 개수
white = 0
# 파란색 색종이의 개수
blue = 0

# 재귀 함수
# n = 한 변의 길이, r = 좌측 최상단의 row idx, c = 좌측 최상단의 col idx
def solve(n, r, c):
    # 전역 변수
    global white
    global blue

    # stop case: 한 변의 길이가 1
    if n == 1:
        if paper[r][c] == 0:
            white += 1
        else:
            blue += 1
        return

    # stop case: 모든 종이에 같은 색 칠해짐
    all_same = True
    prev_color = None
    for x in range(r, r+n):
        for y in range(c, c+n):
            # 가장 첫 구역
            if x == r and y == c:
                prev_color = paper[x][y]
            else:
                # 다른 색이 존재함
                if paper[x][y] != prev_color:
                    all_same = False
                    break
    if all_same:
        if prev_color == 0:
            white += 1
        else:
            blue += 1
        return

    # 계속해서 재귀함수 호출
    next_n = n//2
    solve(next_n, r, c)
    solve(next_n, r, c + next_n)
    solve(next_n, r + next_n, c)
    solve(next_n, r + next_n, c + next_n)

# 입력
n = int(input())

# paper(전체 종이) 입력
paper = []
for _ in range(n):
    data = list(map(int, input().split()))
    paper.append(data)

# 재귀함수 호출
solve(n, 0, 0)

# 하얀색 색종이의 개수
print(white)
# 파란색 색종이의 개수
print(blue)

🧂아이디어


단순한 재귀문제였음!

profile
코뉴의 도딩기록

0개의 댓글