본문 바로가기
Algorithm/etc

[파이썬] 백준 1780 : 종이의 개수 (실버2)

by 베짱이28호 2023. 6. 5.

[파이썬] 백준 1780 : 종이의 개수 (실버2)

 

1780번: 종이의 개수

N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1 중 하나가 저장되어 있다. 우리는 이 행렬을 다음과 같은 규칙에 따라 적절한 크기로 자르려고 한다. 만약 종이가 모두 같은 수

www.acmicpc.net

 


문제


풀이

0.방향성 생각

첫 케이스에서 어떻게 움직일지 규칙을 찾고 하드코딩 해준다.

함수 내에서 조건을 만족하면 재귀호출한다.

1. 전체 코드

import sys
input = sys.stdin.readline

n = int(input())
arr = []
p,m,z = 0,0,0
for i in range(n):
    arr.append(list(map(int,input().split())))

def solution(x,y,N) :
    global p,m,z
    color = arr[y][x]
    for i in range(y,y+N) :
        for j in range(x,x+N) :
            d = N//3
            if color != arr[i][j] :
                solution(x,y,d)
                solution(x+d,y,d)
                solution(x+(2*d),y,d)
                
                solution(x,y+d,d)
                solution(x+d,y+d,d)
                solution(x+(2*d),y+d,d)
                
                solution(x,y+(2*d),d)
                solution(x+d,y+(2*d),d)
                solution(x+(2*d),y+(2*d),d)
                return 

    if color == 1:
        p += 1
    if color == 0 :
        z += 1
    if color == -1 :
        m += 1

solution(0,0,n)
print(m,z,p,sep='\n')

 

 

 

댓글