Count number of ways to reach destination in a Maze using BFS
Last Updated :
11 Mar, 2024
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: To solve the problem, follow the below idea:
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++
#include <bits/stdc++.h>
using namespace std;
#define m 4
#define n 3
int Maze( int matrix[n][m])
{
queue<pair< int , int > > q;
q.push(make_pair(0, 0));
int count = 0;
while (!q.empty()) {
pair< int , int > p = q.front();
q.pop();
if (p.first == n - 1 && p.second == m - 1)
count++;
if (p.first + 1 < n
&& matrix[p.first + 1][p.second] == 1) {
q.push(make_pair(p.first + 1, p.second));
}
if (p.second + 1 < m
&& matrix[p.first][p.second + 1] == 1) {
q.push(make_pair(p.first, p.second + 1));
}
}
return count;
}
int main()
{
int matrix[n][m] = { { 1, 0, 0, 1 },
{ 1, 1, 1, 1 },
{ 1, 0, 1, 1 } };
cout << Maze(matrix);
return 0;
}
|
Java
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;
}
}
static int Maze( int matrix[][])
{
Queue<pair> q = new LinkedList<>();
q.add( new pair( 0 , 0 ));
int count = 0 ;
while (!q.isEmpty())
{
pair p = q.peek();
q.remove();
if (p.first == n - 1 && p.second == m - 1 )
count++;
if (p.first + 1 < n &&
matrix[p.first + 1 ][p.second] == 1 )
{
q.add( new pair(p.first + 1 , p.second));
}
if (p.second + 1 < m &&
matrix[p.first][p.second + 1 ] == 1 )
{
q.add( new pair(p.first, p.second + 1 ));
}
}
return count;
}
public static void main(String[] args)
{
int matrix[][] = {{ 1 , 0 , 0 , 1 },
{ 1 , 1 , 1 , 1 },
{ 1 , 0 , 1 , 1 }};
System.out.println(Maze(matrix));
}
}
|
C#
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;
}
}
static int Maze( int [,]matrix)
{
Queue<pair> q = new Queue<pair>();
q.Enqueue( new pair(0, 0));
int count = 0;
while (q.Count != 0)
{
pair p = q.Peek();
q.Dequeue();
if (p.first == n - 1 && p.second == m - 1)
count++;
if (p.first + 1 < n &&
matrix[p.first + 1, p.second] == 1)
{
q.Enqueue( new pair(p.first + 1, p.second));
}
if (p.second + 1 < m &&
matrix[p.first, p.second + 1] == 1)
{
q.Enqueue( new pair(p.first, p.second + 1));
}
}
return count;
}
public static void Main(String[] args)
{
int [,]matrix = {{ 1, 0, 0, 1 },
{ 1, 1, 1, 1 },
{ 1, 0, 1, 1 }};
Console.WriteLine(Maze(matrix));
}
}
|
Javascript
<script>
var m = 4;
var n = 3;
function Maze(matrix)
{
var q = [];
q.push([0, 0]);
var count = 0;
while (q.length != 0)
{
var p = q[0];
q.shift();
if (p[0] == n - 1 && p[1] == m - 1)
count++;
if (p[0] + 1 < n &&
matrix[p[0] + 1][p[1]] == 1)
{
q.push([p[0] + 1, p[1]]);
}
if (p[1] + 1 < m &&
matrix[p[0]][p[1] + 1] == 1)
{
q.push([p[0], p[1] + 1]);
}
}
return count;
}
var matrix = [ [ 1, 0, 0, 1 ],
[ 1, 1, 1, 1 ],
[ 1, 0, 1, 1 ] ];
document.write( Maze(matrix));
</script>
|
Python3
from collections import deque
m = 4
n = 3
def Maze(matrix):
q = deque()
q.append(( 0 , 0 ))
count = 0
while ( len (q) > 0 ):
p = q.popleft()
if (p[ 0 ] = = n - 1 and p[ 1 ] = = m - 1 ):
count + = 1
if (p[ 0 ] + 1 < n and
matrix[p[ 0 ] + 1 ][p[ 1 ]] = = 1 ):
q.append((p[ 0 ] + 1 , p[ 1 ]))
if (p[ 1 ] + 1 < m and
matrix[p[ 0 ]][p[ 1 ] + 1 ] = = 1 ):
q.append((p[ 0 ], p[ 1 ] + 1 ))
return count
matrix = [ [ 1 , 0 , 0 , 1 ],
[ 1 , 1 , 1 , 1 ],
[ 1 , 0 , 1 , 1 ] ]
print (Maze(matrix))
|
Time Complexity: O((N+M-2)C(N-1)), where N is the number of rows and M is the number of columns.
This is because in the worst case, all the cells in the maze will be unblocked, so we need to find the number of ways to reach cell (N-1, M-1) by moving down or right, which will be equal to (N+M-2)C(N-1).
Auxiliary Space: O(N * M).
Share your thoughts in the comments
Please Login to comment...