https://www.acmicpc.net/problem/12100
์์ด๋์ด
1. ๊ตฌํ์ด ๋จผ์
- ๋ฌธ์ ๋ฅผ ๋ณด๊ณ N์ต๋๊ฐ 20์ด๊ณ ์ด๋๋ 5๋ฒ ํ๋ฏ๋ก ๋ฌธ์ ๊ฐ ๋งํ ๊ทธ๋๋ก ๊ตฌํํ๋ฉด ๋๊ฒ ๋ค๊ณ ์๊ฐ.
- ์, ํ, ์ข, ์ฐ ์ด๋ ์ค์ '์ข'๋ก ์ด๋ํ๋ ๊ฒ๋ถํฐ ๊ตฌํํ๋ฉด ๋๋จธ์ง๋ ์ฝ๊ฒ ๋ง๋ค ์ ์๋ค.
- ๋ง๋ค์๋ก ๋ณ์๋ if๋ฌธ์ด ๋ง์์ ธ ๋ค์ ๋ฐ๊ณ ์ฒ์๋ถํฐ ์์ฑํ๋ค. ๋๋ฌด ๋ง์ ๋ณ์๋ ํ์ ์๋ค. ์ด๊ฑด ๋ค๋ฅธ ๊ตฌํ ๋ฌธ์ ๋ ๋ง์ฐฌ๊ฐ์ง๋ผ๊ณ ์๊ฐ.
- '์ด๋์ํฌ ๋ธ๋ก'๊ณผ '๊ทธ ๋ธ๋ก์ด ์ด๋ํ์ ๋ ๋์ฐฉํ๋ ๊ณณ์ ์๋ ๋ธ๋ก'(๋ง์ด ์ข ์ด์ํ๊ธด ํ๋ฐ) ๋ ๊ฐ๋ฅผ ๋น๊ตํ๋ ๊ฒ์ ์ค์ฌ์ผ๋ก ์ฝ๋๋ฅผ ์งฐ๋ค.
2. ๋ฐฑํธ๋ํน๊ณผ ๋ธ๋ฃจํธ ํฌ์ค
- ์ํ์ข์ฐ ์ด๋ ๊ทธ๋ฆฌ๊ณ ์ต๋ 5๋ฒ ์ด๋์ด๋ฏ๋ก 4^5(1024)๊ฐ์ ๊ฒฝ์ฐ๋ง ๊ณ ๋ คํ๋ฉด ๋๋ค.
- ๋ธ๋ฃจํธ ํฌ์ค ๋ฐฉ๋ฒ์ผ๋ก ํ๋ํ๋ ์ ๋ถ๋ค ํด๋ณด๋ฉด ๋๋ค.
- ์ด์ฐจ์ ๋ฐฐ์ด์ ์ฌ์ฉํ๋๊น deepcopy๋ฅผ ์ฌ์ฉํด์ ์, ํ, ์ข, ์ฐ ์ผ์ด์ค๋ฅผ ๋๋์๋ค.
์ ์ฒด ์ฝ๋
import sys, copy
N = int(input())
pan = []
ans = 0
for _ in range(N):
pan.append([int(x) for x in sys.stdin.readline().rstrip().split()])
def left(board):
for i in range(N):
cursor = 0
for j in range(1, N):
if board[i][j] != 0: # 0์ด ์๋ ๊ฐ์ด
tmp = board[i][j]
board[i][j] = 0 # ์ผ๋จ ๋น์์ง๊บผ๋๊น 0์ผ๋ก ๋ฐ๊ฟ
if board[i][cursor] == 0: # ๋น์ด์์ผ๋ฉด
board[i][cursor] = tmp # ์ฎ๊ธด๋ค
elif board[i][cursor] == tmp: # ๊ฐ์ผ๋ฉด
board[i][cursor] *= 2 # ํฉ์น๋ค
cursor += 1
else: # ๋น์ด์์ง๋ ์๊ณ ๋ค๋ฅธ ๊ฐ์ผ๋
cursor += 1 # pass
board[i][cursor] = tmp # ๋ฐ๋ก ์์ ๋ถ์
return board
def right(board):
for i in range(N):
cursor = N - 1
for j in range(N - 1, -1, -1):
if board[i][j] != 0:
tmp = board[i][j]
board[i][j] = 0
if board[i][cursor] == 0:
board[i][cursor] = tmp
elif board[i][cursor] == tmp:
board[i][cursor] *= 2
cursor -= 1
else:
cursor -= 1
board[i][cursor] = tmp
return board
def up(board):
for j in range(N):
cursor = 0
for i in range(N):
if board[i][j] != 0:
tmp = board[i][j]
board[i][j] = 0
if board[cursor][j] == 0:
board[cursor][j] = tmp
elif board[cursor][j] == tmp:
board[cursor][j] *= 2
cursor += 1
else:
cursor += 1
board[cursor][j] = tmp
return board
def down(board):
for j in range(N):
cursor = N - 1
for i in range(N - 1, -1, -1):
if board[i][j] != 0:
tmp = board[i][j]
board[i][j] = 0
if board[cursor][j] == 0:
board[cursor][j] = tmp
elif board[cursor][j] == tmp:
board[cursor][j] *= 2
cursor -= 1
else:
cursor -= 1
board[cursor][j] = tmp
return board
def dfs(n, arr):
global ans
if n == 5:
for i in range(N):
for j in range(N):
if arr[i][j] > ans:
ans = arr[i][j]
return
for i in range(4):
copy_arr = copy.deepcopy(arr)
if i == 0:
dfs(n + 1, left(copy_arr))
elif i == 1:
dfs(n + 1, right(copy_arr))
elif i == 2:
dfs(n + 1, up(copy_arr))
else:
dfs(n + 1, down(copy_arr))
dfs(0, pan)
print(ans)
์ฝ๋ ์ค๋ช
def left(board):
for i in range(N):
cursor = 0
for j in range(1, N):
if board[i][j] != 0: # 0์ด ์๋ ๊ฐ์ด
tmp = board[i][j]
board[i][j] = 0 # ์ผ๋จ ๋น์์ง๊บผ๋๊น 0์ผ๋ก ๋ฐ๊ฟ
if board[i][cursor] == 0: # ๋น์ด์์ผ๋ฉด
board[i][cursor] = tmp # ์ฎ๊ธด๋ค
elif board[i][cursor] == tmp: # ๊ฐ์ผ๋ฉด
board[i][cursor] *= 2 # ํฉ์น๋ค
cursor += 1
else: # ๋น์ด์์ง๋ ์๊ณ ๋ค๋ฅธ ๊ฐ์ผ๋
cursor += 1 # pass
board[i][cursor] = tmp # ๋ฐ๋ก ์์ ๋ถ์
return board
๋จผ์ ๋ธ๋ก์ ์ด๋์ํค๋ ์ฝ๋.
๋๋จธ์ง ์, ํ, ์ฐ ๋ฐฉ๋ฒ๋ ๋น์ทํ๋ฏ๋ก ์ข๋ก ์์ง์ผ ๋ ์ฝ๋๋ง ์ค๋ช .
์ค๋ช ์ ์ฃผ์์ผ๋ก ํ๋๊ฒ ํธํ ๊ฑฐ ๊ฐ์์ ์ฃผ์์ผ๋ก ๊ฐ๋จํ๊ฒ ์์ฑํ๋ค.
์ฎ๊ธธ ๋ธ๋ก์ board[i][j], ์ฎ๊ธธ ๋ธ๋ก๊ณผ ๋ฐ๊ฟ ์๋ฆฌ๋ board[i][cursor]์ด๋ค.
def dfs(n, arr):
global ans
if n == 5:
for i in range(N):
for j in range(N):
if arr[i][j] > ans:
ans = arr[i][j]
return
for i in range(4):
copy_arr = copy.deepcopy(arr)
if i == 0:
dfs(n + 1, left(copy_arr))
elif i == 1:
dfs(n + 1, right(copy_arr))
elif i == 2:
dfs(n + 1, up(copy_arr))
else:
dfs(n + 1, down(copy_arr))
n์ด 5๊ฐ ๋๋ฉด(๋ธ๋ก์ 5๋ฒ ์ด๋ํ๋ฉด) ํ์ฌ ์๋ ์ด์ฐจ์ ๋ฐฐ์ด์์ ๊ฐ์ฅ ํฐ ๊ฐ์ ์ฐพ์ ์ ์ฅํ๊ณ return ํ๋ค.
๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ ์, ํ, ์ข, ์ฐ case ๋ฐ๋ก๋ฐ๋ก ํ์ธ์ ํด์ผ ํ๋ฏ๋ก ์ด์ฐจ์ ๋ฐฐ์ด์ deepcopy๋ก ๋ณต์ฌํด์ dfsํจ์์ ๋ฃ์ด์ค๋ค.
'๐งฉ Problem Solving > [๋ฐฑ์ค]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 1806 ๋ถ๋ถํฉ (python ํ์ด์ฌ) (0) | 2022.07.29 |
---|---|
[๋ฐฑ์ค] 1504 ํน์ ํ ์ต๋จ ๊ฒฝ๋ก (python ํ์ด์ฌ) (0) | 2022.07.28 |
[๋ฐฑ์ค] 15654 N๊ณผ M(5) (python ํ์ด์ฌ) (0) | 2022.07.13 |
[๋ฐฑ์ค] 1202 ๋ณด์ ๋๋ (python ํ์ด์ฌ) (0) | 2022.07.11 |
[๋ฐฑ์ค] 13913 ์จ๋ฐ๊ผญ์ง 4 (python ํ์ด์ฌ) (0) | 2022.07.10 |