File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .readline
3+
4+ def quad_tree (x_start , y_start , n ):
5+ global answer
6+ cnt = 0 # 압축이 가능한 지 여부를 확인하기 위한 변수
7+ for i in range (x_start , x_start + n ):
8+ for j in range (y_start , y_start + n ):
9+ cnt += grid [i ][j ]
10+
11+ if cnt == 0 : # 다 더한 값이 0이라면 0으로 압축 가능
12+ answer += "0"
13+ elif cnt == n * n : # 다 더한 값이 1*n*n이라면 1로 압축 가능
14+ answer += "1"
15+ else : # 해당 사항 없으면 분할
16+ answer += "(" # 가장 먼저 괄호 열고
17+ quad_tree (x_start , y_start , n // 2 ) # 왼쪽 위
18+ quad_tree (x_start , y_start + n // 2 , n // 2 ) # 오른쪽 위
19+ quad_tree (x_start + n // 2 , y_start , n // 2 ) # 왼쪽 아래
20+ quad_tree (x_start + n // 2 , y_start + n // 2 , n // 2 ) # 오른쪽 아래
21+ answer += ")" # 호출이 모두 끝날 경우 괄호 닫음
22+
23+ n = int (input ()) # 영상의 크기
24+ grid = [] # 영상 배열
25+ for _ in range (n ):
26+ li = input ().strip ()
27+ li = list (map (int , li )) # 문자열을 int형으로 변환하여 리스트로 저장
28+ grid .append (li )
29+
30+ answer = ""
31+ quad_tree (0 , 0 , n )
32+
33+ # 정답 출력
34+ print (answer )
You can’t perform that action at this time.
0 commit comments