Contents

BEAKJOON 2630, 1992

2630_색종이 만들기

 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
30
31
32
33
34
import sys

def DFS(x, y, N):
    global W, B

    color = arr[x][y]
    for cx in range(x, x+N):
        for cy in range(y, y+N):
            if arr[cx][cy] != color:
                DFS(x, y, N//2)
                DFS(x, N//2+y, N//2)
                DFS(N//2+x, y, N//2)
                DFS(N//2+x, N//2+y, N//2)
                return		# 이걸 안해주면 쓸모 없는 DFS에 더 들어가게 된다.
                
    if color == 0:
        W += 1
        return
    else:
        B += 1
        return

N = int(sys.stdin.readline().rstrip())

arr = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

W, B = 0, 0 

DFS(0, 0, N)
print(W)
print(B)

# 분할한 구역에도 동일한 규칙을 적용할 수 있는 
# 알고리즘을 만드는 것이 기본 같다.

1992_쿼드트리

 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
30
import sys

def DFS(x, y, N):
    global result

    check = arr[x][y]
    for i in range(x, x+N):
        for j in range(y, y+N):
            if check != arr[i][j]:
                result += "("
                DFS(x, y, N//2)
                DFS(x, N//2+y, N//2)
                DFS(N//2+x, y, N//2)
                DFS(N//2+x, N//2+y, N//2)   
                result += ")"
                return
    if check == 0:
        result += "0"
    else:
        result += "1"

N = int(sys.stdin.readline().rstrip())

arr  = [list(map(int, sys.stdin.readline().rstrip())) for _ in range(N)]

result = ""

DFS(0, 0, N)

print(result)