๋ฌธ์
https://www.acmicpc.net/problem/14500
ํ์ด
# ๋ฐฑ์ค 14500๋ฒ ๋ฌธ์ - ํ
ํธ๋ก๋ฏธ๋
ธ
import sys
input = sys.stdin.readline
def dfs(x, y, idx, total):
global ans
if ans >= total + max_val * (3 - idx):
return
if idx == 3:
ans = max(ans, total)
return
else:
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m and visit[nx][ny] == 0:
if idx == 1:
visit[nx][ny] = 1
dfs(x, y, idx + 1, total + arr[nx][ny])
visit[nx][ny] = 0
visit[nx][ny] = 1
dfs(nx, ny, idx + 1, total + arr[nx][ny])
visit[nx][ny] = 0
n, m = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(n)]
visit = [([0] * m) for _ in range(n)]
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
ans = 0
max_val = max(map(max, arr))
for x in range(n):
for y in range(m):
visit[x][y] = 1
dfs(x, y, 0, arr[x][y])
visit[x][y] = 0
print(ans)
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold3] 1238๋ฒ_ํํฐ (1) | 2023.05.09 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 11054๋ฒ_๊ฐ์ฅ ๊ธด ๋ฐ์ดํ ๋ ๋ถ๋ถ ์์ด (1) | 2023.05.09 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver2] 1535๋ฒ_์๋ (2) | 2023.05.05 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold3] 2252๋ฒ_์ค ์ธ์ฐ๊ธฐ (1) | 2023.05.04 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 2096๋ฒ_๋ด๋ ค๊ฐ๊ธฐ (2) | 2023.05.03 |