Count number of ways to reach destination in a Maze
Given a maze[][] of dimension N X M, such that maze[i][j] = -1 represents a blocked cell and maze[i][j] = 0 represents an unblocked cell. The task is to count the number of ways to reach bottom-right cell starting from top-left cell by moving right (i, j+1) and down (i+1, j) in the maze.
Note: You can only pass through the unblocked cells.
Examples:Â
Input: maze = {{0, 0, 0, 0},
{0, -1, 0, 0},
{-1, 0, 0, 0},
{0, 0, 0, 0}};
Output: 4
Explanation: There are four possible paths as shown in below diagram.
Recursion:
When considering the cell (0,0) as the starting point, there is actually 1 way to reach it, which is by not making any move at all. This understanding helps us establish the base case for our recursive solution.
By observing the recursion tree, we can notice that there is repetition in the function calls. This is a common occurrence when multiple recursive calls are made.
We can have confidence in the recursive function’s ability to provide the number of unique paths from an intermediate point to the destination.
Since we can only move downward or to the right, we pass these options to the recursive function, trusting that it will determine the answer by traversing from these points to the destination.
By summing up the number of unique ways from the downward and rightward paths, we obtain the total number of unique ways.
C++
#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1e9 + 7;
long long int
helper( long long int m, long long int n,
vector<vector< long long int > >& obstacleGrid)
{
if (m < 0 || n < 0) {
return 0;
}
if (obstacleGrid[m][n] == -1) {
return 0;
}
if (m == 0 && n == 0) {
return 1;
}
return (helper(m - 1, n, obstacleGrid) % MOD
+ helper(m, n - 1, obstacleGrid) % MOD)
% MOD;
}
long long int uniquePathsWithObstacles(
vector<vector< long long int > >& obstacleGrid)
{
long long int m = obstacleGrid.size();
long long int n = obstacleGrid[0].size();
return helper(m - 1, n - 1, obstacleGrid);
}
int main()
{
vector<vector< long long int > > grid
= { { 0, 0, 0, 0 },
{ 0, -1, 0, 0 },
{ -1, 0, 0, 0 },
{ 0, 0, 0, 0 } };
cout << uniquePathsWithObstacles(grid);
return 0;
}
|
Java
import java.util.*;
public class UniquePathsObstacles {
static long MOD = 1000000007 ;
static long helper( int m, int n, int [][] obstacleGrid) {
if (m < 0 || n < 0 ) {
return 0 ;
}
if (obstacleGrid[m][n] == - 1 ) {
return 0 ;
}
if (m == 0 && n == 0 ) {
return 1 ;
}
return (helper(m - 1 , n, obstacleGrid) % MOD
+ helper(m, n - 1 , obstacleGrid) % MOD)
% MOD;
}
static long uniquePathsWithObstacles( int [][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[ 0 ].length;
return helper(m - 1 , n - 1 , obstacleGrid);
}
public static void main(String[] args) {
int [][] grid = { { 0 , 0 , 0 , 0 },
{ 0 , - 1 , 0 , 0 },
{ - 1 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 0 } };
System.out.println(uniquePathsWithObstacles(grid));
}
}
|
Python3
MOD = 10 * * 9 + 7
def helper(m, n, obstacleGrid):
if m < 0 or n < 0 :
return 0
if obstacleGrid[m][n] = = - 1 :
return 0
if m = = 0 and n = = 0 :
return 1
return (helper(m - 1 , n, obstacleGrid) % MOD
+ helper(m, n - 1 , obstacleGrid) % MOD) % MOD
def uniquePathsWithObstacles(obstacleGrid):
m = len (obstacleGrid)
n = len (obstacleGrid[ 0 ])
return helper(m - 1 , n - 1 , obstacleGrid)
if __name__ = = "__main__" :
grid = [
[ 0 , 0 , 0 , 0 ],
[ 0 , - 1 , 0 , 0 ],
[ - 1 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 ]
]
print (uniquePathsWithObstacles(grid))
|
C#
using System;
class Program
{
static long MOD = 1000000007;
static long Helper( int m, int n, long [][] obstacleGrid)
{
if (m < 0 || n < 0)
return 0;
if (obstacleGrid[m][n] == -1)
return 0;
if (m == 0 && n == 0)
return 1;
return (Helper(m - 1, n, obstacleGrid) % MOD
+ Helper(m, n - 1, obstacleGrid) % MOD)
% MOD;
}
static long UniquePathsWithObstacles( long [][] obstacleGrid)
{
int m = obstacleGrid.Length;
int n = obstacleGrid[0].Length;
return Helper(m - 1, n - 1, obstacleGrid);
}
static void Main()
{
long [][] grid =
{
new long [] {0, 0, 0, 0},
new long [] {0, -1, 0, 0},
new long [] {-1, 0, 0, 0},
new long [] {0, 0, 0, 0}
};
Console.WriteLine(UniquePathsWithObstacles(grid));
}
}
|
Javascript
const MOD = 1000000007;
function helper(m, n, obstacleGrid) {
if (m < 0 || n < 0) {
return 0;
}
if (obstacleGrid[m][n] === -1) {
return 0;
}
if (m === 0 && n === 0) {
return 1;
}
return (helper(m - 1, n, obstacleGrid) % MOD + helper(m, n - 1, obstacleGrid) % MOD) % MOD;
}
function uniquePathsWithObstacles(obstacleGrid) {
const m = obstacleGrid.length;
const n = obstacleGrid[0].length;
return helper(m - 1, n - 1, obstacleGrid);
}
const grid = [
[0, 0, 0, 0],
[0, -1, 0, 0],
[-1, 0, 0, 0],
[0, 0, 0, 0],
];
console.log(uniquePathsWithObstacles(grid));
|
Time Complexity:O(2 n * m)
Space Complexity:O(n*m)
Memoization:
We can cache the overlapping subproblems to make it a efficient solution
C++
#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1e9 + 7;
long long int
helper( long long int m, long long int n,
vector<vector< long long int > >& dp,
vector<vector< long long int > >& obstacleGrid)
{
if (m < 0 || n < 0 || obstacleGrid[m][n] == -1) {
return 0;
}
if (m == 0 && n == 0) {
return 1;
}
if (dp[m][n] != -1) {
return dp[m][n];
}
dp[m][n] = (helper(m - 1, n, dp, obstacleGrid) % MOD
+ helper(m, n - 1, dp, obstacleGrid) % MOD)
% MOD;
return dp[m][n];
}
long long int uniquePathsWithObstacles(
vector<vector< long long int > >& obstacleGrid)
{
long long int m = obstacleGrid.size();
long long int n = obstacleGrid[0].size();
vector<vector< long long int > > dp(
m, vector< long long int >(n, -1));
return helper(m - 1, n - 1, dp, obstacleGrid);
}
int main()
{
vector<vector< long long int > > grid
= { { 0, 0, 0, 0 },
{ 0, -1, 0, 0 },
{ -1, 0, 0, 0 },
{ 0, 0, 0, 0 } };
cout << uniquePathsWithObstacles(grid);
return 0;
}
|
Java
import java.util.Arrays;
public class UniquePaths {
static long MOD = 1000000007 ;
static long helper( int m, int n, long [][] dp, int [][] obstacleGrid) {
if (m < 0 || n < 0 || obstacleGrid[m][n] == 1 ) {
return 0 ;
}
if (m == 0 && n == 0 ) {
return 1 ;
}
if (dp[m][n] != - 1 ) {
return dp[m][n];
}
dp[m][n] = (helper(m - 1 , n, dp, obstacleGrid) % MOD
+ helper(m, n - 1 , dp, obstacleGrid) % MOD) % MOD;
return dp[m][n];
}
static long uniquePathsWithObstacles( int [][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[ 0 ].length;
long [][] dp = new long [m][n];
for ( long [] row : dp) {
Arrays.fill(row, - 1 );
}
return helper(m - 1 , n - 1 , dp, obstacleGrid);
}
public static void main(String[] args) {
int [][] grid = {
{ 0 , 0 , 0 , 0 },
{ 0 , 1 , 0 , 0 },
{ 1 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 0 }
};
System.out.println(uniquePathsWithObstacles(grid));
}
}
|
Python3
MOD = 10 * * 9 + 7
def helper(m, n, dp, obstacleGrid):
if m < 0 or n < 0 or obstacleGrid[m][n] = = - 1 :
return 0
if m = = 0 and n = = 0 :
return 1
if dp[m][n] ! = - 1 :
return dp[m][n]
dp[m][n] = (helper(m - 1 , n, dp, obstacleGrid) % MOD +
helper(m, n - 1 , dp, obstacleGrid) % MOD) % MOD
return dp[m][n]
def uniquePathsWithObstacles(obstacleGrid):
m = len (obstacleGrid)
n = len (obstacleGrid[ 0 ])
dp = [[ - 1 ] * n for _ in range (m)]
return helper(m - 1 , n - 1 , dp, obstacleGrid)
if __name__ = = "__main__" :
grid = [
[ 0 , 0 , 0 , 0 ],
[ 0 , - 1 , 0 , 0 ],
[ - 1 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 ]
]
print (uniquePathsWithObstacles(grid))
|
C#
using System;
using System.Collections.Generic;
class Program {
const long MOD = 1000000007;
static long Helper( int m, int n, long [][] dp,
int [][] obstacleGrid)
{
if (m < 0 || n < 0 || obstacleGrid[m][n] == -1) {
return 0;
}
if (m == 0 && n == 0) {
return 1;
}
if (dp[m][n] != -1) {
return dp[m][n];
}
dp[m][n]
= (Helper(m - 1, n, dp, obstacleGrid) % MOD
+ Helper(m, n - 1, dp, obstacleGrid) % MOD)
% MOD;
return dp[m][n];
}
static long
UniquePathsWithObstacles( int [][] obstacleGrid)
{
int m = obstacleGrid.Length;
int n = obstacleGrid[0].Length;
long [][] dp = new long [m][];
for ( int i = 0; i < m; i++) {
dp[i] = new long [n];
for ( int j = 0; j < n; j++) {
dp[i][j] = -1;
}
}
return Helper(m - 1, n - 1, dp, obstacleGrid);
}
static void Main()
{
int [][] grid = { new int [] { 0, 0, 0, 0 },
new int [] { 0, -1, 0, 0 },
new int [] { -1, 0, 0, 0 },
new int [] { 0, 0, 0, 0 } };
Console.WriteLine(UniquePathsWithObstacles(grid));
}
}
|
Javascript
function helper(m, n, dp, obstacleGrid) {
if (m < 0 || n < 0 || obstacleGrid[m][n] === 1) {
return 0;
}
if (m === 0 && n === 0) {
return 1;
}
if (dp[m][n] !== -1) {
return dp[m][n];
}
dp[m][n] = (helper(m - 1, n, dp, obstacleGrid) % MOD
+ helper(m, n - 1, dp, obstacleGrid) % MOD) % MOD;
return dp[m][n];
}
function GFG(obstacleGrid) {
const m = obstacleGrid.length;
const n = obstacleGrid[0].length;
const dp = new Array(m);
for (let i = 0; i < m; i++) {
dp[i] = new Array(n).fill(-1);
}
return helper(m - 1, n - 1, dp, obstacleGrid);
}
const grid = [
[0, 0, 0, 0],
[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 0]
];
const MOD = 1000000007;
console.log(GFG(grid));
|
Time Complexity:O(n*m)Â
Space Complexity:O(n*m)
Tabulation:
Intuition:
- We declare a 2-D matrix of size matrix[n+1][m+1]
- We fill the matrix with -1 where the blocked cells we given.
- Then we traverse through the matrix and put the sum of matrix[i-1][j] and matrix[i][j-1] if there doesn’t exist a -1.
- Atlast we return the matrix[n][m] as our ans
Implementation:
C++
#include <iostream>
#include <vector>
using namespace std;
int FindWays( int n, int m, vector<vector< int >>& blockedCell) {
int mod = 1000000007;
vector<vector< int >> matrix(n + 1, vector< int >(m + 1, 0));
for ( int i = 0; i < blockedCell.size(); i++) {
matrix[blockedCell[i][0]][blockedCell[i][1]] = -1;
}
for ( int i = 0; i <= n; i++) {
if (matrix[i][1] != -1)
matrix[i][1] = 1;
}
for ( int j = 0; j <= m; j++) {
if (matrix[1][j] != -1)
matrix[1][j] = 1;
}
for ( int i = 2; i <= n; i++) {
for ( int j = 2; j <= m; j++) {
if (matrix[i][j] == -1) {
continue ;
}
if (matrix[i - 1][j] != -1 && matrix[i][j - 1] != -1) {
matrix[i][j] = (matrix[i - 1][j] + matrix[i][j - 1]) % mod;
} else if (matrix[i - 1][j] != -1) {
matrix[i][j] = matrix[i - 1][j];
} else if (matrix[i][j - 1] != -1) {
matrix[i][j] = matrix[i][j - 1];
} else {
matrix[i][j] = -1;
}
}
}
if (matrix[n][m] < 0) {
return 0;
} else {
return matrix[n][m];
}
}
int main() {
int n = 3, m = 3;
vector<vector< int >> blocked_cells = { { 1, 2 }, { 3, 2 } };
cout << FindWays(n, m, blocked_cells) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int FindWays( int n, int m,
int [][] blockedCell)
{
int mod = 1000000007 ;
int matrix[][] = new int [n + 1 ][m + 1 ];
for ( int i = 0 ; i < blockedCell.length; i++) {
matrix[blockedCell[i][ 0 ]][blockedCell[i][ 1 ]]
= - 1 ;
}
for ( int i = 0 ; i <= n; i++) {
if (matrix[i][ 1 ] != - 1 )
matrix[i][ 1 ] = 1 ;
}
for ( int j = 0 ; j <= m; j++) {
if (matrix[ 1 ][j] != - 1 )
matrix[ 0 ][ 1 ] = 1 ;
}
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= m; j++) {
if (matrix[i][j] == - 1 ) {
continue ;
}
if (matrix[i - 1 ][j] != - 1
&& matrix[i][j - 1 ] != - 1 ) {
matrix[i][j] = (matrix[i - 1 ][j]
+ matrix[i][j - 1 ])
% mod;
}
else if (matrix[i - 1 ][j] != - 1 ) {
matrix[i][j] = matrix[i - 1 ][j];
}
else if (matrix[i][j - 1 ] != - 1 ) {
matrix[i][j] = matrix[i][j - 1 ];
}
else {
matrix[i][j] = - 1 ;
}
}
}
if (matrix[n][m] < 0 ) {
return 0 ;
}
else {
return matrix[n][m];
}
}
public static void main(String[] args)
{
int n = 3 , m = 3 ;
int [][] blocked_cells = { { 1 , 2 }, { 3 , 2 } };
System.out.println(FindWays(n, m, blocked_cells));
}
}
|
Python3
def find_ways(n, m, blocked_cells):
mod = 1000000007
matrix = [[ 0 ] * (m + 1 ) for _ in range (n + 1 )]
for cell in blocked_cells:
matrix[cell[ 0 ]][cell[ 1 ]] = - 1
for i in range (n + 1 ):
if matrix[i][ 1 ] ! = - 1 :
matrix[i][ 1 ] = 1
for j in range (m + 1 ):
if matrix[ 1 ][j] ! = - 1 :
matrix[ 1 ][j] = 1
for i in range ( 1 , n + 1 ):
for j in range ( 1 , m + 1 ):
if matrix[i][j] = = - 1 :
continue
if matrix[i - 1 ][j] ! = - 1 and matrix[i][j - 1 ] ! = - 1 :
matrix[i][j] = (matrix[i - 1 ][j] + matrix[i][j - 1 ]) % mod
elif matrix[i - 1 ][j] ! = - 1 :
matrix[i][j] = matrix[i - 1 ][j]
elif matrix[i][j - 1 ] ! = - 1 :
matrix[i][j] = matrix[i][j - 1 ]
else :
matrix[i][j] = - 1
if matrix[n][m] < 0 :
return 0
else :
return matrix[n][m]
if __name__ = = "__main__" :
n = 3
m = 3
blocked_cells = [[ 1 , 2 ], [ 3 , 2 ]]
print (find_ways(n, m, blocked_cells))
|
C#
using System;
using System.Collections.Generic;
class Program {
static int FindWays( int n, int m,
List<List< int > > blockedCell)
{
int mod = 1000000007;
int [, ] matrix = new int [n + 1, m + 1];
foreach ( var cell in blockedCell)
{
matrix[cell[0], cell[1]] = -1;
}
for ( int i = 0; i <= n; i++) {
if (matrix[i, 1] != -1)
matrix[i, 1] = 1;
}
for ( int j = 0; j <= m; j++) {
if (matrix[1, j] != -1)
matrix[1, j] = 1;
}
for ( int i = 2; i <= n; i++) {
for ( int j = 2; j <= m; j++) {
if (matrix[i, j] == -1) {
continue ;
}
if (matrix[i - 1, j] != -1
&& matrix[i, j - 1] != -1) {
matrix[i, j] = (matrix[i - 1, j]
+ matrix[i, j - 1])
% mod;
}
else if (matrix[i - 1, j] != -1) {
matrix[i, j] = matrix[i - 1, j];
}
else if (matrix[i, j - 1] != -1) {
matrix[i, j] = matrix[i, j - 1];
}
else {
matrix[i, j] = -1;
}
}
}
if (matrix[n, m] < 0) {
return 0;
}
else {
return matrix[n, m];
}
}
static void Main()
{
int n = 3, m = 3;
List<List< int > > blocked_cells
= new List<List< int > >{ new List< int >{ 1, 2 },
new List< int >{ 3, 2 } };
Console.WriteLine(FindWays(n, m, blocked_cells));
}
}
|
Javascript
function findWays(n, m, blockedCell) {
const mod = 1000000007;
const matrix = new Array(n + 1).fill(0).map(() => new Array(m + 1).fill(0));
blockedCell.forEach(cell => {
matrix[cell[0]][cell[1]] = -1;
});
for (let i = 0; i <= n; i++) {
if (matrix[i][1] !== -1) {
matrix[i][1] = 1;
}
}
for (let j = 0; j <= m; j++) {
if (matrix[1][j] !== -1) {
matrix[1][j] = 1;
}
}
for (let i = 2; i <= n; i++) {
for (let j = 2; j <= m; j++) {
if (matrix[i][j] === -1) {
continue ;
}
if (matrix[i - 1][j] !== -1 && matrix[i][j - 1] !== -1) {
matrix[i][j] = (matrix[i - 1][j] + matrix[i][j - 1]) % mod;
} else if (matrix[i - 1][j] !== -1) {
matrix[i][j] = matrix[i - 1][j];
} else if (matrix[i][j - 1] !== -1) {
matrix[i][j] = matrix[i][j - 1];
} else {
matrix[i][j] = -1;
}
}
}
if (matrix[n][m] < 0) {
return 0;
} else {
return matrix[n][m];
}
}
const n = 3, m = 3;
const blockedCells = [
[1, 2],
[3, 2]
];
console.log(findWays(n, m, blockedCells));
|
Time Complexity: O(N*M)
Space Complexity: O(N*M) Since we are using a 2-D matrix
This problem is an extension of the below problem.
Backtracking | Set 2 (Rat in a Maze)
In this post, a different solution is discussed that can be used to solve the above Rat in a Maze problem also.
The idea is to modify the given grid[][] so that grid[i][j] contains count of paths to reach (i, j) from (0, 0) if (i, j) is not a blockage, else grid[i][j] remains -1.Â
We can recursively compute grid[i][j] using below
formula and finally return grid[R-1][C-1]
// If current cell is a blockage
if (maze[i][j] == -1)
maze[i][j] = -1; // Do not change
// If we can reach maze[i][j] from maze[i-1][j]
// then increment count.
else if (maze[i-1][j] > 0)
maze[i][j] = (maze[i][j] + maze[i-1][j]);
// If we can reach maze[i][j] from maze[i][j-1]
// then increment count.
else if (maze[i][j-1] > 0)
maze[i][j] = (maze[i][j] + maze[i][j-1]);
Below is the implementation of the above idea.Â
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 main()
{
int maze[R][C] = {{0, 0, 0, 0},
{0, -1, 0, 0},
{-1, 0, 0, 0},
{0, 0, 0, 0}};
cout << countPaths(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 ;
}
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 (countPaths(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, 1 ):
if (maze[ 0 ][i] = = 0 ):
maze[ 0 ][i] = 1
else :
break
for i in range ( 1 , R, 1 ):
for j in range ( 1 , C, 1 ):
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
if __name__ = = '__main__' :
maze = [[ 0 , 0 , 0 , 0 ],
[ 0 , - 1 , 0 , 0 ],
[ - 1 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 ]]
print (countPaths(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;
}
public static void Main ()
{
int [,]maze = { {0, 0, 0, 0},
{0, -1, 0, 0},
{-1, 0, 0, 0},
{0, 0, 0, 0}};
Console.Write (countPaths(maze));
}
}
|
Javascript
<script>
let R = 4;
let C = 4;
function countPaths(maze)
{
if (maze[0][0] == -1)
return 0;
for (let i = 0; i < R; i++)
{
if (maze[i][0] == 0)
maze[i][0] = 1;
else
break ;
}
for (let i = 1; i < C; i++)
{
if (maze[0][i] == 0)
maze[0][i] = 1;
else
break ;
}
for (let i = 1; i < R; i++)
{
for (let 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;
}
let maze = [ [ 0, 0, 0, 0 ],
[ 0, -1, 0, 0 ],
[ -1, 0, 0, 0 ],
[ 0, 0, 0, 0 ] ];
document.write(countPaths(maze));
</script>
|
PHP
<?php
$R = 4;
$C = 4;
function countPaths( $maze )
{
global $R , $C ;
if ( $maze [0][0] == - 1)
return 0;
for ( $i = 0; $i < $R ; $i ++)
{
if ( $maze [ $i ][0] == 0)
$maze [ $i ][0] = 1;
else
break ;
}
for ( $i = 1; $i < $C ; $i ++)
{
if ( $maze [0][ $i ] == 0)
$maze [0][ $i ] = 1;
else
break ;
}
for ( $i = 1; $i < $R ; $i ++)
{
for ( $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;
}
$maze = array ( array (0, 0, 0, 0),
array (0, -1, 0, 0),
array (-1, 0, 0, 0),
array (0, 0, 0, 0));
echo countPaths( $maze );
?>
|
Time Complexity: O(R x C)
Auxiliary Space: O(1)
This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Â
Last Updated :
07 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...