Skip to content

Commit 4856743

Browse files
committed
[BOJ] 색종이 만들기 / 실버 2 / 30분 힌트 사용
https://www.acmicpc.net/problem/2630
1 parent 4025dbf commit 4856743

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
3+
4+
# 주어진 정사각형이 모두 같은 색인지 확인하는 함수
5+
def checkRectangle(arr, rowStart, rowEnd, colStart, colEnd):
6+
global ans1, ans2
7+
8+
# 첫 번째 요소로 초기 색상 설정
9+
initial = arr[rowStart][colStart]
10+
11+
# 주어진 정사각형이 모두 같은 색인지 확인
12+
for i in range(rowStart, rowEnd):
13+
for j in range(colStart, colEnd):
14+
if arr[i][j] != initial:
15+
# 모두 같은 색이 아니면 4개의 작은 정사각형으로 분할하여 재귀 호출
16+
midRow = (rowStart + rowEnd) // 2
17+
midCol = (colStart + colEnd) // 2
18+
checkRectangle(arr, rowStart, midRow, colStart, midCol)
19+
checkRectangle(arr, rowStart, midRow, midCol, colEnd)
20+
checkRectangle(arr, midRow, rowEnd, colStart, midCol)
21+
checkRectangle(arr, midRow, rowEnd, midCol, colEnd)
22+
return
23+
24+
if initial == 0:
25+
ans1 += 1
26+
else:
27+
ans2 += 1
28+
29+
30+
n = int(sys.stdin.readline().strip())
31+
32+
ans1 = 0
33+
ans2 = 0
34+
arr = []
35+
36+
for _ in range(n):
37+
arr.append(list(map(int, sys.stdin.readline().strip().split())))
38+
39+
checkRectangle(arr, 0, n, 0, n)
40+
41+
print(ans1)
42+
print(ans2)

0 commit comments

Comments
 (0)