Given a grid which consists of 4 types of characters : ‘B’ ‘.’ ‘S’ and ‘D’. We need to reach D starting from S, at each step we can go to neighboring cells i.e. up, down, left and right. Cells having character ‘B’ are blocked i.e. at any step we can’t move to cell having ‘B’. Given grid has dots in such a way that there is only one way to reach any cell from any other cell. We need to tell how many times we need to choose our way from more than one choices i.e. decide the path to reach D.
Input : Grid = [".BBB.B.BB" ".....B.B." "B.B.B.BSB" ".DB......"] Output : 4 In above shown grid we have to decide 4 times to reach destination at (3, 7), (3, 5), (1, 3) and (1, 1).
We can solve this problem using DFS. In path from source to destination we can see that whenever we have more than 1 neighbors, we need to decide our path so first we do a DFS and store the path from source to the destination in terms of child-parent array and then we move from destination to source, cell by cell using parent array and at every cell where we have more than 1 neighbors we will increase our answer by 1.
Please see below code for better understanding.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
- Count number of ways to reach destination in a Maze using BFS
- Count number of ways to reach destination in a Maze
- Count number of ways to reach destination in a maze
- Minimum steps to reach a destination
- Minimum Initial Points to Reach Destination
- Count total ways to reach destination from source in an undirected Graph
- Minimum cells required to reach destination with jumps equal to cell values
- Number of ways to reach the end of matrix with non-zero AND value
- Count number of ways to reach a given score in a Matrix
- Number of ways to reach (X, Y) in a matrix starting from the origin
- Expected number of moves to reach the end of a board | Dynamic programming
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Print all paths from a given source to a destination
- Count all possible walks from a source to a destination with exactly k edges
- Source to destination in 2-D path with fixed sized jumps
- Counts paths from a point to reach Origin
- Minimum jumps to reach last building in a matrix
- Minimum steps required to reach the end of a matrix | Set 2