solution.md 5.5 KB
Newer Older
1
# 不同路径 II
F
fix bug  
feilong 已提交
2

3 4 5
<p>一个机器人位于一个 <em>m x n </em>网格的左上角 (起始点在下图中标记为“Start” )。</p>
<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。</p>
<p>现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?</p>
每日一练社区's avatar
每日一练社区 已提交
6
<p><img src="https://img-blog.csdnimg.cn/img_convert/dcc604a2757b6fd0e6bdc5155c945ffc.png#pic_center" /></p>
7
<p>网格中的障碍物和空位置分别用 <code>1</code><code>0</code> 来表示。</p>
F
fix bug  
feilong 已提交
8

9
<p><strong>示例 1:</strong></p><img alt=""
每日一练社区's avatar
每日一练社区 已提交
10
    src="https://img-blog.csdnimg.cn/img_convert/d127c16fad8048e19db6f2a2bea73240.png#pic_center" />
11 12
<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=""
每日一练社区's avatar
每日一练社区 已提交
13
    src="https://img-blog.csdnimg.cn/img_convert/d95382b3c9b30e83be3e383421106a67.png#pic_center" />
14
<pre><strong>输入:</strong>obstacleGrid = [[0,1],[0,0]]<strong><br />输出:</strong>1</pre>
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22
<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
每日一练社区 已提交
23
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
24

25
## aop
F
fix bug  
feilong 已提交
26

27
### before
F
fix bug  
feilong 已提交
28

每日一练社区's avatar
每日一练社区 已提交
29
```c
30 31 32
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
33

34
### after
F
fix bug  
feilong 已提交
35

每日一练社区's avatar
每日一练社区 已提交
36
```c
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
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 已提交
52

每日一练社区's avatar
每日一练社区 已提交
53
```c
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
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 已提交
80

81
### A
F
fix bug  
feilong 已提交
82

每日一练社区's avatar
每日一练社区 已提交
83
```c
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
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 已提交
115

每日一练社区's avatar
每日一练社区 已提交
116
```c
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
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 已提交
153

每日一练社区's avatar
每日一练社区 已提交
154
```c
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
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];
    }
};
```