Count number of ways to reach destination in a Maze using BFS
Given a maze with obstacles, count number of paths to reach rightmost-bottom most cell from the topmost-leftmost cell. A cell in the given maze has value -1 if it is a blockage or dead-end, else 0.
From a given cell, we are allowed to move to cells (i+1, j) and (i, j+1) only.
Examples:
Input: mat[][] = {
{1, 0, 0, 1},
{1, 1, 1, 1},
{1, 0, 1, 1}}
Output: 2
Input: mat[][] = {
{1, 1, 1, 1},
{1, 0, 1, 1},
{0, 1, 1, 1},
{1, 1, 1, 1}}
Output: 4
Approach: The idea is to use a queue and apply bfs and use a variable count to store the number of possible paths. Make a pair of row and column and insert (0, 0) into the queue. Now keep popping pairs from queue, if the popped value is the end of matrix then increment count, otherwise check if the next column can give a valid move or the next row can give a valid move and according to that, insert the corresponding row, column pair into the queue.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define m 4 #define n 3 // Function to return the number of valid // paths in the given maze int Maze(int matrix[n][m]) { queue<pair<int, int> > q; // Insert the starting point i.e. // (0, 0) in the queue q.push(make_pair(0, 0)); // To store the count of possible paths int count = 0; while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); // Increment the count of paths since // it is the destination if (p.first == n - 1 && p.second == m - 1) count++; // If moving to the next row is a valid move if (p.first + 1 < n && matrix[p.first + 1][p.second] == 1) { q.push(make_pair(p.first + 1, p.second)); } // If moving to the next column is a valid move if (p.second + 1 < m && matrix[p.first][p.second + 1] == 1) { q.push(make_pair(p.first, p.second + 1)); } } return count; } // Driver code int main() { // Matrix to represent maze int matrix[n][m] = { { 1, 0, 0, 1 }, { 1, 1, 1, 1 }, { 1, 0, 1, 1 } }; cout << Maze(matrix); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { static int m = 4; static int n = 3; static class pair { int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } // Function to return the number of valid // paths in the given maze static int Maze(int matrix[][]) { Queue<pair> q = new LinkedList<>(); // Insert the starting point i.e. // (0, 0) in the queue q.add(new pair(0, 0)); // To store the count of possible paths int count = 0; while (!q.isEmpty()) { pair p = q.peek(); q.remove(); // Increment the count of paths since // it is the destination if (p.first == n - 1 && p.second == m - 1) count++; // If moving to the next row is a valid move if (p.first + 1 < n && matrix[p.first + 1][p.second] == 1) { q.add(new pair(p.first + 1, p.second)); } // If moving to the next column is a valid move if (p.second + 1 < m && matrix[p.first][p.second + 1] == 1) { q.add(new pair(p.first, p.second + 1)); } } return count; } // Driver code public static void main(String[] args) { // Matrix to represent maze int matrix[][] = {{ 1, 0, 0, 1 }, { 1, 1, 1, 1 }, { 1, 0, 1, 1 }}; System.out.println(Maze(matrix)); } } // This code is contributed by Princi Singh |
Python3
# Python3 implementation of the approach from collections import deque m = 4n = 3 # Function to return the number of valid # paths in the given maze def Maze(matrix): q = deque() # Insert the starting poi.e. # (0, 0) in the queue q.append((0, 0)) # To store the count of possible paths count = 0 while (len(q) > 0): p = q.popleft() # Increment the count of paths since # it is the destination if (p[0] == n - 1 and p[1] == m - 1): count += 1 # If moving to the next row is a valid move if (p[0] + 1 < n and matrix[p[0] + 1][p[1]] == 1): q.append((p[0] + 1, p[1])) # If moving to the next column is a valid move if (p[1] + 1 < m and matrix[p[0]][p[1] + 1] == 1): q.append((p[0], p[1] + 1)) return count # Driver code # Matrix to represent maze matrix = [ [ 1, 0, 0, 1 ], [ 1, 1, 1, 1 ], [ 1, 0, 1, 1 ] ] print(Maze(matrix)) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { static int m = 4; static int n = 3; class pair { public int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } // Function to return the number of valid // paths in the given maze static int Maze(int [,]matrix) { Queue<pair> q = new Queue<pair>(); // Insert the starting point i.e. // (0, 0) in the queue q.Enqueue(new pair(0, 0)); // To store the count of possible paths int count = 0; while (q.Count != 0) { pair p = q.Peek(); q.Dequeue(); // Increment the count of paths since // it is the destination if (p.first == n - 1 && p.second == m - 1) count++; // If moving to the next row is a valid move if (p.first + 1 < n && matrix[p.first + 1, p.second] == 1) { q.Enqueue(new pair(p.first + 1, p.second)); } // If moving to the next column is a valid move if (p.second + 1 < m && matrix[p.first, p.second + 1] == 1) { q.Enqueue(new pair(p.first, p.second + 1)); } } return count; } // Driver code public static void Main(String[] args) { // Matrix to represent maze int [,]matrix = {{ 1, 0, 0, 1 }, { 1, 1, 1, 1 }, { 1, 0, 1, 1 }}; Console.WriteLine(Maze(matrix)); } } // This code is contributed by PrinciRaj1992 |
2
Recommended Posts:
- Count number of ways to reach destination in a maze
- Count number of ways to reach destination in a Maze
- Count number of ways to reach a given score in a Matrix
- Number of decisions to reach destination
- Number of ways to reach the end of matrix with non-zero AND value
- Minimum steps to reach a destination
- Minimum Initial Points to Reach Destination
- Minimum cells required to reach destination with jumps equal to cell values
- Count the number of ways to traverse a Matrix
- Minimize the number of steps required to reach the end of the array | Set 2
- Find the minimum number of moves to reach end of the array
- Check if it is possible to reach a number by making jumps of two given length
- Maze With N doors and 1 Key
- Rat in a Maze | Backtracking-2
- Rat in a Maze | Backtracking using Stack
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



