π¦₯ μ½ν
/BAEKJOON
[BAEKJOON python] 12100_2048(easy)
μ§μ§μνμΉ΄
2022. 10. 10. 16:54
728x90
λ°μν
ν λ²μ μ΄λμ 보λ μμ μλ μ 체 λΈλ‘μ μνμ’μ° λ€ λ°©ν₯ μ€ νλλ‘ μ΄λμν€λ κ²
κ°μ κ°μ κ°λ λ λΈλ‘μ΄ μΆ©λνλ©΄ λ λΈλ‘μ νλλ‘ ν©μ³μ§κ² λλ€.
ν λ²μ μ΄λμμ μ΄λ―Έ ν©μ³μ§ λΈλ‘μ λ λ€λ₯Έ λΈλ‘κ³Ό λ€μ ν©μ³μ§ μ μλ€.
(μ€μ κ²μμμλ μ΄λμ ν λ² ν λλ§λ€ λΈλ‘μ΄ μΆκ°λμ§λ§, μ΄ λ¬Έμ μμ λΈλ‘μ΄ μΆκ°λλ κ²½μ°λ μλ€)
λκ°μ μκ° μΈ κ°κ° μλ κ²½μ°μλ μ΄λνλ €κ³ νλ μͺ½μ μΉΈμ΄ λ¨Όμ ν©μ³μ§λ€.
μ΄ λ¬Έμ μμ λ€λ£¨λ 2048 κ²μμ 보λμ ν¬κΈ°κ° N×N μ΄λ€.
보λμ ν¬κΈ°μ 보λνμ λΈλ‘ μνκ° μ£Όμ΄μ‘μ λ,
μ΅λ 5λ² μ΄λν΄μ λ§λ€ μ μλ κ°μ₯ ν° λΈλ‘μ κ°μ ꡬνλ νλ‘κ·Έλ¨μ μμ±
첫째 μ€μ 보λμ ν¬κΈ° N (1 ≤ N ≤ 20)μ΄ μ£Όμ΄μ§λ€.
λμ§Έ μ€λΆν° Nκ°μ μ€μλ κ²μνμ μ΄κΈ° μνκ° μ£Όμ΄μ§λ€.
0μ λΉ μΉΈμ λνλ΄λ©°, μ΄μΈμ κ°μ λͺ¨λ λΈλ‘μ λνλΈλ€.
λΈλ‘μ μ°μ¬ μλ μλ 2λ³΄λ€ ν¬κ±°λ κ°κ³ , 1024λ³΄λ€ μκ±°λ κ°μ 2μ μ κ³±κΌ΄μ΄λ€.
λΈλ‘μ μ μ΄λ νλ μ£Όμ΄μ§λ€.
μ΅λ 5λ² μ΄λμμΌμ μ»μ μ μλ κ°μ₯ ν° λΈλ‘μ μΆλ ₯
from copy import deepcopy
N = int(input())
graph = []
for i in range(N) :
graph.append(list(map(int, input().split())))
result = 0
def left(graph) :
for i in range(N) :
# μ΄ κ°μ₯ μΌμͺ½
p = 0
x = 0
for j in range(N) :
# λΉμΉΈ
if graph[i][j] == 0 :
continue
# λν κ² μλ€λ©΄
if x == 0 :
x = graph[i][j]
# λν κ² μλλ°, μ«μκ° κ°μκ°?
else :
if x == graph[i][j] :
graph[i][p] = x * 2
x = 0
p += 1
else :
graph[i][p] = x
x = graph[i][j]
p += 1
# λΉμμ£ΌκΈ°
graph[i][j] = 0
# μ’μΈ‘μμ μ°μΈ‘ λκΉμ§ λλ¬!
if x != 0 :
graph[i][p] = x
return graph
def right(graph) :
for i in range(N) :
# κ°μ₯ μ°μΈ‘ μ΄
p = N - 1
x = 0
for j in range(N - 1, -1 , -1) :
# λΉμΉΈ
if graph[i][j] == 0 :
continue
# λν κ² μλ€λ©΄
if x == 0 :
x = graph[i][j]
# λν κ² μλλ°, μ«μκ° κ°μκ°?
else :
if x == graph[i][j] :
graph[i][p] = x * 2
x = 0
p -= 1
else :
graph[i][p] = x
x = graph[i][j]
p -= 1
# λΉμμ£ΌκΈ°
graph[i][j] = 0
# μ°μΈ‘μμ μ’μΈ‘ λκΉμ§ λλ¬!
if x != 0 :
graph[i][p] = x
return graph
def up(graph) :
for i in range(N) :
# κ°μ₯ μ ν
p = 0
x = 0
for j in range(N) :
# λΉμΉΈ
if graph[j][i] == 0 :
continue
# λν κ² μλ€λ©΄
if x == 0 :
x = graph[j][i]
# λν κ² μλλ°, μ«μκ° κ°μκ°?
else :
if x == graph[j][i] :
graph[p][i] = x * 2
x = 0
p += 1
else :
graph[p][i] = x
x = graph[j][i]
p += 1
# λΉμμ£ΌκΈ°
graph[j][i] = 0
# μμΈ‘μμ μλμΈ‘ λκΉμ§ λλ¬!
if x != 0 :
graph[p][i] = x
return graph
def down(graph) :
for i in range(N) :
# κ°μ₯ μλ ν
p = N - 1
x = 0
for j in range(N - 1, -1 , -1) :
# λΉμΉΈ
if graph[j][i] == 0 :
continue
# λν κ² μλ€λ©΄
if x == 0 :
x = graph[j][i]
# λν κ² μλλ°, μ«μκ° κ°μκ°?
else :
if x == graph[j][i] :
graph[p][i] = x * 2
x = 0
p -= 1
else :
graph[p][i] = x
x = graph[j][i]
p -= 1
# λΉμμ£ΌκΈ°
graph[j][i] = 0
# μλμΈ‘μμ μμΈ‘ λκΉμ§ λλ¬!
if x != 0 :
graph[p][i] = x
return graph
def solution(graph, cnt) :
global result
if cnt == 5 :
for i in range(N) :
for j in range(N) :
result = max(result, graph[i][j])
return
solution(left(deepcopy(graph)), cnt + 1)
solution(right(deepcopy(graph)), cnt + 1)
solution(up(deepcopy(graph)), cnt + 1)
solution(down(deepcopy(graph)), cnt + 1)
solution(graph, 0)
print(result)
# input
# 3
# 2 2 2
# 4 4 4
# 8 8 8
# output
# 16
728x90
λ°μν