Given a square maze containing positive numbers, find all paths from a corner cell (any of the extreme four corners) to the middle cell. We can move exactly n steps from a cell in 4 directions i.e. North, East, West and South where n is value of the cell,
We can move to mat[i+n][j], mat[i-n][j], mat[i][j+n], and mat[i][j-n] from a cell mat[i][j] where n is value of mat[i][j].
Example
Input: 9 x 9 maze
[ 3, 5, 4, 4, 7, 3, 4, 6, 3 ]
[ 6, 7, 5, 6, 6, 2, 6, 6, 2 ]
[ 3, 3, 4, 3, 2, 5, 4, 7, 2 ]
[ 6, 5, 5, 1, 2, 3, 6, 5, 6 ]
[ 3, 3, 4, 3, 0, 1, 4, 3, 4 ]
[ 3, 5, 4, 3, 2, 2, 3, 3, 5 ]
[ 3, 5, 4, 3, 2, 6, 4, 4, 3 ]
[ 3, 5, 1, 3, 7, 5, 3, 6, 4 ]
[ 6, 2, 4, 3, 4, 5, 4, 5, 1 ]
Output:
(0, 0) -> (0, 3) -> (0, 7) ->
(6, 7) -> (6, 3) -> (3, 3) ->
(3, 4) -> (5, 4) -> (5, 2) ->
(1, 2) -> (1, 7) -> (7, 7) ->
(7, 1) -> (2, 1) -> (2, 4) ->
(4, 4) -> MID
The idea is to use backtracking. We start with each corner cell of the maze and recursively checks if it leads to the solution or not. Following is the Backtracking algorithm –
If destination is reached
- print the path
Else
- Mark current cell as visited and add it to path array.
- Move forward in all 4 allowed directions and recursively check if any of them leads to a solution.
- If none of the above solutions work then mark this cell as not visited and remove it from path array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 9
bool isValid(set<pair< int , int > > visited,
pair< int , int > pt)
{
return (pt.first >= 0) && (pt.first < N) &&
(pt.second >= 0) && (pt.second < N) &&
(visited.find(pt) == visited.end());
}
void printPath(list<pair< int , int > > path)
{
for ( auto it = path.begin(); it != path.end(); it++)
cout << "(" << it->first << ", "
<< it->second << ") -> " ;
cout << "MID" << endl << endl;
}
int row[] = {-1, 1, 0, 0};
int col[] = { 0, 0, -1, 1};
int _row[] = { 0, 0, N-1, N-1};
int _col[] = { 0, N-1, 0, N-1};
void findPathInMazeUtil( int maze[N][N],
list<pair< int , int > > &path,
set<pair< int , int > > &visited,
pair< int , int > &curr)
{
if (curr.first == N / 2 && curr.second == N / 2)
{
printPath(path);
return ;
}
for ( int i = 0; i < 4; ++i)
{
int n = maze[curr.first][curr.second];
int x = curr.first + row[i]*n;
int y = curr.second + col[i]*n;
pair< int , int > next = make_pair(x, y);
if (isValid(visited, next))
{
visited.insert(next);
path.push_back(next);
findPathInMazeUtil(maze, path, visited, next);
path.pop_back();
visited.erase(next);
}
}
}
void findPathInMaze( int maze[N][N])
{
list<pair< int , int > > path;
set<pair< int , int > > visited;
for ( int i = 0; i < 4; ++i)
{
int x = _row[i];
int y = _col[i];
pair< int , int > pt = make_pair(x, y);
visited.insert(pt);
path.push_back(pt);
findPathInMazeUtil(maze, path, visited, pt);
path.pop_back();
visited.erase(pt);
}
}
int main()
{
int maze[N][N] =
{
{ 3, 5, 4, 4, 7, 3, 4, 6, 3 },
{ 6, 7, 5, 6, 6, 2, 6, 6, 2 },
{ 3, 3, 4, 3, 2, 5, 4, 7, 2 },
{ 6, 5, 5, 1, 2, 3, 6, 5, 6 },
{ 3, 3, 4, 3, 0, 1, 4, 3, 4 },
{ 3, 5, 4, 3, 2, 2, 3, 3, 5 },
{ 3, 5, 4, 3, 2, 6, 4, 4, 3 },
{ 3, 5, 1, 3, 7, 5, 3, 6, 4 },
{ 6, 2, 4, 3, 4, 5, 4, 5, 1 }
};
findPathInMaze(maze);
return 0;
}
|
Python3
N = 9
def isValid(visited, pt):
return (pt[ 0 ] > = 0 ) and (pt[ 0 ] < N) and (pt[ 1 ] > = 0 ) and (pt[ 1 ] < N) and (pt not in visited)
def printPath(path):
for i in path:
print ( "({}, {}) -> " . format (i[ 0 ], i[ 1 ]), end = "")
print ( "MID" )
print ()
row = [ - 1 , 1 , 0 , 0 ]
col = [ 0 , 0 , - 1 , 1 ]
_row = [ 0 , 0 , N - 1 , N - 1 ]
_col = [ 0 , N - 1 , 0 , N - 1 ]
def findPathInMazeUtil(maze, path, visited, curr):
if curr[ 0 ] = = N / / 2 and curr[ 1 ] = = N / / 2 :
printPath(path)
return
for i in range ( 4 ):
n = maze[curr[ 0 ]][curr[ 1 ]]
x = curr[ 0 ] + row[i] * n
y = curr[ 1 ] + col[i] * n
next = (x, y)
if isValid(visited, next ):
visited.append( next )
path.append( next )
findPathInMazeUtil(maze, path, visited, next )
path.pop()
visited.remove( next )
def findPathInMaze(maze):
path = []
visited = []
for i in range ( 4 ):
x = _row[i]
y = _col[i]
pt = (x, y)
visited.append(pt)
path.append(pt)
findPathInMazeUtil(maze, path, visited, pt)
path.pop()
visited.remove(pt)
if __name__ = = "__main__" :
maze = [
[ 3 , 5 , 4 , 4 , 7 , 3 , 4 , 6 , 3 ],
[ 6 , 7 , 5 , 6 , 6 , 2 , 6 , 6 , 2 ],
[ 3 , 3 , 4 , 3 , 2 , 5 , 4 , 7 , 2 ],
[ 6 , 5 , 5 , 1 , 2 , 3 , 6 , 5 , 6 ],
[ 3 , 3 , 4 , 3 , 0 , 1 , 4 , 3 , 4 ],
[ 3 , 5 , 4 , 3 , 2 , 2 , 3 , 3 , 5 ],
[ 3 , 5 , 4 , 3 , 2 , 6 , 4 , 4 , 3 ],
[ 3 , 5 , 1 , 3 , 7 , 5 , 3 , 6 , 4 ],
[ 6 , 2 , 4 , 3 , 4 , 5 , 4 , 5 , 1 ]
]
findPathInMaze(maze)
|
C#
using System;
using System.Collections.Generic;
namespace MazePathFinder
{
class Program
{
const int N = 9;
static bool IsValid(List<( int , int )> visited, ( int , int ) pt)
{
return (pt.Item1 >= 0) && (pt.Item1 < N) && (pt.Item2 >= 0) && (pt.Item2 < N) && (!visited.Contains(pt));
}
static void PrintPath(List<( int , int )> path)
{
foreach ( var i in path)
{
Console.Write($ "({i.Item1}, {i.Item2}) -> " );
}
Console.WriteLine( "MID" );
Console.WriteLine();
}
static int [] row = { -1, 1, 0, 0 };
static int [] col = { 0, 0, -1, 1 };
static int [] _row = { 0, 0, N - 1, N - 1 };
static int [] _col = { 0, N - 1, 0, N - 1 };
static void FindPathInMazeUtil( int [][] maze, List<( int , int )> path, List<( int , int )> visited, ( int , int ) curr)
{
if (curr.Item1 == N / 2 && curr.Item2 == N / 2)
{
PrintPath(path);
return ;
}
for ( int i = 0; i < 4; i++)
{
int n = maze[curr.Item1][curr.Item2];
int x = curr.Item1 + row[i] * n;
int y = curr.Item2 + col[i] * n;
var next = (x, y);
if (IsValid(visited, next))
{
visited.Add(next);
path.Add(next);
FindPathInMazeUtil(maze, path, visited, next);
path.RemoveAt(path.Count - 1);
visited.Remove(next);
}
}
}
static void FindPathInMaze( int [][] maze)
{
var path = new List<( int , int )>();
var visited = new List<( int , int )>();
for ( int i = 0; i < 4; i++)
{
int x = _row[i];
int y = _col[i];
var pt = (x, y);
visited.Add(pt);
path.Add(pt);
FindPathInMazeUtil(maze, path, visited, pt);
path.RemoveAt(path.Count - 1);
visited.Remove(pt);
}
}
static void Main( string [] args)
{
int [][] maze = new int [][] {
new int [] {3, 5, 4, 4, 7, 3, 4, 6, 3},
new int [] {6, 7, 5, 6, 6, 2, 6, 6, 2},
new int [] {3, 3, 4, 3, 2, 5, 4, 7, 2},
new int [] {6, 5, 5, 1, 2, 3, 6, 5, 6},
new int [] {3, 3, 4, 3, 0, 1, 4, 3, 4},
new int [] {3, 5, 4, 3, 2, 2, 3, 3, 5},
new int [] {3, 5, 4, 3, 2, 6, 4, 4, 3},
new int [] {3, 5, 1, 3, 7, 5, 3, 6, 4},
new int [] {6, 2, 4, 3, 4, 5, 4, 5, 1}
};
FindPathInMaze(maze);
}
}
}
|
Javascript
let N = 9;
function isValid(visited, pt) {
return (pt[0] >= 0) && (pt[0] < N) && (pt[1] >= 0) && (pt[1] < N) &&
(!visited.some(item => item[0] === pt[0] && item[1] === pt[1]));
}
function printPath(path) {
let pathStr = "" ;
for (let i of path) {
pathStr += "(" + i[0] + ", " + i[1] + ") -> " ;
}
pathStr += "MID" ;
console.log(pathStr);
console.log();
}
const row = [-1, 1, 0, 0];
const col = [0, 0, -1, 1];
const _row = [0, 0, N-1, N-1];
const _col = [0, N-1, 0, N-1];
function findPathInMazeUtil(maze, path, visited, curr) {
if (curr[0] === Math.floor(N / 2) && curr[1] === Math.floor(N / 2)) {
printPath(path);
return ;
}
for (let i = 0; i < 4; i++) {
let n = maze[curr[0]][curr[1]];
let x = curr[0] + row[i]*n;
let y = curr[1] + col[i]*n;
let next = [x, y];
if (isValid(visited, next)) {
visited.push(next);
path.push(next);
findPathInMazeUtil(maze, path, visited, next);
path.pop();
visited = visited.filter(item => item[0] !== next[0] || item[1] !== next[1]);
}
}
}
function findPathInMaze(maze) {
let path = [];
let visited = [];
for (let i = 0; i < 4; i++) {
let x = _row[i];
let y = _col[i];
let pt = [x, y];
visited.push(pt);
path.push(pt);
findPathInMazeUtil(maze, path, visited, pt);
path.pop();
visited = visited.filter(item => item[0] !== pt[0] || item[1] !== pt[1]);
}
}
let maze = [
[3, 5, 4, 4, 7, 3, 4, 6, 3],
[6, 7, 5, 6, 6, 2, 6, 6, 2],
[3, 3, 4, 3, 2, 5, 4, 7, 2],
[6, 5, 5, 1, 2, 3, 6, 5, 6],
[3, 3, 4, 3, 0, 1, 4, 3, 4],
[3, 5, 4, 3, 2, 2, 3, 3, 5],
[3, 5, 4, 3, 2, 6, 4, 4, 3],
[3, 5, 1, 3, 7, 5, 3, 6, 4],
[6, 2, 4, 3, 4, 5, 4, 5, 1]
]
findPathInMaze(maze)
|
Java
import java.util.*;
public class MazePathFinder {
private static int N = 9 ;
private static int [] row = { - 1 , 1 , 0 , 0 };
private static int [] col = { 0 , 0 , - 1 , 1 };
private static int [] _row = { 0 , 0 , N - 1 , N - 1 };
private static int [] _col = { 0 , N - 1 , 0 , N - 1 };
private static boolean isValid(ArrayList< int []> visited,
int [] pt)
{
return (pt[ 0 ] >= 0 ) && (pt[ 0 ] < N) && (pt[ 1 ] >= 0 )
&& (pt[ 1 ] < N)
&& (!visited.stream().anyMatch(
item
-> item[ 0 ] == pt[ 0 ] && item[ 1 ] == pt[ 1 ]));
}
private static void printPath(ArrayList< int []> path)
{
String pathStr = "" ;
for ( int [] i : path) {
pathStr += "(" + i[ 0 ] + ", " + i[ 1 ] + ") -> " ;
}
pathStr += "MID" ;
System.out.println(pathStr);
System.out.println();
}
private static void
findPathInMazeUtil( int [][] maze, ArrayList< int []> path,
ArrayList< int []> visited, int [] curr)
{
if (curr[ 0 ] == (N / 2 ) && curr[ 1 ] == (N / 2 )) {
printPath(path);
return ;
}
for ( int i = 0 ; i < 4 ; i++) {
int n = maze[curr[ 0 ]][curr[ 1 ]];
int x = curr[ 0 ] + row[i] * n;
int y = curr[ 1 ] + col[i] * n;
int [] next = { x, y };
if (isValid(visited, next)) {
visited.add(next);
path.add(next);
findPathInMazeUtil(maze, path, visited,
next);
path.remove(path.size() - 1 );
visited.removeIf(item
-> item[ 0 ] == next[ 0 ]
&& item[ 1 ]
== next[ 1 ]);
}
}
}
public static void findPathInMaze( int [][] maze)
{
ArrayList< int []> path = new ArrayList<>();
ArrayList< int []> visited = new ArrayList<>();
for ( int i = 0 ; i < 4 ; i++) {
int x = _row[i];
int y = _col[i];
int [] pt = { x, y };
visited.add(pt);
path.add(pt);
findPathInMazeUtil(maze, path, visited, pt);
path.remove(path.size() - 1 );
visited.removeIf(item
-> item[ 0 ] == pt[ 0 ]
&& item[ 1 ] == pt[ 1 ]);
}
}
public static void main(String[] args)
{
int [][] maze = { { 3 , 5 , 4 , 4 , 7 , 3 , 4 , 6 , 3 },
{ 6 , 7 , 5 , 6 , 6 , 2 , 6 , 6 , 2 },
{ 3 , 3 , 4 , 3 , 2 , 5 , 4 , 7 , 2 },
{ 6 , 5 , 5 , 1 , 2 , 3 , 6 , 5 , 6 },
{ 3 , 3 , 4 , 3 , 0 , 1 , 4 , 3 , 4 },
{ 3 , 5 , 4 , 3 , 2 , 2 , 3 , 3 , 5 },
{ 3 , 5 , 4 , 3 , 2 , 6 , 4 , 4 , 3 },
{ 3 , 5 , 1 , 3 , 7 , 5 , 3 , 6 , 4 },
{ 6 , 2 , 4 , 3 , 4 , 5 , 4 , 5 , 1 } };
findPathInMaze(maze);
}
}
|
Output
(0, 0) -> (0, 3) -> (0, 7) -> (6, 7) -> (6, 3) -> (3, 3) -> (3, 4) -> (5, 4) -> (5, 2) -> (1, 2) -> (1, 7) -> (7, 7) -> (7, 1) -> (2, 1) -> (2, 4) -> (4, 4) -> MID
Output :
(0, 0) -> (0, 3) -> (0, 7) ->
(6, 7) -> (6, 3) -> (3, 3) ->
(3, 4) -> (5, 4) -> (5, 2) ->
(1, 2) -> (1, 7) -> (7, 7) ->
(7, 1) -> (2, 1) -> (2, 4) ->
(4, 4) -> MID
Time Complexity: O(4^N2), where N is the size of the maze and the program may need to explore all possible paths in the maze before finding the path from the corner to the middle cell.
Space Complexity: O(N2), where N*N is the size of the maze.
A better approach:
C++
#include<bits/stdc++.h>
using namespace std;
void printPath(vector<vector< int >>&maze, int i, int j, string ans){
if (i == maze.size()/2 && j==maze.size()/2){
ans += "(" ;
ans += to_string(i);
ans += ", " ;
ans += to_string(j);
ans += ") -> MID" ;
cout<<ans<<endl;
return ;
}
if (maze[i][j]==0){
return ;
}
int k = maze[i][j];
maze[i][j]=0;
ans += "(" ;
ans += to_string(i);
ans += ", " ;
ans += to_string(j);
ans += ") -> " ;
if (j+k<maze.size()){
printPath(maze, i, j+k, ans);
}
if (i+k<maze.size()){
printPath(maze, i+k, j,ans);
}
if (j-k>0){
printPath(maze, i, j-k, ans);
}
if (i-k>0){
printPath(maze, i-k, j, ans);
}
maze[i][j] = k;
}
int main () {
vector<vector< int >>maze = {
{ 3, 5, 4, 4, 7, 3, 4, 6, 3 },
{ 6, 7, 5, 6, 6, 2, 6, 6, 2 },
{ 3, 3, 4, 3, 2, 5, 4, 7, 2 },
{ 6, 5, 5, 1, 2, 3, 6, 5, 6 },
{ 3, 3, 4, 3, 0, 1, 4, 3, 4 },
{ 3, 5, 4, 3, 2, 2, 3, 3, 5 },
{ 3, 5, 4, 3, 2, 6, 4, 4, 3 },
{ 3, 5, 1, 3, 7, 5, 3, 6, 4 },
{ 6, 2, 4, 3, 4, 5, 4, 5, 1 }
};
printPath(maze,0,0, "" );
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int [][] maze = {
{ 3 , 5 , 4 , 4 , 7 , 3 , 4 , 6 , 3 },
{ 6 , 7 , 5 , 6 , 6 , 2 , 6 , 6 , 2 },
{ 3 , 3 , 4 , 3 , 2 , 5 , 4 , 7 , 2 },
{ 6 , 5 , 5 , 1 , 2 , 3 , 6 , 5 , 6 },
{ 3 , 3 , 4 , 3 , 0 , 1 , 4 , 3 , 4 },
{ 3 , 5 , 4 , 3 , 2 , 2 , 3 , 3 , 5 },
{ 3 , 5 , 4 , 3 , 2 , 6 , 4 , 4 , 3 },
{ 3 , 5 , 1 , 3 , 7 , 5 , 3 , 6 , 4 },
{ 6 , 2 , 4 , 3 , 4 , 5 , 4 , 5 , 1 }
};
printPath(maze, 0 , 0 , "" );
}
public static void printPath( int [][] maze, int i, int j, String ans){
if (i == maze.length/ 2 && j==maze.length/ 2 ){
ans += "(" +i+ ", " +j+ ") -> MID" ;
System.out.println(ans);
return ;
}
if (maze[i][j]== 0 ){
return ;
}
int k = maze[i][j];
maze[i][j]= 0 ;
if (j+k<maze.length){
printPath(maze, i, j+k, ans+ "(" +i+ ", " +j+ ") -> " );
}
if (i+k<maze.length){
printPath(maze, i+k, j, ans+ "(" +i+ ", " +j+ ") -> " );
}
if (j-k> 0 ){
printPath(maze, i, j-k, ans+ "(" +i+ ", " +j+ ") -> " );
}
if (i-k> 0 ){
printPath(maze, i-k, j, ans+ "(" +i+ ", " +j+ ") -> " );
}
maze[i][j] = k;
}
}
|
Python3
def printPath(maze, i, j, ans):
if (i = = len (maze) / / 2 and j = = len (maze) / / 2 ):
ans + = "(" + str (i) + ", " + str (j) + ") -> MID" ;
print (ans);
return ;
if (maze[i][j] = = 0 ):
return ;
k = maze[i][j];
maze[i][j] = 0 ;
if (j + k < len (maze)):
printPath(maze, i, j + k, ans + "(" + str (i) + ", " + str (j) + ") -> " );
if (i + k < len (maze)):
printPath(maze, i + k, j, ans + "(" + str (i) + ", " + str (j) + ") -> " );
if (j - k > 0 ):
printPath(maze, i, j - k, ans + "(" + str (i) + ", " + str (j) + ") -> " );
if (i - k > 0 ):
printPath(maze, i - k, j, ans + "(" + str (i) + ", " + str (j) + ") -> " );
maze[i][j] = k;
if __name__ = = '__main__' :
maze = [[ 3 , 5 , 4 , 4 , 7 , 3 , 4 , 6 , 3 ],[ 6 , 7 , 5 , 6 , 6 , 2 , 6 , 6 , 2 ],[ 3 , 3 , 4 , 3 , 2 , 5 , 4 , 7 , 2 ],
[ 6 , 5 , 5 , 1 , 2 , 3 , 6 , 5 , 6 ],[ 3 , 3 , 4 , 3 , 0 , 1 , 4 , 3 , 4 ],[ 3 , 5 , 4 , 3 , 2 , 2 , 3 , 3 , 5 ],
[ 3 , 5 , 4 , 3 , 2 , 6 , 4 , 4 , 3 ],[ 3 , 5 , 1 , 3 , 7 , 5 , 3 , 6 , 4 ],[ 6 , 2 , 4 , 3 , 4 , 5 , 4 , 5 , 1 ]] ;
printPath(maze, 0 , 0 , "");
|
C#
using System;
class GFG{
public static void Main(String[] args)
{
int [,] maze = {
{ 3, 5, 4, 4, 7, 3, 4, 6, 3 },
{ 6, 7, 5, 6, 6, 2, 6, 6, 2 },
{ 3, 3, 4, 3, 2, 5, 4, 7, 2 },
{ 6, 5, 5, 1, 2, 3, 6, 5, 6 },
{ 3, 3, 4, 3, 0, 1, 4, 3, 4 },
{ 3, 5, 4, 3, 2, 2, 3, 3, 5 },
{ 3, 5, 4, 3, 2, 6, 4, 4, 3 },
{ 3, 5, 1, 3, 7, 5, 3, 6, 4 },
{ 6, 2, 4, 3, 4, 5, 4, 5, 1 }
};
printPath(maze, 0, 0, "" );
}
public static void printPath( int [,] maze, int i,
int j, String ans)
{
if (i == maze.GetLength(0) / 2 &&
j == maze.GetLength(1) / 2)
{
ans += "(" + i + ", " + j + ") -> MID" ;
Console.WriteLine(ans);
return ;
}
if (maze[i, j] == 0)
{
return ;
}
int k = maze[i, j];
maze[i, j] = 0;
if (j + k < maze.GetLength(1))
{
printPath(maze, i, j + k,
ans + "(" + i +
", " + j + ") -> " );
}
if (i + k < maze.GetLength(0))
{
printPath(maze, i + k, j,
ans + "(" + i +
", " + j + ") -> " );
}
if (j - k > 0)
{
printPath(maze, i, j - k,
ans + "(" + i +
", " + j + ") -> " );
}
if (i - k > 0)
{
printPath(maze, i - k, j,
ans + "(" + i +
", " + j + ") -> " );
}
maze[i, j] = k;
}
}
|
Javascript
<script>
function printPath( maze,i,j,ans)
{
if (i == Math.floor(maze.length/2) && j == Math.floor(maze.length/2))
{
ans += "(" +i+ ", " +j+ ") -> MID" ;
document.write(ans+ "<br>" );
return ;
}
if (maze[i][j] == 0){
return ;
}
let k = maze[i][j];
maze[i][j] = 0;
if (j + k < maze.length){
printPath(maze, i, j+k, ans+ "(" +i+ ", " +j+ ") -> " );
}
if (i + k < maze.length){
printPath(maze, i+k, j, ans+ "(" +i+ ", " +j+ ") -> " );
}
if (j-k>0){
printPath(maze, i, j-k, ans+ "(" +i+ ", " +j+ ") -> " );
}
if (i-k>0){
printPath(maze, i-k, j, ans+ "(" +i+ ", " +j+ ") -> " );
}
maze[i][j] = k;
}
let maze = [[ 3, 5, 4, 4, 7, 3, 4, 6, 3 ],[ 6, 7, 5, 6, 6, 2, 6, 6, 2 ],[ 3, 3, 4, 3, 2, 5, 4, 7, 2 ],
[ 6, 5, 5, 1, 2, 3, 6, 5, 6 ],[ 3, 3, 4, 3, 0, 1, 4, 3, 4 ],[ 3, 5, 4, 3, 2, 2, 3, 3, 5 ],
[ 3, 5, 4, 3, 2, 6, 4, 4, 3 ],[ 3, 5, 1, 3, 7, 5, 3, 6, 4 ],[ 6, 2, 4, 3, 4, 5, 4, 5, 1 ]] ;
printPath(maze, 0, 0, "" );
</script>
|
Output :
(0, 0) -> (0, 3) -> (0, 7) ->
(6, 7) -> (6, 3) -> (3, 3) ->
(3, 4) -> (5, 4) -> (5, 2) ->
(1, 2) -> (1, 7) -> (7, 7) ->
(7, 1) -> (2, 1) -> (2, 4) ->
(4, 4) -> MID
Time Complexity: O(4^N2), where N is the size of the maze and the function makes recursive calls in all four directions, resulting in a branching factor of 4.
Space Complexity: O(N2), where N is the size of the maze. This is because the function uses a 2D vector to represent the maze, which has N^2 elements.
Share your thoughts in the comments
Please Login to comment...