-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_2048.py
104 lines (90 loc) · 3.8 KB
/
BOJ_2048.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 이동 구현: 스택 (한번 합쳐진 놈은 1로 마킹)
from itertools import product
N = int(input())
table = [list(map(int, input().split())) for _ in range(N)]
result = 0
for p in list(product([0, 1, 2, 3], repeat=5)):
new_table = [line[:] for line in table]
def move(d):
if d == 0:
for i in range(N):
stack = []
for j in range(N):
if new_table[i][j] != 0:
if not stack or stack[-1][0] != new_table[i][j]:
stack.append((new_table[i][j], 0))
new_table[i][j] = 0
else:
if stack[-1][1] == 1:
stack.append((new_table[i][j], 0))
new_table[i][j] = 0
else:
val = stack.pop()[0]
stack.append((2*val, 1))
new_table[i][j] = 0
for k in range(len(stack)):
new_table[i][k] = stack[k][0]
elif d == 1:
for i in range(N):
stack = []
for j in range(N-1, -1, -1):
if new_table[i][j] != 0:
if not stack or stack[-1][0] != new_table[i][j]:
stack.append((new_table[i][j], 0))
new_table[i][j] = 0
else:
if stack[-1][1] == 1:
stack.append((new_table[i][j], 0))
new_table[i][j] = 0
else:
val = stack.pop()[0]
stack.append((2 * val, 1))
new_table[i][j] = 0
for k in range(len(stack)):
new_table[i][N-k-1] = stack[k][0]
elif d == 2:
for j in range(N):
stack = []
for i in range(N):
if new_table[i][j] != 0:
if not stack or stack[-1][0] != new_table[i][j]:
stack.append((new_table[i][j], 0))
new_table[i][j] = 0
else:
if stack[-1][1] == 1:
stack.append((new_table[i][j], 0))
new_table[i][j] = 0
else:
val = stack.pop()[0]
stack.append((2 * val, 1))
new_table[i][j] = 0
for k in range(len(stack)):
new_table[k][j] = stack[k][0]
elif d == 3:
for j in range(N):
stack = []
for i in range(N-1, -1, -1):
if new_table[i][j] != 0:
if not stack or stack[-1][0] != new_table[i][j]:
stack.append((new_table[i][j], 0))
new_table[i][j] = 0
else:
if stack[-1][1] == 1:
stack.append((new_table[i][j], 0))
new_table[i][j] = 0
else:
val = stack.pop()[0]
stack.append((2 * val, 1))
new_table[i][j] = 0
for k in range(len(stack)):
new_table[N-k-1][j] = stack[k][0]
for d in p:
move(d)
# print(p)
# for line in new_table:
# print(line)
# print()
for i in range(N):
for j in range(N):
result = max(result, new_table[i][j])
print(result)