# 最小路径和

<p>给定一个包含非负整数的 <code><em>m</em> x <em>n</em></code> 网格 <code>grid</code> ，请找出一条从左上角到右下角的路径，使得路径上的数字总和为最小。</p><p><strong>说明：</strong>每次只能向下或者向右移动一步。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/0c6d9496445c66b255a406ba2f9a3eb9.jpeg" style="width: 242px; height: 242px;" /><pre><strong>输入：</strong>grid = [[1,3,1],[1,5,1],[4,2,1]]<strong><br />输出：</strong>7<strong><br />解释：</strong>因为路径 1→3→1→1→1 的总和最小。</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>grid = [[1,2,3],[4,5,6]]<strong><br />输出：</strong>12</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>m == grid.length</code></li>	<li><code>n == grid[i].length</code></li>	<li><code>1 <= m, n <= 200</code></li>	<li><code>0 <= grid[i][j] <= 100</code></li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline int min(int a, int b)
{
	return a < b ? a : b;
}
int minPathSum(int **grid, int gridRowSize, int gridColSize)
{
	int i, j;
	int **dp = malloc(gridRowSize * sizeof(int *));
	for (i = 0; i < gridRowSize; i++)
	{
		dp[i] = malloc(gridColSize * sizeof(int));
	}
	dp[0][0] = grid[0][0];
	int sum = dp[0][0];
	for (i = 1; i < gridRowSize; i++)
	{
		sum += grid[i][0];
		dp[i][0] = sum;
	}
	sum = dp[0][0];
	for (i = 1; i < gridColSize; i++)
	{
		sum += grid[0][i];
		dp[0][i] = sum;
	}
	for (i = 1; i < gridRowSize; i++)
	{
		for (j = 1; j < gridColSize; j++)
		{
			_____________________;
		}
	}
	return dp[gridRowSize - 1][gridColSize - 1];
}
int main(int argc, char **argv)
{
	int i, j;
	int row = argc - 1;
	int col = strlen(argv[1]);
	int **grid = malloc(row * sizeof(int *));
	for (i = 0; i < row; i++)
	{
		grid[i] = malloc(col * sizeof(int));
		for (j = 0; j < col; j++)
		{
			grid[i][j] = argv[i + 1][j] - '0';
			printf("%d ", grid[i][j]);
		}
		printf("\n");
	}
	printf("%d\n", minPathSum(grid, row, col));
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline int min(int a, int b)
{
	return a < b ? a : b;
}
int minPathSum(int **grid, int gridRowSize, int gridColSize)
{
	int i, j;
	int **dp = malloc(gridRowSize * sizeof(int *));
	for (i = 0; i < gridRowSize; i++)
	{
		dp[i] = malloc(gridColSize * sizeof(int));
	}
	dp[0][0] = grid[0][0];
	int sum = dp[0][0];
	for (i = 1; i < gridRowSize; i++)
	{
		sum += grid[i][0];
		dp[i][0] = sum;
	}
	sum = dp[0][0];
	for (i = 1; i < gridColSize; i++)
	{
		sum += grid[0][i];
		dp[0][i] = sum;
	}
	for (i = 1; i < gridRowSize; i++)
	{
		for (j = 1; j < gridColSize; j++)
		{
			dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);
		}
	}
	return dp[gridRowSize - 1][gridColSize - 1];
}
int main(int argc, char **argv)
{
	int i, j;
	int row = argc - 1;
	int col = strlen(argv[1]);
	int **grid = malloc(row * sizeof(int *));
	for (i = 0; i < row; i++)
	{
		grid[i] = malloc(col * sizeof(int));
		for (j = 0; j < col; j++)
		{
			grid[i][j] = argv[i + 1][j] - '0';
			printf("%d ", grid[i][j]);
		}
		printf("\n");
	}
	printf("%d\n", minPathSum(grid, row, col));
	return 0;
}
```

## 答案

```cpp
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);
```

## 选项

### A

```cpp
dp[i][j] = grid[i][j] + min(dp[i][j], dp[i - 1][j - 1]);
```

### B

```cpp
dp[i][j] = grid[i][j] + min(dp[i][j - 1], dp[i - 1][j - 1]);
```

### C

```cpp
dp[i][j] = grid[i][j] + min(dp[i - 1][j + 1], dp[i + 1][j - 1]);
```