[BOJ][๐ŸŸก5][๋ฐฑ์ค€#14500] ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #14500


๋ฌธ์ œ

ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋ž€ ํฌ๊ธฐ๊ฐ€ 1ร—1์ธ ์ •์‚ฌ๊ฐํ˜•์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ด์–ด์„œ ๋ถ™์ธ ๋„ํ˜•์ด๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค.

์ •์‚ฌ๊ฐํ˜•์€ ์„œ๋กœ ๊ฒน์น˜๋ฉด ์•ˆ ๋œ๋‹ค. ๋„ํ˜•์€ ๋ชจ๋‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ •์‚ฌ๊ฐํ˜•์˜ ๋ณ€๋ผ๋ฆฌ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ๊ผญ์ง“์ ๊ณผ ๊ผญ์ง“์ ๋งŒ ๋งž๋‹ฟ์•„ ์žˆ์œผ๋ฉด ์•ˆ ๋œ๋‹ค.

์ •์‚ฌ๊ฐํ˜• 4๊ฐœ๋ฅผ ์ด์–ด ๋ถ™์ธ ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋Š” ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๋ผ๊ณ  ํ•˜๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ 5๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

์•„๋ฆ„์ด๋Š” ํฌ๊ธฐ๊ฐ€ Nร—M์ธ ์ข…์ด ์œ„์— ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ํ•˜๋‚˜๋ฅผ ๋†“์œผ๋ ค๊ณ  ํ•œ๋‹ค. ์ข…์ด๋Š” 1ร—1 ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ๊ฐ์˜ ์นธ์—๋Š” ์ •์ˆ˜๊ฐ€ ํ•˜๋‚˜ ์“ฐ์—ฌ ์žˆ๋‹ค. ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ํ•˜๋‚˜๋ฅผ ์ ์ ˆํžˆ ๋†“์•„์„œ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๊ฐ€ ๋†“์ธ ์นธ์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๋“ค์˜ ํ•ฉ์„ ์ตœ๋Œ€๋กœ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๋Š” ๋ฐ˜๋“œ์‹œ ํ•œ ์ •์‚ฌ๊ฐํ˜•์ด ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ์นธ์„ ํฌํ•จํ•˜๋„๋ก ๋†“์•„์•ผ ํ•˜๋ฉฐ, ํšŒ์ „์ด๋‚˜ ๋Œ€์นญ์„ ์‹œ์ผœ๋„ ๋œ๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ข…์ด์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (4ย โ‰ค N, M โ‰ค 500) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ข…์ด์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ์ˆ˜๋Š” ์œ„์—์„œ๋ถ€ํ„ฐ i๋ฒˆ์งธ ์นธ, ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ j๋ฒˆ์งธ ์นธ์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜์ด๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์ˆ˜๋Š” 1,000์„ ๋„˜์ง€ ์•Š๋Š” ์ž์—ฐ์ˆ˜์ด๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๊ฐ€ ๋†“์ธ ์นธ์— ์“ฐ์ธ ์ˆ˜๋“ค์˜ ํ•ฉ์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

5 5
1 2 3 4 5
5 4 3 2 1
2 3 4 5 6
6 5 4 3 2
1 2 1 2 1


์ถœ๋ ฅ

19


์˜ˆ์ œ 2

์ž…๋ ฅ

4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5


์ถœ๋ ฅ

20


์˜ˆ์ œ 3

์ž…๋ ฅ

4 10
1 2 1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1 2 1
1 2 1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1 2 1


์ถœ๋ ฅ

7


My Sol

import sys
input = sys.stdin.readline

def main1(i, ni, nj, ssum):
    global visited
    if i==4:
        global maxV
        maxV = max(maxV, ssum)
        return

    for di, dj in didj:
        si, sj = ni+di, nj+dj
        if not (0<=si<I and 0<=sj<J): continue
        if visited[si][sj]: continue
        visited[si][sj] = 1
        main1(i+1, si, sj, ssum+mat[si][sj])
        visited[si][sj] = 0

def main2(ni, nj):
    global maxV

    arr = []
    for di, dj in didj:
        si, sj = ni+di, nj+dj
        if not (0<=si<I and 0<=sj<J): continue
        arr.append(mat[si][sj])

    l = len(arr)
    val = mat[ni][nj]
    if l==4:
        val += sum(arr) - min(arr)
        maxV = max(maxV, val)
    elif l==3:
        val += sum(arr)
        maxV = max(maxV, val)
    return

I, J = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(I)]
visited = [[0]*J for _ in range(I)]
didj = ((-1,0),(1,0),(0,-1),(0,1))

maxV = 0
for i in range(I):
    for j in range(J):
        visited[i][j] = 1
        main1(1, i, j, mat[i][j])
        visited[i][j] = 0
        main2(i, j)

print(maxV)

๋ชจ๋“  ์นธ์— ๋Œ€ํ•˜์—ฌ ์™„์ „ํƒ์ƒ‰์„ ์‹ค์‹œํ•˜์˜€๋Š”๋ฐ, ๋ชจ๋“  ์นธ์„ ๊ธฐ์ค€์ ์œผ๋กœ ์‚ผ์•„ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค. ใ…—์ž ๋ชจ์–‘ ๋ธ”๋Ÿญ์„ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋‘ dfs๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ใ…—์ž ๋ชจ์–‘์„ ํ™•์ธํ•˜๋Š” main2() ํ•จ์ˆ˜๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€๊ณ , ๋‚˜๋จธ์ง€๋Š” main1() ํ•จ์ˆ˜๋กœ ์ž‘์„ฑํ•˜์˜€๋‹ค.

๊นŠ์ด๋ฅผ 4๊นŒ์ง€ ํ•˜์—ฌ ์ด 4์นธ์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•ด ์ธ์ž๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€์œผ๋ฉฐ, 4์นธ์˜ ํ•ฉ์„ ๊ตฌํ–ˆ๋‹ค๋ฉด ์ „์—ญ๋ณ€์ˆ˜์ธ maxV์™€ ๋น„๊ตํ•ด ๋” ํฐ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋‹ค.

ใ…—์ž ๋ชจ์–‘์˜ ๊ฒฝ์šฐ ๋ฒ”์œ„ ๋‚ด์˜ ์‚ฌ๋ฐฉ์˜ ๊ฐ’์„ arr์— ๋‹ด๋Š”๋ฐ, ๊ผญ์ง“์ ์€ ๋ฒ”์œ„ ๋‚ด์˜ ์‚ฌ๋ฐฉ์˜ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ์ด๋ฏ€๋กœ ใ…—์ž๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†์–ด ์ œ์™ธํ•˜์˜€๊ณ , ๋ณ€์— ์žˆ์–ด์„œ ์‚ฌ๋ฐฉ ์ค‘ 3๊ฐœ๋งŒ ์ทจํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ธฐ์ค€์ ์˜ ๊ฐ’๊ณผ ๋”ํ•ด maxV์— ๋น„๊ตํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ๋Š” ์‚ฌ๋ฐฉ์— ๊ฐ’์ด ๋ชจ๋‘ ์žˆ๋Š” ๊ฒฝ์šฐ์ผํ…๋ฐ, ์ด ์‚ฌ๋ฐฉ์˜ ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ 3๊ฐœ๋ฅผ ๋”ํ•ด ๊ธฐ์ค€์ ์˜ ๊ฐ’๊ณผ ๋”ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

n, m = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(n)]

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def dfs(x, y, depth, result):
    global MAX
    if depth == 4:
        MAX = max(MAX, result)
        return

    if result + (4 - depth) * mNumber <= MAX:
        return

    for d in range(4):
        nx = x + dx[d]
        ny = y + dy[d]

        if 0 <= nx < n and 0 <= ny < m and not visit[nx][ny]:
            visit[nx][ny] = True
            dfs(nx, ny, depth + 1, result + graph[nx][ny])
            visit[nx][ny] = False

            if depth == 2:
                visit[nx][ny] = True
                dfs(x, y, depth + 1, result + graph[nx][ny])
                visit[nx][ny] = False

mNumber = -1
for i in range(n):
    mNumber = max(mNumber, max(graph[i]))

MAX = -1
visit = [[False] * m for _ in range(n)]
for x in range(n):
    for y in range(m):
        visit[x][y] = True
        dfs(x, y, 1, graph[x][y])
        visit[x][y] = False

print(MAX)

์—ฐ์‚ฐ์‹œ๊ฐ„์ด ๋ช‡ ๋ฐฐ๋Š” ๋” ๋น ๋ฅธ ์ฝ”๋“œ๋ฅผ ์ฑ„์ ํ˜„ํ™ฉ์—์„œ ๋ฐœ๊ฒฌํ•ด์„œ ๋ถ„์„ํ•˜์˜€๋‹ค. ์ „์ฒด์—์„œ ์ตœ๋Œ“๊ฐ’์„ mNumber๋กœ ์ง€์ •ํ•˜์˜€๋Š”๋ฐ, depth๋งˆ๋‹ค depth์— mNumber๋ฅผ ๊ณฑํ•œ ๊ฐ’๊ณผ result๋ฅผ ๋”ํ•œ ๊ฐ’์ด MAX๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด returnํ•˜์˜€๋‹ค. mNumber๋ฅผ depth๋งŒํผ ๊ณฑํ•œ ๊ฐ’์€ ๊ฐ€์žฅ ์ปค์•ผํ•˜๋Š”๋ฐ result๊ฐ€ ์ถฉ๋ถ„ํžˆ ํฌ์ง€ ์•Š์•„ MAX๋ณด๋‹ค ์ž‘์•„์ง„๋‹ค๋ฉด ์•„๋ฌด๋ฆฌ ๋” depth๋ฅผ ์ง„ํ–‰ํ•ด๋„ MAX๋ณด๋‹ค ์ปค์งˆ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋” ์ง„ํ–‰ํ•  ์˜๋ฏธ๊ฐ€ ์—†๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ€์ง€์น˜๊ธฐ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ