Unique Paths II

来源:互联网 发布:条形码数据采集器用法 编辑:程序博客网 时间:2024/06/06 02:12
class Solution {public:    int paths[100][100];    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {        int m = obstacleGrid.size(), n = 0;        if(0 != m) n = obstacleGrid[0].size();        if(0 == n || 0 == m) return 0;        for(int i = 0; i < m; ++i){            if(1 == obstacleGrid[i][0]){                while(i < m)                    paths[i++][0] = 0;            }else{                paths[i][0] = 1;            }        }        for(int j = 0; j < n; ++j){            if(1 == obstacleGrid[0][j]){                while(j < n)                    paths[0][j++] = 0;                }else{                    paths[0][j] = 1;                }        }        for(int i = 1; i < m; ++i){            for(int j = 1; j < n; ++j){                if(1 == obstacleGrid[i][j])                    paths[i][j] = 0;                else                    paths[i][j] = paths[i-1][j] + paths[i][j-1];            }        }        return paths[m-1][n-1];    }};