Skip to content

Commit

Permalink
2024-07-22
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Jul 22, 2024
1 parent b0ec1d1 commit 4002c63
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tgyuuAn/DP/๋กœ๋ด‡ ์กฐ์ข…ํ•˜๊ธฐ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys

def input(): return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
board = []

for _ in range(N):
row = list(map(int, input().split()))
board.append(row)

DP = [[-float('inf')] * M for _ in range(N)]
DP[0][0] = board[0][0]

# ์ฒซ ๋ฒˆ์งธ ํ–‰ ์ดˆ๊ธฐํ™”
for j in range(1, M):
DP[0][j] = DP[0][j-1] + board[0][j]

# ๊ฐ ํ–‰ ์ฒ˜๋ฆฌ
for i in range(1, N):
left_to_right = [-float('inf')] * M
right_to_left = [-float('inf')] * M

# ํ˜„์žฌ ํ–‰์—์„œ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํƒ์ƒ‰
left_to_right[0] = DP[i-1][0] + board[i][0]
for j in range(1, M):
left_to_right[j] = max(DP[i-1][j], left_to_right[j-1]) + board[i][j]

# ํ˜„์žฌ ํ–‰์—์„œ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ํƒ์ƒ‰
right_to_left[M-1] = DP[i-1][M-1] + board[i][M-1]
for j in range(M-2, -1, -1):
right_to_left[j] = max(DP[i-1][j], right_to_left[j+1]) + board[i][j]

# ๊ฒฐ๊ณผ ๋ณ‘ํ•ฉ
for j in range(M):
DP[i][j] = max(left_to_right[j], right_to_left[j])

print(DP[N-1][M-1])
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@
| 61์ฐจ์‹œ | 2024.06.20 | ํฌ๋ฃจ์Šค์นผ | <a href="https://www.acmicpc.net/problem/1774">์šฐ์ฃผ์‹ ๊ณผ์˜ ๊ต๊ฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/212
| 62์ฐจ์‹œ | 2024.07.01 | DP | <a href="https://www.acmicpc.net/problem/1949">์šฐ์ˆ˜ ๋งˆ์„</a> | https://github.com/AlgoLeadMe/AlgoLeadMse-1/pull/214
| 63์ฐจ์‹œ | 2024.07.08 | BFS | <a href="https://www.acmicpc.net/problem/3108">๋กœ๊ณ </a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/216
| 66์ฐจ์‹œ | 2024.07.22 | DP | <a href="https://www.acmicpc.net/problem/2169">๋กœ๋ด‡ ์กฐ์ข…ํ•˜๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/222
---

0 comments on commit 4002c63

Please sign in to comment.