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.
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.
- 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
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 firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Unique paths covering every non-obstacle block exactly once in a grid
- 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
- Number of cells a queen can move with obstacles on the chessborad
- Sum of lengths of all paths possible in a given tree
- Print all possible paths from the first row to the last row in a 2D array
- Number of paths with exactly k coins
- Count of palindromic plus paths in a given Matrix
- Number of palindromic paths in a matrix
- Count number of paths with at-most k turns
- Number of Paths of Weight W in a K-ary tree
- Minimum sum falling path in a NxN grid
- Count Magic squares in a grid
- Find an N x N grid whose xor of every row and column is equal
- Largest connected component on a grid
- Maximum perimeter of a square in a 2D grid
- Check if a word exists in a grid or not
- Minimum distance to the end of a grid from source
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Count possible moves in the given direction in a grid