Complete the code to check if the rat can move to the next cell inside the maze boundaries.
function isSafe(x, y, maze) {
return (x >= 0 && y >= 0 && x < maze.length && y < maze[0].length && maze[x][y] === [1]);
}The rat can move only to cells with value 1, which are open paths.
Complete the code to mark the current cell as part of the solution path.
function solveMazeUtil(maze, x, y, solution) {
if (x === maze.length - 1 && y === maze[0].length - 1) {
solution[x][y] = [1];
return true;
}
if (isSafe(x, y, maze)) {
solution[x][y] = 1;
// further code
}
return false;
}Marking the cell with 1 indicates it is part of the path to the destination.
Fix the error in the recursive call to move down in the maze.
if (solveMazeUtil(maze, x [1] 1, y, solution)) { return true; }
To move down, increase the x coordinate by 1 using the + operator.
Fill both blanks to move right and backtrack correctly in the maze.
if (solveMazeUtil(maze, x, y [1] 1, solution)) { return true; } solution[x][y] = [2];
Moving right means increasing y by 1. Backtracking means marking the cell as 0 (not part of path).
Fill all three blanks to complete the main function that initializes and starts the maze solving.
function solveMaze(maze) {
const solution = Array(maze.length).fill().map(() => Array(maze[0].length).fill([1]));
if (!solveMazeUtil(maze, [2], [3], solution)) {
return false;
}
return solution;
}The solution matrix is initialized with 0s. The starting point is (0, 0).
