有些障碍的 unique path

我第一时间想到的方案是化归而不是规划,毕竟之前的问题类似,所以就有了这个版本:

int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
    if (obstacleGrid.empty()){
        return 0;
    }
    const int begin = obstacleGrid.front().front();
    if (begin == 1){
        return 0;
    }
    const size_t m = obstacleGrid.size();
    const size_t n = obstacleGrid.front().size();

    vector<vector<int>> record((m), vector<int>(n));
    function<int(size_t, size_t)> getPathsNum;
    getPathsNum = [&](int a, int b)
    {
        if (obstacleGrid[a][b] == 1){
            return 0;
        }

        if (a == 0 && b == 0){
            return 1;
        }
        int&& result = 0;
        if (a == 0){
            result = getPathsNum(0, b - 1);
            record[a][b] = result;
            return result;
        }
        if (b == 0){
            result = getPathsNum(a - 1, 0);
            record[a][b] = result;
            return result;
        }
        const int paths_num = record[a][b];
        if (paths_num != 0){
            return paths_num;
        }
        result = getPathsNum(a - 1, b) + getPathsNum(a, b - 1);
        record[a][b] = result;
        return result;
    };
    return getPathsNum(m - 1, n - 1);
}

感觉:嗯嗯,可以可以。一测时间, 92 ms。尼玛用 ruby 的都比我速度快,这玩毛。

后来发现我又陷入 DP 就是递归的思维定势里了,f(m, n) = f(m - 1, n) + f(m, n -1) 这个式子固然可以反着推,但正着填充不是更好?这才发现上面的版本根本不是用的动态规划的思想。

真正 DP 的思想是填充。当然填充也很简单大概意思就是 result[m, n] = result[m - 1, n] + result[m, n - 1]。这才是动规的思想好不拉!

能更省空间吗?我发现其实每一行的结果只依赖上一行,再往上的对它并没有直接影响。那还存储它们作甚?所以只需要一行数据的空间,然后逐行扫描。

int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
    if (obstacleGrid.empty() || obstacleGrid[0][0] == 1 ||
        obstacleGrid.back().back() == 1){
        return 0;
    }
    const size_t row_length = obstacleGrid[0].size();
    vector<int> row(row_length);
    auto const& first_row = obstacleGrid[0];
    bool is_blocked = false;
    for (size_t i = 0; i != row_length; ++i){
        if (is_blocked || first_row[i] == 1){
            row[i] = 0;
            is_blocked = true;
        }else{
            row[i] = 1;
        }
    }
    for (size_t i = 1; i != obstacleGrid.size(); ++i){
        for (size_t j = 0; j != row_length; ++j){
            if (obstacleGrid[i][j] == 1){
                row[j] = 0;
            }
            else if (j > 0){
                row[j] = row[j] + row[j - 1];
            }
        }
    }
    return row.back();
}
时间: 2024-10-24 15:37:37

有些障碍的 unique path的相关文章

LeetCode 63. Unique Path II(所有不同路径之二)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

[LeetCode]题解(python):062 Unique path

题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bott

63. Unique Path II Leetcode Python

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

#Leet Code# Unique Path

描述: 使用了递归,有些计算是重复的,用了额外的空间,Version 1是m*n Bonus:一共走了m+n步,例如 m = 2, n = 3 [#, @, @, #, @],所以抽象成数学问题,解是C(m + n, m) 代码: 1 class Solution: 2 # @return an integer 3 def __init__(self): 4 self.record = {} 5 6 def uniquePaths(self, m, n): 7 if m == 0 or n ==

leetcode63&mdash;Unique Path II

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t

Unique path ii

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

【leetcode】 Unique Path ||(easy)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

每日算法之四十四:Unique Path(矩阵中不重复路径的数目)

Unique Paths: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked

unique path 阶梯

一.思路 迷宫出逃不知道还记得不? 只不过这里的迷宫有进口和出口了. 递归解题,但是数据过大就超时了 import java.util.Stack; public class UniquePath { public int[][] path={ {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0} }; public int[][] dire={ {1,0}, {0,1} }; public int count=0; /** * 查找线路递归 */