solution.md 5.3 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# 不同路径 II

<p>一个机器人位于一个 <em>m x n </em>网格的左上角 (起始点在下图中标记为“Start” )。</p>
<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。</p>
<p>现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?</p>
<p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot_maze.png"
        style="height: 183px; width: 400px;" /></p>
<p>网格中的障碍物和空位置分别用 <code>1</code><code>0</code> 来表示。</p>
<p> </p>
<p><strong>示例 1:</strong></p><img alt=""
    src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot1.jpg"
    style="width: 242px; height: 242px;" />
<pre><strong>输入:</strong>obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]<strong><br />输出:</strong>2<strong><br />解释:</strong>3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 2 条不同的路径:<br />1. 向右 -> 向右 -> 向下 -> 向下<br />2. 向下 -> 向下 -> 向右 -> 向右</pre>
<p><strong>示例 2:</strong></p><img alt=""
    src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot2.jpg"
    style="width: 162px; height: 162px;" />
<pre><strong>输入:</strong>obstacleGrid = [[0,1],[0,0]]<strong><br />输出:</strong>1</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
    <li><code>m == obstacleGrid.length</code></li>
    <li><code>n == obstacleGrid[i].length</code></li>
    <li><code>1 <= m, n <= 100</code></li>
    <li><code>obstacleGrid[i][j]</code><code>0</code><code>1</code></li>
</ul>

每日一练社区's avatar
每日一练社区 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
以下程序实现了这一功能,请你填补空白处内容:

```cpp
#include <stdio.h>
#include <stdlib.h>
static int uniquePathsWithObstacles(int **obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize)
{
	int row, col;
	int reset = 0;
	for (row = 0; row < obstacleGridRowSize; row++)
	{
		if (reset)
		{
			obstacleGrid[row][0] = 1;
		}
		else
		{
			if (obstacleGrid[row][0] == 1)
			{
				reset = 1;
			}
		}
	}
	reset = 0;
	for (col = 0; col < obstacleGridColSize; col++)
	{
		if (reset)
		{
			obstacleGrid[0][col] = 1;
		}
		else
		{
			if (obstacleGrid[0][col] == 1)
			{
				reset = 1;
			}
		}
	}
	for (row = 0; row < obstacleGridRowSize; row++)
	{
		int *line = obstacleGrid[row];
		for (col = 0; col < obstacleGridColSize; col++)
		{
			line[col] ^= 1;
		}
	}
	for (row = 1; row < obstacleGridRowSize; row++)
	{
		int *last_line = obstacleGrid[row - 1];
		int *line = obstacleGrid[row];
		for (col = 1; col < obstacleGridColSize; col++)
		{
ToTensor's avatar
ToTensor 已提交
79
			________________________;
每日一练社区's avatar
每日一练社区 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		}
	}
	return obstacleGrid[obstacleGridRowSize - 1][obstacleGridColSize - 1];
}
int main(int argc, char **argv)
{
	if (argc < 3)
	{
		fprintf(stderr, "Usage: ./test m n\n");
		exit(-1);
	}
	int i, j, k = 3;
	int row_size = atoi(argv[1]);
	int col_size = atoi(argv[2]);
	int **grids = malloc(row_size * sizeof(int *));
	for (i = 0; i < row_size; i++)
	{
		grids[i] = malloc(col_size * sizeof(int));
		int *line = grids[i];
		for (j = 0; j < col_size; j++)
		{
			line[j] = atoi(argv[k++]);
			printf("%d ", line[j]);
		}
		printf("\n");
	}
	printf("%d\n", uniquePathsWithObstacles(grids, row_size, col_size));
	return 0;
}
```

每日一练社区's avatar
每日一练社区 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
## template

```cpp
#include <stdio.h>
#include <stdlib.h>
static int uniquePathsWithObstacles(int **obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize)
{
	int row, col;
	int reset = 0;
	for (row = 0; row < obstacleGridRowSize; row++)
	{
		if (reset)
		{
			obstacleGrid[row][0] = 1;
		}
		else
		{
			if (obstacleGrid[row][0] == 1)
			{
				reset = 1;
			}
		}
	}
	reset = 0;
	for (col = 0; col < obstacleGridColSize; col++)
	{
		if (reset)
		{
			obstacleGrid[0][col] = 1;
		}
		else
		{
			if (obstacleGrid[0][col] == 1)
			{
				reset = 1;
			}
		}
	}
	for (row = 0; row < obstacleGridRowSize; row++)
	{
		int *line = obstacleGrid[row];
		for (col = 0; col < obstacleGridColSize; col++)
		{
			line[col] ^= 1;
		}
	}
	for (row = 1; row < obstacleGridRowSize; row++)
	{
		int *last_line = obstacleGrid[row - 1];
		int *line = obstacleGrid[row];
		for (col = 1; col < obstacleGridColSize; col++)
		{
			if (line[col] != 0)
			{
				line[col] = line[col - 1] + last_line[col];
			}
		}
	}
	return obstacleGrid[obstacleGridRowSize - 1][obstacleGridColSize - 1];
}
int main(int argc, char **argv)
{
	if (argc < 3)
	{
		fprintf(stderr, "Usage: ./test m n\n");
		exit(-1);
	}
	int i, j, k = 3;
	int row_size = atoi(argv[1]);
	int col_size = atoi(argv[2]);
	int **grids = malloc(row_size * sizeof(int *));
	for (i = 0; i < row_size; i++)
	{
		grids[i] = malloc(col_size * sizeof(int));
		int *line = grids[i];
		for (j = 0; j < col_size; j++)
		{
			line[j] = atoi(argv[k++]);
			printf("%d ", line[j]);
		}
		printf("\n");
	}
	printf("%d\n", uniquePathsWithObstacles(grids, row_size, col_size));
	return 0;
}
```

## 答案

```cpp
每日一练社区's avatar
每日一练社区 已提交
201 202 203 204
if (line[col] != 0)
{
	line[col] = line[col - 1] + last_line[col];
}
每日一练社区's avatar
每日一练社区 已提交
205 206 207 208 209 210 211
```

## 选项

### A

```cpp
每日一练社区's avatar
每日一练社区 已提交
212 213 214 215
if (line[col] == 0)
{
	line[col] = line[col - 1] + last_line[col];
}
每日一练社区's avatar
每日一练社区 已提交
216 217 218 219 220
```

### B

```cpp
每日一练社区's avatar
每日一练社区 已提交
221 222 223 224
if (line[col] != 0)
{
	line[col] = line[col + 1] + last_line[col];
}
每日一练社区's avatar
每日一练社区 已提交
225 226 227 228 229
```

### C

```cpp
每日一练社区's avatar
每日一练社区 已提交
230 231 232 233
if (line[col] == 0)
{
	line[col] = line[col + 1] + last_line[col];
}
每日一练社区's avatar
每日一练社区 已提交
234
```