int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize) {
int i, j;
int m = obstacleGridRowSize - 1;
int n = obstacleGridColSize - 1;
if(obstacleGridRowSize == 0 || obstacleGridColSize == 0 || obstacleGrid[m][n] == 1 || obstacleGrid[0][0] == 1) return 0;
else if(obstacleGridRowSize == 1 && obstacleGridColSize == 1) return 1;
else if(obstacleGridRowSize == 1){
obstacleGrid[m][n] = 1;
for(j = n; j > 0; --j){
if(obstacleGrid[0][j] == 1 && obstacleGrid[0][j - 1] == 0)
obstacleGrid[0][j - 1] = 1;
else
obstacleGrid[0][j - 1] = 0;
}
return obstacleGrid[0][0];
}
else if(obstacleGridColSize == 1){
obstacleGrid[m][n] = 1;
for(i = m; i > 0; --i){
if(obstacleGrid[i][0] == 1 && obstacleGrid[i - 1][0] == 0)
obstacleGrid[i - 1][0] = 1;
else
obstacleGrid[i - 1][0] = 0;
}
return obstacleGrid[0][0];
}
else{
obstacleGrid[m][n] = 1;
for(j = n; j > 0; --j){
if(obstacleGrid[m][j] == 1 && obstacleGrid[m][j - 1] == 0)
obstacleGrid[m][j - 1] = 1;
else
obstacleGrid[m][j - 1] = 0;
}
for(i = m; i > 0; --i){
if(obstacleGrid[i][n] == 1 && obstacleGrid[i - 1][n] == 0)
obstacleGrid[i - 1][n] = 1;
else
obstacleGrid[i - 1][n] = 0;
}
for(i = m - 1; i >= 0; --i)
for(j = n - 1; j >= 0; --j){
if(obstacleGrid[i][j] == 1) obstacleGrid[i][j] = 0;
else obstacleGrid[i][j] = obstacleGrid[i + 1][j] + obstacleGrid[i][j + 1];
}
return obstacleGrid[0][0];
}
}
int i, j;
int m = obstacleGridRowSize - 1;
int n = obstacleGridColSize - 1;
if(obstacleGridRowSize == 0 || obstacleGridColSize == 0 || obstacleGrid[m][n] == 1 || obstacleGrid[0][0] == 1) return 0;
else if(obstacleGridRowSize == 1 && obstacleGridColSize == 1) return 1;
else if(obstacleGridRowSize == 1){
obstacleGrid[m][n] = 1;
for(j = n; j > 0; --j){
if(obstacleGrid[0][j] == 1 && obstacleGrid[0][j - 1] == 0)
obstacleGrid[0][j - 1] = 1;
else
obstacleGrid[0][j - 1] = 0;
}
return obstacleGrid[0][0];
}
else if(obstacleGridColSize == 1){
obstacleGrid[m][n] = 1;
for(i = m; i > 0; --i){
if(obstacleGrid[i][0] == 1 && obstacleGrid[i - 1][0] == 0)
obstacleGrid[i - 1][0] = 1;
else
obstacleGrid[i - 1][0] = 0;
}
return obstacleGrid[0][0];
}
else{
obstacleGrid[m][n] = 1;
for(j = n; j > 0; --j){
if(obstacleGrid[m][j] == 1 && obstacleGrid[m][j - 1] == 0)
obstacleGrid[m][j - 1] = 1;
else
obstacleGrid[m][j - 1] = 0;
}
for(i = m; i > 0; --i){
if(obstacleGrid[i][n] == 1 && obstacleGrid[i - 1][n] == 0)
obstacleGrid[i - 1][n] = 1;
else
obstacleGrid[i - 1][n] = 0;
}
for(i = m - 1; i >= 0; --i)
for(j = n - 1; j >= 0; --j){
if(obstacleGrid[i][j] == 1) obstacleGrid[i][j] = 0;
else obstacleGrid[i][j] = obstacleGrid[i + 1][j] + obstacleGrid[i][j + 1];
}
return obstacleGrid[0][0];
}
}