1780. 종이의 개수
업데이트 시간 : 2023-03-01 23:56:40 +0000[Silver II] 종이의 개수 - 1780
성능 요약
메모리: 69340 KB, 시간: 5088 ms
분류
분할 정복(divide_and_conquer), 재귀(recursion)
문제 설명
N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1 중 하나가 저장되어 있다. 우리는 이 행렬을 다음과 같은 규칙에 따라 적절한 크기로 자르려고 한다.
- 만약 종이가 모두 같은 수로 되어 있다면 이 종이를 그대로 사용한다.
- (1)이 아닌 경우에는 종이를 같은 크기의 종이 9개로 자르고, 각각의 잘린 종이에 대해서 (1)의 과정을 반복한다.
이와 같이 종이를 잘랐을 때, -1로만 채워진 종이의 개수, 0으로만 채워진 종이의 개수, 1로만 채워진 종이의 개수를 구해내는 프로그램을 작성하시오.
입력
첫째 줄에 N(1 ≤ N ≤ 37, N은 3k 꼴)이 주어진다. 다음 N개의 줄에는 N개의 정수로 행렬이 주어진다.
출력
첫째 줄에 -1로만 채워진 종이의 개수를, 둘째 줄에 0으로만 채워진 종이의 개수를, 셋째 줄에 1로만 채워진 종이의 개수를 출력한다.
💡 Solutions
📄 종이의 개수.py
N = int(input())
pap = [list(map(int,input().split())) for _ in range(N)]
ans = { -1:0, 0:0, 1:0 }
def checker(row, col, size):
global pap
target = pap[row][col]
for i in range(row, row + size):
for j in range(col, col + size):
if target != pap[i][j]:
return False
return True
def solution(row, col, size):
global ans
if checker(row, col, size):
ans[pap[row][col]] += 1
return
n_size = size//3
for i in range(3):
for j in range(3):
# node = (row + i * n_size, col + j * n_size, n_size)
solution(row + i * n_size, col + j * n_size, n_size)
# node1 = (row, col, n_size)
# node2 = (row, col + n_size , n_size)
# node3 = (row, col + 2 * n_size , n_size)
# node4 = (row + n_size, col, n_size)
# node5 = (row + n_size, col + n_size , n_size)
# node6 = (row + n_size, col + 2 * n_size , n_size)
# node7 = (row + 2 * n_size, col, n_size)
# node8 = (row + 2 * n_size, col + n_size , n_size)
# node9 = (row + 2 * n_size, col + 2 * n_size , n_size)
solution(0,0,N)
print(*ans.values(), sep='\n')