12100-2048
๋งค ์๋๋ง๋ค 4 ๋ฐฉํฅ์ ๋ชจ๋ DFS ๋ก ์ฌ๊ท๋ฅผ ํ์ค์ผ ํ๋ค.
์, ํ, ์ข, ์ฐ ๋ก ๋ชฐ์์ฃผ๋ ๋งค์ด ์์ ์ ๊ตฌํํ๋ค.
5๋ฒ ์ํํ๊ณ ๋๋ฉด ๋งค๋ฒ ๋ฐฐ์ด ์ค ๊ฐ์ฅ ํฐ ๊ฐ์ ์ต๋๊ฐ ์ ๋ฐ์ดํธ ํด์ค๋ค.
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
# ์ฐ, ์ข, ์, ํ
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
''''''
def dfs(time, a, dir):
global max_ans, n
if time >= 5:
for i in range(n):
max_ans = max(max_ans, max(a[i]))
return
na = [[0 for _ in range(n)] for _ in range(n)]
# dir ์ ๋ฐ๋ผ ์ด๋์ํํ์ฌ a ๊ธฐ์ค์ผ๋ก na ๋ฅผ ํ ๋นํ์
'''
a ๋ฅผ ๋ณด๊ณ , dir ์ ๋ฐ๋ผ์ ์คํ์ ์์๊ฐ์
dir ์ด ์, ํ ์ธ ๊ฒ๊ณผ ์ข, ์ฐ ์ธ๊ฑธ ๋๊ฐ๋ก ๊ตฌ๋ถํด์ ์ฒ๋ฆฌํ์
'''
if dir == 0:
for i in range(len(a)):
stack = []
rst = []
for j in range(len(a)):
if a[i][n - 1 - j]:
stack.append(a[i][n - 1 - j])
# ๋๋ฒ์งธ ๋ถํฐ๋
if len(stack) >= 2:
# ์๋ก๋ค์ด์จ๊ฒ ๊ฐ์ผ๋ฉด
if stack[-1] == stack[-2]:
# ๊ฐ์๊ฑฐ 2๋ฐฐํด์ค๊ฑฐ ๋ฐ๋ก ์ ์ฅํด์ฃผ๊ณ
together = stack[-1] * 2
# ์คํ์์ ๋ค์ ๋๊ฐ ๋นผ์ฃผ๊ณ
stack.pop()
stack.pop()
# 2๋ฐฐํด์ค๊ฑฐ ์คํ์ ๋ํด์ฃผ๊ณ
stack.append(together)
# ๋ชฝ๋
rst ์ ์ถ๊ฐ
rst.extend(stack[:])
stack = []
# ๋ฐฐ์นํ rst ๋ฅผ ์๋ฒฝํ ๋ง๋ค์๋น
rst.extend(stack[:])
loc = 0
for v in rst:
na[i][n - loc - 1] = v
loc += 1
elif dir == 1:
for i in range(len(a)):
stack = []
rst = []
for j in range(len(a)):
if a[i][j]:
stack.append(a[i][j])
# ๋๋ฒ์งธ ๋ถํฐ๋
if len(stack) >= 2:
# ์๋ก๋ค์ด์จ๊ฒ ๊ฐ์ผ๋ฉด
if stack[-1] == stack[-2]:
# ๊ฐ์๊ฑฐ 2๋ฐฐํด์ค๊ฑฐ ๋ฐ๋ก ์ ์ฅํด์ฃผ๊ณ
together = stack[-1] * 2
# ์คํ์์ ๋ค์ ๋๊ฐ ๋นผ์ฃผ๊ณ
stack.pop()
stack.pop()
# 2๋ฐฐํด์ค๊ฑฐ ์คํ์ ๋ํด์ฃผ๊ณ
stack.append(together)
# ๋ชฝ๋
rst ์ ์ถ๊ฐ
rst.extend(stack[:])
stack = []
# ๋ฐฐ์นํ rst ๋ฅผ ์๋ฒฝํ ๋ง๋ค์๋น
rst.extend(stack[:])
loc = 0
for v in rst:
na[i][loc] = v
loc += 1
elif dir == 2:
for i in range(len(a)):
stack = []
rst = []
for j in range(len(a)):
if a[j][i]:
stack.append(a[j][i])
# ๋๋ฒ์งธ ๋ถํฐ๋
if len(stack) >= 2:
# ์๋ก๋ค์ด์จ๊ฒ ๊ฐ์ผ๋ฉด
if stack[-1] == stack[-2]:
# ๊ฐ์๊ฑฐ 2๋ฐฐํด์ค๊ฑฐ ๋ฐ๋ก ์ ์ฅํด์ฃผ๊ณ
together = stack[-1] * 2
# ์คํ์์ ๋ค์ ๋๊ฐ ๋นผ์ฃผ๊ณ
stack.pop()
stack.pop()
# 2๋ฐฐํด์ค๊ฑฐ ์คํ์ ๋ํด์ฃผ๊ณ
stack.append(together)
# ๋ชฝ๋
rst ์ ์ถ๊ฐ
rst.extend(stack[:])
stack = []
# ๋ฐฐ์นํ rst ๋ฅผ ์๋ฒฝํ ๋ง๋ค์๋น
rst.extend(stack[:])
loc = 0
for v in rst:
na[loc][i] = v
loc += 1
elif dir == 3:
for i in range(len(a)):
stack = []
rst = []
for j in range(len(a)):
if a[n - j - 1][i]:
stack.append(a[n - j - 1][i])
# ๋๋ฒ์งธ ๋ถํฐ๋
if len(stack) >= 2:
# ์๋ก๋ค์ด์จ๊ฒ ๊ฐ์ผ๋ฉด
if stack[-1] == stack[-2]:
# ๊ฐ์๊ฑฐ 2๋ฐฐํด์ค๊ฑฐ ๋ฐ๋ก ์ ์ฅํด์ฃผ๊ณ
together = stack[-1] * 2
# ์คํ์์ ๋ค์ ๋๊ฐ ๋นผ์ฃผ๊ณ
stack.pop()
stack.pop()
# 2๋ฐฐํด์ค๊ฑฐ ์คํ์ ๋ํด์ฃผ๊ณ
stack.append(together)
# ๋ชฝ๋
rst ์ ์ถ๊ฐ
rst.extend(stack[:])
stack = []
# ๋ฐฐ์นํ rst ๋ฅผ ์๋ฒฝํ ๋ง๋ค์๋น
rst.extend(stack[:])
loc = 0
for v in rst:
na[n - loc - 1][i] = v
loc += 1
time += 1
for i in range(4):
dfs(time, na, i)
''''''
time = 0
max_ans = 2
for i in range(4):
dfs(time, a, i)
print(max_ans)
Last updated