class Solution:
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
R = len(grid)
C = len(grid[0])
dp = [[0] * C for _ in range(R)]
# 初始条件
_sum = 0
for i in range(R):
_sum += grid[i][0]
dp[i][0] = _sum
_sum = 0
for j in range(C):
_sum += grid[0][j]
dp[0][j] = _sum
# 状态转移方程
for i in range(1, R):
for j in range(1, C):
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
return dp[-1][-1]