목차

로봇 조종하기

ps
링크acmicpc.net/…
출처BOJ
문제 번호2169
문제명로봇 조종하기
레벨골드 1
분류

DP

시간복잡도O(nm)
인풋사이즈n<=1000, m<=1000
사용한 언어Python
제출기록52224KB / 984ms
최고기록984ms
해결날짜2021/11/19

풀이

코드

"""Solution code for "BOJ 2169. 로봇 조종하기".

- Problem link: https://www.acmicpc.net/problem/2169
- Solution link: http://www.teferi.net/ps/problems/boj/2169

Tags: [DP]
"""

import sys

INF = float('inf')


def main():
    N, M = [int(x) for x in sys.stdin.readline().split()]
    grid = [[int(x) for x in sys.stdin.readline().split()] for _ in range(N)]

    dp_cur = [-INF] * M
    dp_cur[0] = 0
    for row in grid:
        dp_prev = dp_cur
        dp_l, dp_r = [None] * M, [None] * M
        prev = -INF
        for i, row_i, dp_prev_i in zip(range(M), row, dp_prev):
            dp_r[i] = prev = max(prev, dp_prev_i) + row_i
        prev = -INF
        for i, row_i, dp_prev_i in zip(reversed(range(M)), reversed(row),
                                       reversed(dp_prev)):
            dp_l[i] = prev = max(prev, dp_prev_i) + row_i
        dp_cur = [max(vals) for vals in zip(dp_r, dp_l)]

    print(dp_cur[-1])


if __name__ == '__main__':
    main()