https://www.acmicpc.net/problem/14500
์์ด๋์ด
1. ๋ ๊ฐ์ง๋ก ๋๋ ์๊ฐํ๋ฉด
- ํ ํธ๋ก๋ฏธ๋ ธ ๋ํ ๋ง๋ค๊ธฐ
- ์ข ์ด ์์ ์ฌ๋ฆฌ๊ธฐ
2. ๋ํ ๋ง๋ค๊ธฐ
- ๋ํ์ด ํ์ ์ด๋ ๋์นญ๋ ๊ฐ๋ฅํ๋ dfs๋ฅผ ์ฌ์ฉํ๋ฉด T๋ํ ๋นผ๊ณ ์ ๋ถ ๋ค ๋ง๋ค ์ ์๋ค.
- dfs ํ์ ๊น์ด๋ฅผ 4๊น์ง ์ค์ ํ๋ฉด ๋ํ์ ํ์ธํ ์ ์๋ค.
- T ๋ชจ์์ ๋ฐ๋ก ํจ์๋ฅผ ๋ง๋ค์ด ์ฒ๋ฆฌํ๋ค.
3. ์ข ์ด์ ๋ํ ๋๊ธฐ
- ์๊ฐ์ ํ์ด๋ N, M ํฌ๊ธฐ๋ฅผ ๋ณด๊ณ ๋ธ๋ฃจํธ ํฌ์ค๋ฅผ ์ฌ์ฉํ๊ธฐ๋ก ๊ฒฐ์ ํ๋ค.
- ๊ฐ ์์น๋ง๋ค ๋ง๋ค์ด์ง๋ ๋ชจ๋ ๋ํ์ ์ต๋๊ฐ๋ค์ ๋ชจ์ ์ ์ฒด ๋น๊ต๋ฅผ ํด์ค๋ค.
์ฝ๋ ์ค๋ช
def dfs(x,y,n,c):
global dfs_max
if c == 4:
if dfs_max < n:
dfs_max = n
else:
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < M:
if visited[nx][ny] == 0:
visited[nx][ny] = 1
dfs(nx,ny,n + paper[nx][ny],c + 1)
visited[nx][ny] = 0
๋ฑํ ํน์ด์ฌํญ์ด ์๋ dfs์ฝ๋
def t_spin(x,y):
t_max = 0
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < M: # +1์นธ
nnx = nx + dx[i]
nny = ny + dy[i]
if 0 <= nnx < N and 0 <= nny < M: # +2์นธ
for h in [-1,1]: #๋ณผ๋กํ ๋ถ๋ถ
value = paper[x][y]
if i == 0 or i == 1: #์ธ๋ก๋ก ๊ธธ๋ฉด
sny = ny + h
if 0 <= sny < M:
value += paper[nx][ny]
value += paper[nnx][nny]
value += paper[nx][sny]
elif i == 2 or i == 3: #๊ฐ๋ก๋ก ๊ธธ๋ฉด
snx = nx + h
if 0 <= snx < N:
value += paper[nx][ny]
value += paper[nnx][nny]
value += paper[snx][ny]
if t_max < value:
t_max = value
return t_max
์ข ๋ฌด์ํ ๋ฐฉ๋ฒ์ด์ง๋ง for๋ฌธ์ ์ฌ์ฉํด์ ๊ฐ๋ฅํ ๋ชจ๋ T๋ชจ์์ ์ฒดํฌํ๋ค.
์ ์ฒด ์ฝ๋
import sys
N, M = map(int,input().split())
input = sys.stdin.readline
dx = [-1, 1, 0, 0] # ๋ถ, ๋จ, ์, ๋
dy = [0, 0, -1, 1]
paper = []
ans = 0
# @@@@ @@
# @@
# @ @
# @ @@ @@@
# @@ @ @
for i in range(N):
paper.append([int(x) for x in input().split()])
visited = [[0 for _ in range(M)] for _ in range(N)]
def dfs(x,y,n,c):
global dfs_max
if c == 4:
if dfs_max < n:
dfs_max = n
else:
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < M:
if visited[nx][ny] == 0:
visited[nx][ny] = 1
dfs(nx,ny,n + paper[nx][ny],c + 1)
visited[nx][ny] = 0
def t_spin(x,y):
t_max = 0
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < M: # +1์นธ
nnx = nx + dx[i]
nny = ny + dy[i]
if 0 <= nnx < N and 0 <= nny < M: # +2์นธ
for h in [-1,1]: #๋ณผ๋กํ ๋ถ๋ถ
value = paper[x][y]
if i == 0 or i == 1: #์ธ๋ก๋ก ๊ธธ๋ฉด
sny = ny + h
if 0 <= sny < M:
value += paper[nx][ny]
value += paper[nnx][nny]
value += paper[nx][sny]
elif i == 2 or i == 3: #๊ฐ๋ก๋ก ๊ธธ๋ฉด
snx = nx + h
if 0 <= snx < N:
value += paper[nx][ny]
value += paper[nnx][nny]
value += paper[snx][ny]
if t_max < value:
t_max = value
return t_max
for i in range(N):
for j in range(M):
dfs_max = 0
visited[i][j] = 1
dfs(i,j,paper[i][j],1)
visited[i][j] = 0
block_max = max(dfs_max, t_spin(i, j))
ans = max(ans,block_max)
print(ans)
'๐งฉ Problem Solving > [๋ฐฑ์ค]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 1074 Z (python ํ์ด์ฌ) (0) | 2022.06.26 |
---|---|
[๋ฐฑ์ค] 9019 DSLR (python ํ์ด์ฌ) (0) | 2022.06.26 |
[๋ฐฑ์ค] 18111 ๋ง์ธํฌ๋ํํธ (python ํ์ด์ฌ) (4) | 2022.06.23 |
[๋ฐฑ์ค] 1107 ๋ฆฌ๋ชจ์ฝ (python ํ์ด์ฌ) (0) | 2022.06.22 |
[๋ฐฑ์ค] 16234 ์ธ๊ตฌ์ด๋ (python ํ์ด์ฌ) (0) | 2022.06.21 |