728x90
반응형
SMALL
구현은 그렇게 어렵지 않은 문제였습니다.
우선 어떤 게임인지 알고 있었기 때문에 이해가 빨라서 1시간에 넉넉하게 푼듯합니다. 다만 각 분기 처리에 대해서 고민을 깊게 하지 않으면 훨씬 오래 걸렸을 듯 합니다.
특히 14퍼센트에서 에러가 잡혀서 봤더니, 0처리를 안해줬었더라는....
예를들어
테스트케이스
4
2 0 0 2
0 0 0 0
0 0 0 0
2 0 0 2
의 경우.
왼쪽으로 옮기면
4 0 0 0
0 0 0 0
0 0 0 0
4 0 0 0
이 되야 하지만
0 처리를 해주지 않으면
2 2 0 0
0 0 0 0
0 0 0 0
2 2 0 0
이 되버립니다.
분명 더 좋은 코드가 많을텐데...
여전히 공부가 많이 필요합니다.
import sys, copy
sys.setrecursionlimit(10 ** 5)
n = int(sys.stdin.readline())
answer = 0
check = []
graph = [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
def trans_left_right(graph):
for y in range(len(graph)):
graph[y] = list(reversed(graph[y]))
return graph
def zero_management(graph):
for y in range(len(graph)):
for x in range(len(graph[y]) - 1):
if graph[y][x] == 0:
graph[y][x] = "empty"
continue
while "empty" in graph[y]:
graph[y].remove("empty")
graph[y].append(0)
return graph
def find_answer(graph):
global answer
for y in range(len(graph)):
for x in range(len(graph[y])):
answer = max(answer, graph[y][x])
return
def sum_left(graph):
graph = zero_management(graph)
for y in range(len(graph)):
flag = False
for x in range(len(graph[y]) - 1):
if flag:
flag = False
continue
if graph[y][x] == graph[y][x + 1]:
graph[y][x] = graph[y][x] + graph[y][x + 1]
graph[y][x + 1] = "empty"
flag = True
while "empty" in graph[y]:
graph[y].remove("empty")
graph[y].append(0)
return graph
def sum_right(graph):
graph = trans_left_right(graph)
graph = zero_management(graph)
for y in range(len(graph)):
flag = False
for x in range(len(graph[y]) - 1):
if flag:
flag = False
continue
if graph[y][x] == graph[y][x + 1]:
graph[y][x] = graph[y][x] + graph[y][x + 1]
graph[y][x + 1] = "empty"
flag = True
while "empty" in graph[y]:
graph[y].remove("empty")
graph[y].append(0)
graph = trans_left_right(graph)
return graph
def sum_up(graph):
graph = list(map(list, zip(*graph)))
graph = trans_left_right(graph)
graph = zero_management(graph)
for y in range(len(graph)):
flag = False
for x in range(len(graph[y]) - 1):
if flag:
flag = False
continue
if graph[y][x] == graph[y][x + 1]:
graph[y][x] = graph[y][x] + graph[y][x + 1]
graph[y][x + 1] = "empty"
flag = True
while "empty" in graph[y]:
graph[y].remove("empty")
graph[y].append(0)
graph = trans_left_right(graph)
graph = list(map(list, zip(*graph)))
return graph
def sum_down(graph):
graph = list(map(list, zip(*graph)))
graph = zero_management(graph)
for y in range(len(graph)):
flag = False
for x in range(len(graph[y]) - 1):
if flag:
flag = False
continue
if graph[y][x] == graph[y][x + 1]:
graph[y][x] = graph[y][x] + graph[y][x + 1]
graph[y][x + 1] = "empty"
flag = True
while "empty" in graph[y]:
graph[y].remove("empty")
graph[y].append(0)
graph = list(map(list, zip(*graph)))
return graph
def dfs(graph, count):
global check
left_graph, right_graph, up_graph, down_graph = copy.deepcopy(graph), copy.deepcopy(graph), copy.deepcopy(graph), copy.deepcopy(graph)
if count == 5:
find_answer(graph)
return
if (graph, count) in check:
return
find_answer(graph)
check.append((graph, count))
left_graph = sum_left(left_graph)
right_graph = sum_right(right_graph)
up_graph = sum_up(up_graph)
down_graph = sum_down(down_graph)
# print("left")
# for i in left_graph:
# print(i)
# print()
# print("right")
# for i in right_graph:
# print(i)
# print()
#
# print("up")
# for i in up_graph:
# print(i)
# print()
#
# print("down")
# for i in down_graph:
# print(i)
# print()
dfs(left_graph, count + 1)
dfs(right_graph, count + 1)
dfs(up_graph, count + 1)
dfs(down_graph, count + 1)
# print(left_graph)
dfs(graph, 0)
print(answer)
728x90
반응형
LIST
'코테' 카테고리의 다른 글
산타의 선물 공장2 (0) | 2023.03.24 |
---|---|
코드트리 빵 (0) | 2023.03.24 |
14503 로봇 청소기 python (0) | 2023.03.23 |
14499 - 주사위 굴리기 (0) | 2023.03.22 |
5373 큐빙 - Python (0) | 2023.03.09 |