https://www.acmicpc.net/problem/14500
ํ
ํธ๋ก๋ฏธ๋
ธ ์์ฒญ ๋ฌด์ํ๊ฒ ํผ ์ฝ๋..ใ
ใ
ใ
ใ
ํ
ํธ๋ก๋ฏธ๋
ธ ์ขํ ์ผ์ผ์ด ์ ์ด์ ํ์๋ค
n, m = map(int, input().split())
paper = []
for _ in range(n):
paper.append(list(map(int, input().split())))
dy = [[0, 0, 0, 0], [0, 1, 2, 3], [0, 1, 2, 2], [0, 1, 2, 2], [0, 0, 1, 2],
[0, 0, 1, 2], [0, 1, 1, 1], [0, 1, 1, 1], [0, 0, 0, 1], [0, 0, 0, 1],
[0, 0, 1, 1], [0, 1, 1, 2], [0, 1, 1, 2], [0, 0, 1, 1], [0, 0, 1, 1],
[0, 1, 1, 1], [0, 0, 0, 1], [0, 1, 1, 2], [0, 1, 1, 2]]
dx = [[0, 1, 2, 3], [0, 0, 0, 0], [0, 0, 0, 1], [1, 1, 0, 1], [0, 1, 0, 0],
[0, 1, 1, 1], [0, 0, 1, 2], [2, 0, 1, 2], [0, 1, 2, 0], [0, 1, 2, 2],
[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 1, 0], [1, 2, 0, 1], [0, 1, 1, 2],
[1, 0, 1, 2], [0, 1, 2, 1], [0, 0, 1, 0], [1, 0, 1, 1]]
answer = 0
# 19๊ฐ์ ํด๋ฆฌ๋
ธ๋ฏธ๋
ธ ํ๋์ฉ ์๋ํด๋ณด๊ธฐ
for i in range(19):
for y in range(n):
for x in range(m):
flag = True # ํด๋น ํด๋ฆฌ๋
ธ๋ฏธ๋
ธ ๋์์์๋์ง ์ฌ๋ถ
temp = 0 # ํด๋น ํด๋ฆฌ๋
ธ๋ฏธ๋
ธ ์๋ค์ ํฉ
for k in range(len(dx[i])):
nx = x + dx[i][k]
ny = y + dy[i][k]
if 0 <= nx < m and 0 <= ny < n:
temp += paper[ny][nx]
else:
flag = False
break
if flag:
answer = max(temp, answer)
print(answer)
์ฝ๋ ์ dy, dx ๋ฆฌ์คํธ ์์๋ ์ฌ์ง ์ ํด๋ฆฌ๋ ธ๋ฏธ๋ ธ ๋ฒํธ์!
๊ทธ๋๋ DFS ํ์ด๋ ์๊ณ ๋์ด๊ฐ์ผ์ง!
ํน์ ์ขํ๋ก๋ถํฐ dfs๋ฅผ ๊ฑฐ๋ฆฌ 3๊น์ง๋ง ๋๊ฒ๋๋ฉด ใ
๋ชจ์์ ์ ์ธํ ๋ชจ๋ ํ
ํธ๋ก๋ฏธ๋
ธ๋ฅผ ๋ง๋ค ์ ์๋ค
์ฝ๋๋ ๋ค๋ฅธ ๋ถ๋ค ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ์
n, m = map(int, input().split())
board = []
for _ in range(n):
board.append(list(map(int, input().split())))
maxi = max(map(max, board)) # ์ข
์ด ์ซ์ ์ค ์ต๋๊ฐ
visited = [[False] * m for _ in range(n)]
answer = 0
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
def dfs(y, x, cnt, total): # y์ขํ, x์ขํ, ํ์ํ ๋ธ๋ก ๊ฐ์, ์๋ค์ ํฉ
global answer
if answer >= total + maxi * (4 - cnt): # ์ง๊ธ ์ํ์์ ๊ณ์ ์ต๋๊ฐ์ด ๋์๋ answer๋ณด๋ค ์์ ๊ฒฝ์ฐ
return
if cnt == 4:
answer = max(total, answer)
return
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
if 0 <= ny < n and 0 <= nx < m and not visited[ny][nx]:
if cnt == 2: # ใ
๋ชจ์์ ๋ง๋ค์ด์ฃผ๊ธฐ ์ํด, ๋๊ฐ์ ๋ธ๋ก์ ํ์ํ์ ๋
visited[ny][nx] = True
dfs(y, x, cnt+1, total+board[ny][nx])
visited[ny][nx] = False
visited[ny][nx] = True
dfs(ny, nx, cnt+1, total+board[ny][nx])
visited[ny][nx] = False
for y in range(n):
for x in range(m):
visited[y][x] = True
dfs(y, x, 1, board[y][x])
visited[y][x] = False
print(answer)
์๊ฐ ์์ฒญ ์ค์๋ค....!