히치키치

[백준] 2630번 : 색종이 만들기 - Python(파이썬) 본문

카테고리 없음

[백준] 2630번 : 색종이 만들기 - Python(파이썬)

히치키치 2022. 6. 13. 23:40

문제

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

 

2630번: 색종이 만들기

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

www.acmicpc.net

 

아이디어

1. 재귀 시점

색깔이 다르면 4등분으로 나눔

2. 종료 시점

4개로 나눈 것에 대한 탐색 함수 각각 실행 완료 후 종료

3. 결과 처리

조각을 다 돌고 같지 않은 색깔이 없는 경우 해당 조각의 색깔을 res 배열에 저장

res 배열 내 색깔별 갯수를 출력해 결과냄

 

전체 코드

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

import sys
input=sys.stdin.readline

N=int(input())
arr=[list(map(int, input().split())) for _ in range(N)]
#print(N,arr)
res=[]

def split(n,x,y):
    color=arr[x][y]
    #print(color)
    for i in range(x,x+n):
        for j in range(y,y+n):
            if arr[i][j]!=color:
                split(n//2,x,y)
                split(n//2,x,y+n//2)
                split(n//2,x+n//2,y)
                split(n//2,x+n//2,y+n//2)
                return
    if color==0:
       res.append(0)
    else:
       res.append(1)

split(N,0,0)
print(res.count(0))
print(res.count(1))
Comments