LeetCode 20200205 (different paths II)

II 1. Different paths
of a first row and a first column reset i.e. obstacle for the latter are set to zero, and
then the middle of the intermediate elements is provided if there are obstacles on the current zero

Like the rest of the solution of different paths

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));
        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];
    }
};
Published 44 original articles · won praise 9 · views 3330

Guess you like

Origin blog.csdn.net/puying1/article/details/104209951