solution.md 5.7 KB
Newer Older
1
# 不同路径 II
F
fix bug  
feilong 已提交
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
<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>
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
27

28
## aop
F
fix bug  
feilong 已提交
29

30
### before
F
fix bug  
feilong 已提交
31

32 33 34 35 36
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
```cpp
int main()
{
    Solution sol;
    int a = 2;
    int b = 4;
    vector<vector<int>> obstacleGrid = vector<vector<int>>(a, vector<int>(b)) = {{0, 0, 0, 0}, {0, 1, 2, 3}};
    int res;

    res = sol.uniquePathsWithObstacles(obstacleGrid);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
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 79 80 81
```cpp
class Solution
{
public:
    int dp[110];
    int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid)
    {
        int m = obstacleGrid.size(), n = obstacleGrid[0].size();
        if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1)
            return 0;
        dp[1] = 1;
        for (int i = 1; i <= m; ++i)
        {
            for (int j = 1; j <= n; ++j)
            {
                if (obstacleGrid[i - 1][j - 1] == 0)
                    dp[j] += dp[j - 1];
                else
                    dp[j] = 0;
            }
        }
        return dp[n];
    }
};
```
## 选项

F
fix bug  
feilong 已提交
82

83
### A
F
fix bug  
feilong 已提交
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 111 112 113 114 115 116
```cpp
class Solution
{
public:
    int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid)
    {
        int m = obstacleGrid.size(), n = obstacleGrid[0].size();
        vector<vector<int>> dp(m, vector<int>(n, 0));
        for (int i = 0; i < m && obstacleGrid[i][0] != 1; i++)
        {
            dp[i][0] = 1;
        }
        for (int i = 0; i < n && obstacleGrid[0][i] != 1; i++)
        {
            dp[0][i] = 1;
        }
        for (int i = 1; i < m; i++)
        {
            for (int j = 1; j < n; j++)
            {
                if (obstacleGrid[i][j] != 1)
                {
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
        }
        return dp[m - 1][n - 1];
    }
};
```

### B
F
fix bug  
feilong 已提交
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
```cpp
class Solution
{
public:
    int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid)
    {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        int p[m][n];

        int k = 0;
        while (k < m && obstacleGrid[k][0] != 1)
            p[k++][0] = 1;

        while (k < m)
            p[k++][0] = 0;

        k = 0;
        while (k < n && obstacleGrid[0][k] != 1)
            p[0][k++] = 1;
        while (k < n)
            p[0][k++] = 0;

        for (int i = 1; i < m; i++)
            for (int j = 1; j < n; j++)
            {
                if (obstacleGrid[i][j] == 1)
                    p[i][j] = 0;
                else
                    p[i][j] = p[i - 1][j] + p[i][j - 1];
            }
        return p[m - 1][n - 1];
    }
};
```

### C
F
fix bug  
feilong 已提交
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 201 202 203 204 205 206 207 208 209 210
```cpp
class Solution
{
public:
    int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid)
    {
        if (obstacleGrid.size() == 0 || obstacleGrid[0].size() == 0)
            return 0;
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        vector<vector<int>> info(m, vector<int>(n, 0));
        for (int i = 0; i < m; ++i)
        {
            if (obstacleGrid[i][0] == 1)
            {
                for (int j = i; j < m; j++)
                {
                    info[j][0] = 0;
                }
                break;
            }
            else
                info[i][0] = 1;
        }
        for (int i = 0; i < n; ++i)
        {
            if (obstacleGrid[0][i] == 1)
            {
                for (int j = i; j < n; ++j)
                {
                    info[0][j] = 0;
                }
                break;
            }
            else
                info[0][i] = 1;
        }
        for (int i = 1; i < m; ++i)
        {
            for (int j = 1; j < n; ++j)
            {
                if (obstacleGrid[i][j] == 1)
                {
                    info[i][j] = 0;
                }
                else
                {
                    info[i][j] = info[i - 1][j] + info[i][j - 1];
                }
            }
        }
        return info[m - 1][n - 1];
    }
};
```