# 不同路径

<p>一个机器人位于一个 <code>m x n</code><em> </em>网格的左上角 （起始点在下图中标记为 “Start” ）。</p>
<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角（在下图中标记为 “Finish” ）。</p>
<p>问总共有多少条不同的路径？</p>
<p> </p>
<p><strong>示例 1：</strong></p><img
    src="https://img-service.csdnimg.cn/img_convert/099169dc43ebe3d42afb948803f38eda.png" />
<pre><strong>输入：</strong>m = 3, n = 7<strong><br />输出：</strong>28</pre>
<p><strong>示例 2：</strong></p>
<pre><strong>输入：</strong>m = 3, n = 2<strong><br />输出：</strong>3<strong><br />解释：</strong>从左上角开始，总共有 3 条路径可以到达右下角。<br />1. 向右 -> 向下 -> 向下<br />2. 向下 -> 向下 -> 向右<br />3. 向下 -> 向右 -> 向下</pre>
<p><strong>示例 3：</strong></p>
<pre><strong>输入：</strong>m = 7, n = 3<strong><br />输出：</strong>28</pre>
<p><strong>示例 4：</strong></p>
<pre><strong>输入：</strong>m = 3, n = 3<strong><br />输出：</strong>6</pre>
<p> </p>
<p><strong>提示：</strong></p>
<ul>
    <li><code>1 <= m, n <= 100</code></li>
    <li>题目数据保证答案小于等于 <code>2 * 10<sup>9</sup></code></li>
</ul>

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

```cpp
#include <stdio.h>
#include <stdlib.h>
static int uniquePaths(int m, int n)
{
	int row, col;
	int *grids = malloc(m * n * sizeof(int));
	for (col = 0; col < m; col++)
	{
		grids[col] = 1;
	}
	for (row = 0; row < n; row++)
	{
		grids[row * m] = 1;
	}
	for (row = 1; row < n; row++)
	{
		for (col = 1; col < m; col++)
		{
			______________________________;
		}
	}
	return grids[m * n - 1];
}
int main(int argc, char **argv)
{
	if (argc != 3)
	{
		fprintf(stderr, "Usage: ./test m n\n");
		exit(-1);
	}
	printf("%d\n", uniquePaths(atoi(argv[1]), atoi(argv[2])));
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
static int uniquePaths(int m, int n)
{
	int row, col;
	int *grids = malloc(m * n * sizeof(int));
	for (col = 0; col < m; col++)
	{
		grids[col] = 1;
	}
	for (row = 0; row < n; row++)
	{
		grids[row * m] = 1;
	}
	for (row = 1; row < n; row++)
	{
		for (col = 1; col < m; col++)
		{
			grids[row * m + col] = grids[row * m + col - 1] + grids[(row - 1) * m + col];
		}
	}
	return grids[m * n - 1];
}
int main(int argc, char **argv)
{
	if (argc != 3)
	{
		fprintf(stderr, "Usage: ./test m n\n");
		exit(-1);
	}
	printf("%d\n", uniquePaths(atoi(argv[1]), atoi(argv[2])));
	return 0;
}
```

## 答案

```cpp
grids[row * m + col] = grids[row * m + col - 1] + grids[(row - 1) * m + col];
```

## 选项

### A

```cpp
grids[row * m + col] = grids[row * m + col] + grids[row * m + col];
```

### B

```cpp
grids[row * m + col] = grids[row * m + col - 1] + grids[row * m + col + 1];
```

### C

```cpp
grids[row * m + col] = grids[row * m + col + 1] + grids[row * m + col];
```