728x90
(1) 문제
- 흑백 영상을 압축하여 표현하는 데이터 구조로 쿼드 트리(Quad Tree)라는 방법이 있다. 흰 점을 나타내는 0과 검은 점을 나타내는 1로만 이루어진 영상(2차원 배열)에서 같은 숫자의 점들이 한 곳에 많이 몰려있으면, 쿼드 트리에서는 이를 압축하여 간단히 표현할 수 있다.
- 주어진 영상이 모두 0으로만 되어 있으면 압축 결과는 "0"이 되고, 모두 1로만 되어 있으면 압축 결과는 "1"이 된다. 만약 0과 1이 섞여 있으면 전체를 한 번에 나타내지를 못하고, 왼쪽 위, 오른쪽 위, 왼쪽 아래, 오른쪽 아래, 이렇게 4개의 영상으로 나누어 압축하게 되며, 이 4개의 영역을 압축한 결과를 차례대로 괄호 안에 묶어서 표현한다
- 위 그림에서 왼쪽의 영상은 오른쪽의 배열과 같이 숫자로 주어지며, 이 영상을 쿼드 트리 구조를 이용하여 압축하면 "(0(0011)(0(0111)01)1)"로 표현된다. N ×N 크기의 영상이 주어질 때, 이 영상을 압축한 결과를 출력하는 프로그램을 작성하시오.
(2) 입력
- 첫째 줄에는 영상의 크기를 나타내는 숫자 N 이 주어진다. N 은 언제나 2의 제곱수로 주어지며, 1 ≤ N ≤ 64의 범위를 가진다. 두 번째 줄부터는 길이 N의 문자열이 N개 들어온다. 각 문자열은 0 또는 1의 숫자로 이루어져 있으며, 영상의 각 점들을 나타낸다.
(3) 출력
- 영상을 압축한 결과를 출력한다.
(4) 예제 입력 및 출력
(5) 코드
import sys
import math
square_length = int(sys.stdin.readline())
square_list = []
square_store = []
def DFS(square_arrow_list):
square = square_arrow_list
half_size = len(square) // 2
square_left_up_list = []
square_rigth_up_list = []
square_left_down_list = []
square_right_down_list = []
for i in range(half_size):
temp_left_up_list = []
temp_rigth_up_list = []
temp_left_down_list = []
temp_right_down_list = []
for j in range(half_size):
# 왼쪽 위
temp_left_up_list.append(square[i][j])
# 오른쪽 위
temp_rigth_up_list.append(square[i][j + half_size])
# 왼쪽 아래
temp_left_down_list.append(square[i + half_size][j])
# 오른쪽 아래
temp_right_down_list.append(square[i + half_size][j + half_size])
square_left_up_list.append(temp_left_up_list)
square_rigth_up_list.append(temp_rigth_up_list)
square_left_down_list.append(temp_left_down_list)
square_right_down_list.append(temp_right_down_list)
print('(', end='')
is_zero_one(square_store, square_left_up_list)
is_zero_one(square_store, square_rigth_up_list)
is_zero_one(square_store, square_left_down_list)
is_zero_one(square_store, square_right_down_list)
print(')', end='')
def is_zero_one(square_store, square_arrow_list):
is_zero = False
is_one = False
for row in square_arrow_list:
if not is_one or not is_zero:
if 1 in row:
is_one = True
if 0 in row:
is_zero = True
else:
break
if is_one and is_zero:
DFS(square_arrow_list)
square_store.append(square_arrow_list)
elif not is_zero:
print(1, end='')
else:
print(0, end='')
for i in range(square_length):
string = sys.stdin.readline().rstrip('\n');
temp_list = []
for char in string:
temp_list.append(int(char))
square_list.append(temp_list)
is_zero_one(square_store, square_list)
(6) 실행결과
반응형
'BaekJoon Algorithm > Python' 카테고리의 다른 글
[백준알고리즘 - 2667] 단지번호붙이기(Python) (0) | 2021.04.11 |
---|---|
[백준알고리즘 - 11054] 가장 긴 바이토닉 부분 수열 (Python) (0) | 2021.04.11 |
[백준알고리즘 - 2231] 요세푸스 문제 0 (Python) (0) | 2021.03.28 |
[백준알고리즘 - 2231] 분해합 (Python) (0) | 2021.03.28 |
[백준알고리즘 - 2798] 블랙잭 (Python) (0) | 2021.03.28 |