Unique Paths II

来源:互联网 发布:网站域名是什么 编辑:程序博客网 时间:2024/06/08 09:50

二维动态规划。

class Solution {public:    int map[101][101];    int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {        for (int i = 0; i < 101; ++i)            for (int j = 0; j < 101; ++j)                map[i][j] = -1;        if (obstacleGrid.size() == 0)            return 0;        if (obstacleGrid[0].size() == 0)            return 0;        return helper(obstacleGrid, 0, 0, obstacleGrid.size() - 1, obstacleGrid[0].size() - 1);    }    int helper(vector<vector<int>> &obstacleGrid, int i, int j, int m, int n) {        if (i == m && j == n && obstacleGrid[i][j] != 1) {            map[i][j] = 1;            return map[i][j];        }        if (i > m || j > n || obstacleGrid[i][j] == 1)            return 0;        if (map[i][j] != -1)            return map[i][j];        map[i][j] = helper(obstacleGrid, i + 1, j, m, n) + helper(obstacleGrid, i, j + 1, m, n);        return map[i][j];    }};

http://oj.leetcode.com/problems/unique-paths-ii/

0 0
原创粉丝点击