728x90
반응형
https://www.acmicpc.net/problem/12100
import sys
input = sys.stdin.readline
N = int(input())
board = [list(map(int, input().split())) for _ in range(N)]
result = 0
def deep_copy_board(board):
newBoard = []
for i in range(N):
elems = []
for j in range(N):
elems.append(board[i][j])
newBoard.append(elems)
return newBoard
def move(cur_board, cnt):
if cnt >= 5:
global result
for x in range(N):
for y in range(N):
result = max(result, cur_board[x][y])
return
# top
newBoard = deep_copy_board(cur_board)
for y in range(N):
hasReposition = False
for x in range(N-1):
if newBoard[x][y] == 0:
hasReposition = True
continue
for nx in range(x+1, N):
if newBoard[nx][y] == 0:
hasReposition = True
continue
elif newBoard[x][y] == newBoard[nx][y]:
newBoard[x][y] *= 2
newBoard[nx][y] = 0
hasReposition = True
break
else:
break
if hasReposition:
newElem = []
for x in range(N):
if newBoard[x][y] != 0:
newElem.append(newBoard[x][y])
while len(newElem) < N:
newElem.append(0)
for x in range(N):
newBoard[x][y] = newElem[x]
move(newBoard, cnt+1)
# bottom
newBoard = deep_copy_board(cur_board)
for y in range(N):
hasReposition = False
for x in range(N-1, 0, -1):
if newBoard[x][y] == 0:
hasReposition = True
continue
for nx in range(x-1, -1, -1):
if newBoard[nx][y] == 0:
hasReposition = True
continue
elif newBoard[x][y] == newBoard[nx][y]:
newBoard[x][y] *= 2
newBoard[nx][y] = 0
hasReposition = True
break
else:
break
if hasReposition:
newElem = []
for x in range(N-1, -1, -1):
if newBoard[x][y] != 0:
newElem.append(newBoard[x][y])
while len(newElem) < N:
newElem.append(0)
i = 0
for x in range(N-1, -1, -1):
newBoard[x][y] = newElem[i]
i += 1
move(newBoard, cnt + 1)
# left
newBoard = deep_copy_board(cur_board)
for x in range(N):
hasReposition = False
for y in range(N - 1):
if newBoard[x][y] == 0:
hasReposition = True
continue
for ny in range(y + 1, N):
if newBoard[x][ny] == 0:
hasReposition = True
continue
elif newBoard[x][y] == newBoard[x][ny]:
newBoard[x][y] *= 2
newBoard[x][ny] = 0
hasReposition = True
break
else:
break
if hasReposition:
newElem = []
for y in range(N):
if newBoard[x][y] != 0:
newElem.append(newBoard[x][y])
while len(newElem) < N:
newElem.append(0)
for y in range(N):
newBoard[x][y] = newElem[y]
move(newBoard, cnt + 1)
# right
newBoard = deep_copy_board(cur_board)
for x in range(N):
hasReposition = False
for y in range(N-1, 0, -1):
if newBoard[x][y] == 0:
hasReposition = True
continue
for ny in range(y-1, -1, -1):
if newBoard[x][ny] == 0:
hasReposition = True
continue
elif newBoard[x][y] == newBoard[x][ny]:
newBoard[x][y] *= 2
newBoard[x][ny] = 0
hasReposition = True
break
else:
break
if hasReposition:
newElem = []
for y in range(N-1, -1, -1):
if newBoard[x][y] != 0:
newElem.append(newBoard[x][y])
while len(newElem) < N:
newElem.append(0)
i = 0
for y in range(N-1, -1, -1):
newBoard[x][y] = newElem[i]
i += 1
move(newBoard, cnt + 1)
if N == 1:
result = board[0][0]
else:
move(board, 0)
print(result)
728x90
반응형
'Algorithm > Baekjoon' 카테고리의 다른 글
BOJ 13458 시험 감독 Python3 (0) | 2023.07.03 |
---|---|
BOJ 1463 1로 만들기 Python3 (0) | 2023.06.28 |
BOJ 1992 쿼드트리 python3 (0) | 2023.06.22 |
17142 java 연습 (0) | 2021.08.12 |
셀프 넘버(4673번 문제) Python3 답안 (0) | 2020.02.09 |