Given a maze of 0 and -1 cells, the task is to find all the paths from (0, 0) to (n-1, m-1), and every path should pass through at least one cell which contains -1. From a given cell, we are allowed to move to cells (i+1, j) and (i, j+1) only.
This problem is a variation of the problem published here.
Examples:
Input: maze[][] = {
{0, 0, 0, 0},
{0, -1, 0, 0},
{-1, 0, 0, 0},
{0, 0, 0, 0}}
Output: 16
Approach: To find all the paths which go through at least one marked cell (cell containing -1). If we find the paths that do not go through any of the marked cells and all the possible paths from (0, 0) to (n-1, m-1) then we can find all the paths that go through at least one of the marks cells.
Number of paths that pass through at least one marked cell = (Total number of paths – Number of paths that do not pass through any marked cell)
We will use the approach mentioned in this article to find the total number of paths that do not pass through any marked cell and the total number of paths from source to destination will be (m + n – 2)! / (n – 1)! * (m – 1)! where m and n are the number of rows and columns.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
int countPaths( int maze[][C])
{
if (maze[0][0] == -1)
return 0;
for ( int i = 0; i < R; i++) {
if (maze[i][0] == 0)
maze[i][0] = 1;
else
break ;
}
for ( int i = 1; i < C; i++) {
if (maze[0][i] == 0)
maze[0][i] = 1;
else
break ;
}
for ( int i = 1; i < R; i++) {
for ( int j = 1; j < C; j++) {
if (maze[i][j] == -1)
continue ;
if (maze[i - 1][j] > 0)
maze[i][j] = (maze[i][j] + maze[i - 1][j]);
if (maze[i][j - 1] > 0)
maze[i][j] = (maze[i][j] + maze[i][j - 1]);
}
}
return (maze[R - 1][C - 1] > 0) ? maze[R - 1][C - 1] : 0;
}
int numberOfPaths( int m, int n)
{
int path = 1;
for ( int i = n; i < (m + n - 1); i++) {
path *= i;
path /= (i - n + 1);
}
return path;
}
int solve( int maze[][C])
{
int ans = numberOfPaths(R, C) - countPaths(maze);
return ans;
}
int main()
{
int maze[R][C] = { { 0, 0, 0, 0 },
{ 0, -1, 0, 0 },
{ -1, 0, 0, 0 },
{ 0, 0, 0, 0 } };
cout << solve(maze);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int R = 4 ;
static int C = 4 ;
static int countPaths( int maze[][])
{
if (maze[ 0 ][ 0 ] == - 1 )
return 0 ;
for ( int i = 0 ; i < R; i++)
{
if (maze[i][ 0 ] == 0 )
maze[i][ 0 ] = 1 ;
else
break ;
}
for ( int i = 1 ; i < C; i++)
{
if (maze[ 0 ][i] == 0 )
maze[ 0 ][i] = 1 ;
else
break ;
}
for ( int i = 1 ; i < R; i++)
{
for ( int j = 1 ; j < C; j++)
{
if (maze[i][j] == - 1 )
continue ;
if (maze[i - 1 ][j] > 0 )
maze[i][j] = (maze[i][j] +
maze[i - 1 ][j]);
if (maze[i][j - 1 ] > 0 )
maze[i][j] = (maze[i][j] +
maze[i][j - 1 ]);
}
}
return (maze[R - 1 ][C - 1 ] > 0 ) ?
maze[R - 1 ][C - 1 ] : 0 ;
}
static int numberOfPaths( int m, int n)
{
int path = 1 ;
for ( int i = n; i < (m + n - 1 ); i++)
{
path *= i;
path /= (i - n + 1 );
}
return path;
}
static int solve( int maze[][])
{
int ans = numberOfPaths(R, C) - countPaths(maze);
return ans;
}
public static void main (String[] args)
{
int maze[][] = { { 0 , 0 , 0 , 0 },
{ 0 , - 1 , 0 , 0 },
{ - 1 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 0 } };
System.out.println(solve(maze));
}
}
|
Python3
R = 4
C = 4
def countPaths(maze):
if (maze[ 0 ][ 0 ] = = - 1 ):
return 0
for i in range (R):
if (maze[i][ 0 ] = = 0 ):
maze[i][ 0 ] = 1
else :
break
for i in range ( 1 , C):
if (maze[ 0 ][i] = = 0 ):
maze[ 0 ][i] = 1
else :
break
for i in range ( 1 , R):
for j in range ( 1 , C):
if (maze[i][j] = = - 1 ):
continue
if (maze[i - 1 ][j] > 0 ):
maze[i][j] = (maze[i][j] +
maze[i - 1 ][j])
if (maze[i][j - 1 ] > 0 ):
maze[i][j] = (maze[i][j] +
maze[i][j - 1 ])
if (maze[R - 1 ][C - 1 ] > 0 ):
return maze[R - 1 ][C - 1 ]
else :
return 0
def numberOfPaths(m, n):
path = 1
for i in range (n, m + n - 1 ):
path * = i
path / / = (i - n + 1 )
return path
def solve(maze):
ans = (numberOfPaths(R, C) -
countPaths(maze))
return ans
maze = [[ 0 , 0 , 0 , 0 ],
[ 0 , - 1 , 0 , 0 ],
[ - 1 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 ]]
print (solve(maze))
|
C#
using System;
class GFG
{
static int R = 4;
static int C = 4;
static int countPaths( int [,]maze)
{
if (maze[0, 0] == -1)
return 0;
for ( int i = 0; i < R; i++)
{
if (maze[i, 0] == 0)
maze[i, 0] = 1;
else
break ;
}
for ( int i = 1; i < C; i++)
{
if (maze[0, i] == 0)
maze[0, i] = 1;
else
break ;
}
for ( int i = 1; i < R; i++)
{
for ( int j = 1; j < C; j++)
{
if (maze[i, j] == -1)
continue ;
if (maze[i - 1, j] > 0)
maze[i, j] = (maze[i, j] +
maze[i - 1, j]);
if (maze[i, j - 1] > 0)
maze[i, j] = (maze[i, j] +
maze[i, j - 1]);
}
}
return (maze[R - 1, C - 1] > 0) ?
maze[R - 1, C - 1] : 0;
}
static int numberOfPaths( int m, int n)
{
int path = 1;
for ( int i = n; i < (m + n - 1); i++)
{
path *= i;
path /= (i - n + 1);
}
return path;
}
static int solve( int [,]maze)
{
int ans = numberOfPaths(R, C) -
countPaths(maze);
return ans;
}
public static void Main ()
{
int [,]maze = {{ 0, 0, 0, 0 },
{ 0, -1, 0, 0 },
{ -1, 0, 0, 0 },
{ 0, 0, 0, 0 }};
Console.Write(solve(maze));
}
}
|
Javascript
<script>
var R = 4
var C = 4
function countPaths(maze)
{
if (maze[0][0] == -1)
return 0;
for ( var i = 0; i < R; i++) {
if (maze[i][0] == 0)
maze[i][0] = 1;
else
break ;
}
for ( var i = 1; i < C; i++) {
if (maze[0][i] == 0)
maze[0][i] = 1;
else
break ;
}
for ( var i = 1; i < R; i++) {
for ( var j = 1; j < C; j++) {
if (maze[i][j] == -1)
continue ;
if (maze[i - 1][j] > 0)
maze[i][j] = (maze[i][j] + maze[i - 1][j]);
if (maze[i][j - 1] > 0)
maze[i][j] = (maze[i][j] + maze[i][j - 1]);
}
}
return (maze[R - 1][C - 1] > 0) ? maze[R - 1][C - 1] : 0;
}
function numberOfPaths(m, n)
{
var path = 1;
for ( var i = n; i < (m + n - 1); i++) {
path *= i;
path /= (i - n + 1);
}
return path;
}
function solve(maze)
{
var ans = numberOfPaths(R, C) - countPaths(maze);
return ans;
}
var maze = [ [ 0, 0, 0, 0 ],
[ 0, -1, 0, 0 ],
[ -1, 0, 0, 0 ],
[ 0, 0, 0, 0 ] ];
document.write( solve(maze));
</script>
|
Time Complexity: O(R*C)
Auxiliary Space: O(R*C)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
04 Aug, 2021
Like Article
Save Article