Created
February 26, 2020 05:20
-
-
Save inspirit941/bb8df32a843ebea8c07b624b84e691f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from itertools import chain | |
from copy import deepcopy | |
n = int(sys.stdin.readline()) | |
maps = [list(map(int, sys.stdin.readline().split())) for _ in range(n)] | |
max_value = 0 | |
# 앞으로 기울이기 - 0 | |
# 뒤로 기울이기 - 1 | |
# 오른쪽으로 기울이기 - 2 | |
# 왼쪽으로 기울이기 - 3 | |
def move(dirs, maps, count): | |
global max_value | |
result_map = [] | |
# 위 or 아래로 기울일 경우 | |
# 계산을 위해 리스트 transpose | |
if dirs == 0 or dirs == 1: | |
maps = list(map(list, zip(*maps))) | |
for idx in range(len(maps)): | |
each_row = maps[idx] | |
# 0 아닌 값들만 추출 | |
not_zero = [i for i in each_row if i != 0] | |
# 1. 값이 왼쪽으로 합쳐지는 경우. | |
if dirs == 0 or dirs == 3: | |
for i in range(1, len(not_zero)): | |
# 앞 값과 뒤 값이 같을 경우 | |
if not_zero[i-1] == not_zero[i]: | |
not_zero[i-1] += not_zero[i] | |
not_zero[i] = 0 | |
not_zero = [i for i in not_zero if i != 0] | |
# 남은 길이만큼 0 맞춰주기 | |
for _ in range(len(each_row) - len(not_zero)): | |
not_zero.append(0) | |
# 2. 값이 오른쪽으로 합쳐지는 경우 | |
elif dirs == 1 or dirs == 2: | |
for i in range(len(not_zero)-1, 0, -1): | |
if not_zero[i-1] == not_zero[i]: | |
not_zero[i] += not_zero[i-1] | |
not_zero[i-1] = 0 | |
not_zero = [i for i in not_zero if i != 0] | |
# 0을 앞으로 붙여줘야 하는데, insert가 비효율적일 것 같아 | |
# 리스트 reverse로 뒤집은 후 뒤에 0 append + 다시 reverse로 뒤집기 사용 | |
# ex) 2 2 2 -> 2 0 4 -> 2 4. 값을 맞추려면 0 2 4 형태여야 하는데, | |
# 2 4 앞에 0을 직접 넣는 대신 | |
# 4 2로 배열을 뒤집고 0을 넣은 뒤 (4 2 0) -> 0 2 4 형태로 다시 배열을 뒤집는 형태 | |
not_zero = not_zero[::-1] | |
for _ in range(len(each_row) - len(not_zero)): | |
not_zero.append(0) | |
not_zero = not_zero[::-1] | |
result_map.append(not_zero) | |
# # 위 or 아래로 기울이기 == transpose로 원래 배열과 동일하게 변환한다 | |
if dirs == 0 or dirs == 1: | |
result_map = list(map(list, zip(*result_map))) | |
# 얻을 수 있는 최대블록 크기 계산 | |
# 계속 진행한다. | |
if count == 5: | |
max_value = max(max_value, max(list(chain(*result_map)))) | |
return | |
move(0, result_map, count + 1) | |
move(1, result_map, count + 1) | |
move(2, result_map, count + 1) | |
move(3, result_map, count + 1) | |
original_map = deepcopy(maps) | |
for i in range(4): | |
move(i, original_map, 1) | |
print(max_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment