[๋ฐฑ์ค€ ๐Ÿฅ‡4] 14500๋ฒˆ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ (Python/ํŒŒ์ด์ฌ)

mingssoยท2023๋…„ 3์›” 19์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
23/35

1๏ธโƒฃ ๋ฌธ์ œ

https://www.acmicpc.net/problem/14500



2๏ธโƒฃ ์ฝ”๋“œ

ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ์—„์ฒญ ๋ฌด์‹ํ•˜๊ฒŒ ํ‘ผ ์ฝ”๋“œ..ใ…‹ใ…‹ใ…‹ใ…‹
ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ์ขŒํ‘œ ์ผ์ผ์ด ์ ์–ด์„œ ํ’€์—ˆ๋‹ค

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)



3๏ธโƒฃ ํ’€์ด

์ฝ”๋“œ ์† 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)

์‹œ๊ฐ„ ์—„์ฒญ ์ค„์—ˆ๋‹ค....!

profile
๐Ÿฅ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ’ฐ

0๊ฐœ์˜ ๋Œ“๊ธ€