알고리즘 테스트/백준
백준 2630: 색종이 만들기 (파이썬)
codeyaki
2022. 3. 29. 17:34
반응형
문제: https://www.acmicpc.net/problem/2630
2630번: 색종이 만들기
첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.
www.acmicpc.net
코드
#https://teching.tistory.com/
import sys
cntWhite = 0
cntBlue = 0
def makeConftti(paper):
global cntBlue, cntWhite
tmp = 0
for line in paper:
tmp += sum(line)
n = len(paper)
if tmp == n*n:
cntBlue += 1
return
elif tmp == 0:
cntWhite += 1
return
#가로 세로 쪼개기
splitPaper1 =[]
splitPaper2 =[]
splitPaper3 =[]
splitPaper4 =[]
for i in range(n):
if i <= n//2 - 1 :
splitPaper1.append(paper[i][:n//2])
splitPaper2.append(paper[i][n//2:])
if i > n//2 - 1:
splitPaper3.append(paper[i][:n//2])
splitPaper4.append(paper[i][n//2:])
makeConftti(splitPaper1)
makeConftti(splitPaper2)
makeConftti(splitPaper3)
makeConftti(splitPaper4)
n = int(sys.stdin.readline().rstrip())
inputPaper = []
for _ in range(n):
inputPaper.append(list(map(int,sys.stdin.readline().split())))
makeConftti(inputPaper)
print(cntWhite)
print(cntBlue)
해설
단순하게 재귀를 이용해서 만드는데 종이가 전부 같은 색이면 종료,
같은 색이 아닐 경우 반으로 쪼개서 재귀를 하도록 만들었다.
반응형