Complete the code to check if the rat can move to the next cell inside the maze boundaries.
func isSafe(x, y, n int, maze [][]int) bool {
return x >= 0 && y >= 0 && x < [1] && y < n && maze[x][y] == 1
}The maze size is given by the length of the maze slice. So, to check boundaries, use len(maze).
Complete the code to mark the current cell as part of the solution path.
func solveMazeUtil(maze [][]int, x, y, n int, sol [][]int) bool {
if x == n-1 && y == n-1 && maze[x][y] == 1 {
sol[x][y] = [1]
return true
}
if isSafe(x, y, n, maze) {
sol[x][y] = 1
// Move forward in x direction
if solveMazeUtil(maze, x+1, y, n, sol) {
return true
}
// Move down in y direction
if solveMazeUtil(maze, x, y+1, n, sol) {
return true
}
sol[x][y] = 0
return false
}
return false
}Marking the cell as 1 means it is part of the solution path.
Fix the error in the recursive calls to explore the maze directions.
if solveMazeUtil(maze, x+[1], y, n, sol) { return true } if solveMazeUtil(maze, x, y+[1], n, sol) { return true }
We move one step forward in x or y direction, so increment by 1.
Fill both blanks to initialize the solution matrix with zeros.
sol := make([][]int, n) for i := 0; i < n; i++ { sol[i] = make([]int, [1]) for j := 0; j < [2]; j++ { sol[i][j] = 0 } }
The solution matrix is n x n, so both dimensions are n.
Fill all three blanks to print the solution path after solving the maze.
for i := 0; i < n; i++ { for j := 0; j < n; j++ { fmt.Print(sol[i][[1]], " ") } fmt.Println() } if solveMazeUtil(maze, 0, 0, n, sol) { fmt.Println("[2] found") } else { fmt.Println("No [3] found") }
Use j to print columns, and print 'path found' or 'No path found' messages.
