力扣63. 不同路径 II

 

 

题目:【https://leetcode.cn/problems/unique-paths-ii/?envType=study-plan-v2&envId=top-interview-150

 

下面这个深度优先搜索超时了。。。不过还是记录一下,看看自己失败在那里。。。

 1 class Solution {
 2 public:
 3     int body(vector<vector<int>>& arr, int x, int y)
 4     {
 5         if (1 == arr[y][x])
 6             return 0;
 7         if (max_x == x && max_y == y)
 8             return 1;
 9         if (max_x == x)
10             return body(arr, x, y + 1);
11         else if (max_y == y)
12             return body(arr, x + 1, y);
13         else
14             return body(arr, x + 1, y) + body(arr, x, y + 1);
15     }
16     int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
17         max_x = obstacleGrid[0].size() - 1;
18         max_y = obstacleGrid.size() - 1;
19         return body(obstacleGrid, 0, 0);
20     }
21 
22 private:
23     int max_x;
24     int max_y;
25 };

 

进化一个版本,二维dp,存储也是二维,初始状态的0判断是为了防止入口放障碍物。。。

 1 class Solution {
 2 public:
 3     int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
 4         int h = obstacleGrid.size(), l = obstacleGrid[0].size();
 5         vector dp(h, vector<int>(l));
 6         dp[0][0] = 0 == obstacleGrid[0][0];
 7         for (int i = 0; i < h; ++i) {
 8             for (int j = 0; j < l; ++j) {
 9                 if (0 == obstacleGrid[i][j]) {
10                     int a = j - 1 >= 0 ? dp[i][j - 1] : 0;
11                     int b = i - 1 >= 0 ? dp[i - 1][j] : 0;
12                     dp[i][j] += a + b;
13                 }
14             }
15         }
16 
17         // for (int i = 0; i < h; i++) {
18         //     for (int j = 0; j < l; j++) {
19         //         cout << " " << dp[i][j];
20         //     }
21         //     cout << endl;
22         // }
23         return dp.back().back();
24     }
25 };

 

空间优化一下

 1 class Solution {
 2 public:
 3     int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
 4         int h = obstacleGrid.size(), l = obstacleGrid[0].size();
 5         // vector dp(h, vector<int>(l));
 6         // dp[0][0] = 0 == obstacleGrid[0][0];
 7         vector<int> dp(l);
 8         dp[0] = 0 == obstacleGrid[0][0];
 9         for (int i = 0; i < h; ++i) {
10             for (int j = 0; j < l; ++j) {
11                 if (0 == obstacleGrid[i][j]) {
12                     int a = j - 1 >= 0 ? dp[j - 1] : 0;
13                     // int b = i - 1 >= 0 ? dp[i - 1][j] : 0;
14                     // dp[i][j] += a + b;
15                     dp[j] += a;
16                 } else
17                     dp[j] = 0;
18             }
19         }
20 
21         // for (int i = 0; i < h; i++) {
22         //     for (int j = 0; j < l; j++) {
23         //         cout << " " << dp[i][j];
24         //     }
25         //     cout << endl;
26         // }
27         // return dp.back().back();
28         return dp.back();
29     }
30 };

 

最终结果

 1 class Solution {
 2 public:
 3     int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
 4         int h = obstacleGrid.size(), l = obstacleGrid[0].size();
 5         vector<int> dp(l);
 6         dp[0] = 0 == obstacleGrid[0][0] ? 1 : 0;
 7         for (int i = 0; i < h; ++i) {
 8             for (int j = 0; j < l; ++j) {
 9                 if (0 == obstacleGrid[i][j]) {
10                     int a = j - 1 >= 0 ? dp[j - 1] : 0;
11                     dp[j] += a;
12                 } else
13                     dp[j] = 0;
14             }
15         }
16         return dp.back();
17     }
18 };

 

posted @ 2025-05-09 17:12  J&YANG  阅读(8)  评论(0)    收藏  举报