BOJ/미해결

BOJ 2630 - 색종이 만들기

yanJuicy 2024. 8. 20. 23:02
반응형

문제

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

 

 

풀이

분할 정복으로 문제를 해결한다

 

(0~n, 0~n) 구역이 모두 같은 색이면 해당 색깔의 종이 수를 1 더하고 함수를 종료한다

모두 같은 색이 아니라면 다음 4구역으로 나눈 후 각 구역에서 모두 같은 색인지 확인한다

(0~n/2, 0~n/2)

(n/2~n, 0~n/2)

(0~n/2, 0/2~n)

(n/2~n, n/2~n)

 

 

코드

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def solve(y, x, n):
    global white, blue
    reference_color = color_paper[y][x]
 
    for i in range(y, y + n):
        for j in range(x, x + n):
            if reference_color != color_paper[i][j]:
                solve(y, x, n // 2)
                solve(y, x + n // 2, n // 2)
                solve(y + n // 2, x, n // 2)
                solve(y + n // 2, x + n // 2, n // 2)
                return
 
    if reference_color == 0:
        white += 1
    else:
        blue += 1
 
 
white = 0
blue = 0
 
= int(input())
color_paper = [list(map(int, input().split())) for _ in range(n)]
 
solve(00, n)
print(white)
print(blue)
 
cs

 

반응형

'BOJ > 미해결' 카테고리의 다른 글

BOJ 14500 - 테트로미노  (0) 2024.08.27
BOJ 1436 - 영화감독 숌  (0) 2024.08.23
BOJ 2477 - 참외밭  (0) 2024.08.19
BOJ 1489 - 대결  (0) 2024.08.17
BOJ 1071 - 소트  (0) 2024.08.16