Given a grid of size m * n, let us assume you are starting at (1, 1) and your goal is to reach (m, n). At any instance, if you are on (x, y), you can either go to (x, y + 1) or (x + 1, y).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space are marked as 1 and 0 respectively in the grid.

**Examples: **

Input: [[0, 0, 0], [0, 1, 0], [0, 0, 0]] Output : 2 There is only one obstacle in the middle.

We have discussed a problem to count the number of unique paths in a Grid when no obstacle was present in the grid. But here the situation is quite different. While moving through the grid, we can get some obstacles which we can not jump and that way to reach the bottom right corner is blocked.

The most efficient solution to this problem can be achieved using dynamic programming. Like every dynamic problem concept, we will not recompute the subproblems. A temporary 2D matrix will be constructed and value will be stored using the bottom up approach.

### Approach

- Create a 2D matrix of same size of the given matrix to store the results.
- Traverse through the created array row wise and start filling the values in it.
- If an obstacle is found, set the value to 0.
- For the first row and column, set the value to 1 if obstacle is not found.
- Set the sum of the right and the upper values if obstacle is not present at that corresponding position in the given matirx
- Return the last value of the created 2d matrix

## C++

`// C++ code to find number of unique paths ` `// in a Matrix` `#include<bits/stdc++.h>` `using` `namespace` `std;` `int` `uniquePathsWithObstacles(vector<vector<` `int` `>>& A) ` `{` ` ` ` ` `int` `r = A.size(), c = A[0].size(); ` ` ` ` ` `// create a 2D-matrix and initializing` ` ` `// with value 0` ` ` `vector<vector<` `int` `>> paths(r, vector<` `int` `>(c, 0));` ` ` ` ` `// Initializing the left corner if` ` ` `// no obstacle there` ` ` `if` `(A[0][0] == 0)` ` ` `paths[0][0] = 1;` ` ` ` ` `// Initializing first column of` ` ` `// the 2D matrix` ` ` `for` `(` `int` `i = 1; i < r; i++)` ` ` `{` ` ` `// If not obstacle` ` ` `if` `(A[i][0] == 0)` ` ` `paths[i][0] = paths[i-1][0];` ` ` `} ` ` ` ` ` `// Initializing first row of the 2D matrix` ` ` `for` `(` `int` `j = 1; j < c; j++)` ` ` `{` ` ` ` ` `// If not obstacle` ` ` `if` `(A[0][j] == 0)` ` ` `paths[0][j] = paths[0][j - 1];` ` ` `} ` ` ` ` ` `for` `(` `int` `i = 1; i < r; i++)` ` ` `{` ` ` `for` `(` `int` `j = 1; j < c; j++)` ` ` `{` ` ` ` ` `// If current cell is not obstacle ` ` ` `if` `(A[i][j] == 0)` ` ` `paths[i][j] = paths[i - 1][j] +` ` ` `paths[i][j - 1]; ` ` ` `} ` ` ` `}` ` ` ` ` `// Returning the corner value ` ` ` `// of the matrix` ` ` `return` `paths[r - 1];` `}` `// Driver code` `int` `main()` `{` ` ` `vector<vector<` `int` `>> A = { { 0, 0, 0 },` ` ` `{ 0, 1, 0 },` ` ` `{ 0, 0, 0 } };` ` ` ` ` `cout << uniquePathsWithObstacles(A) << ` `" \n"` `; ` `}` `// This code is contributed by ajaykr00kj` |

*chevron_right*

*filter_none*

## Python

`# Python code to find number of unique paths in a ` `# matrix with obstacles.` `def` `uniquePathsWithObstacles(A):` ` ` `# create a 2D-matrix and initializing with value 0` ` ` `paths ` `=` `[[` `0` `]` `*` `len` `(A[` `0` `]) ` `for` `i ` `in` `A]` ` ` ` ` `# initializing the left corner if no obstacle there` ` ` `if` `A[` `0` `][` `0` `] ` `=` `=` `0` `:` ` ` `paths[` `0` `][` `0` `] ` `=` `1` ` ` ` ` `# initializing first column of the 2D matrix` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(A)):` ` ` ` ` `# If not obstacle` ` ` `if` `A[i][` `0` `] ` `=` `=` `0` `:` ` ` `paths[i][` `0` `] ` `=` `paths[i` `-` `1` `][` `0` `]` ` ` ` ` `# initializing first row of the 2D matrix` ` ` `for` `j ` `in` `range` `(` `1` `, ` `len` `(A[` `0` `])):` ` ` ` ` `# If not obstacle` ` ` `if` `A[` `0` `][j] ` `=` `=` `0` `:` ` ` `paths[` `0` `][j] ` `=` `paths[` `0` `][j` `-` `1` `]` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(A)):` ` ` `for` `j ` `in` `range` `(` `1` `, ` `len` `(A[` `0` `])):` ` ` `# If current cell is not obstacle` ` ` `if` `A[i][j] ` `=` `=` `0` `:` ` ` `paths[i][j] ` `=` `paths[i` `-` `1` `][j] ` `+` `paths[i][j` `-` `1` `]` ` ` `# returning the corner value of the matrix` ` ` `return` `paths[` `-` `1` `][` `-` `1` `]` `# Driver Code` `A ` `=` `[[` `0` `, ` `0` `, ` `0` `], [` `0` `, ` `1` `, ` `0` `], [` `0` `, ` `0` `, ` `0` `]]` `print` `(uniquePathsWithObstacles(A))` |

*chevron_right*

*filter_none*

**Output**

2

This article is contributed by **Rishabh Bansal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Unique paths covering every non-obstacle block exactly once in a grid
- Number of cells a queen can move with obstacles on the chessborad
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Count unique paths is a matrix whose product of elements contains odd number of divisors
- Count unique paths with given sum in an N-ary Tree
- Collect maximum points in a grid using two traversals
- Search a Word in a 2D Grid of characters
- Count number of ways to fill a "n x 4" grid using "1 x 4" tiles
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Shortest distance between two cells in a matrix or grid
- Largest connected component on a grid
- Minimum product in a grid of adjacent elements
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Minimum sum falling path in a NxN grid
- Count Magic squares in a grid
- Minimum cost to cover the given positions in a N*M grid