From 4002c6366dec63bf0c84e603bc1a5528d386caad Mon Sep 17 00:00:00 2001 From: tgyuuAn Date: Mon, 22 Jul 2024 22:48:09 +0900 Subject: [PATCH] 2024-07-22 --- ...60\354\242\205\355\225\230\352\270\260.py" | 38 +++++++++++++++++++ tgyuuAn/README.md | 1 + 2 files changed, 39 insertions(+) create mode 100644 "tgyuuAn/DP/\353\241\234\353\264\207 \354\241\260\354\242\205\355\225\230\352\270\260.py" diff --git "a/tgyuuAn/DP/\353\241\234\353\264\207 \354\241\260\354\242\205\355\225\230\352\270\260.py" "b/tgyuuAn/DP/\353\241\234\353\264\207 \354\241\260\354\242\205\355\225\230\352\270\260.py" new file mode 100644 index 00000000..08564f6b --- /dev/null +++ "b/tgyuuAn/DP/\353\241\234\353\264\207 \354\241\260\354\242\205\355\225\230\352\270\260.py" @@ -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]) diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index f73a8180..2b0a38aa 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -68,4 +68,5 @@ | 61차시 | 2024.06.20 | 크루스칼 | 우주신과의 교감 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/212 | 62차시 | 2024.07.01 | DP | 우수 마을 | https://github.com/AlgoLeadMe/AlgoLeadMse-1/pull/214 | 63차시 | 2024.07.08 | BFS | 로고 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/216 +| 66차시 | 2024.07.22 | DP | 로봇 조종하기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/222 ---