Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the rat can move to the cell (x, y) inside the maze boundaries.
DSA Rust
fn is_safe(x: i32, y: i32, n: usize) -> bool {
x >= 0 && y >= 0 && (x as usize) < n && (y as usize) < n && [1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning false instead of true.
Checking only one boundary condition.
✗ Incorrect
The function checks if the coordinates are within bounds and the cell is safe. Returning true means the cell is safe to move.
2fill in blank
mediumComplete the code to mark the current cell (x, y) as part of the solution path.
DSA Rust
fn solve_maze_util(maze: &Vec<Vec<i32>>, x: usize, y: usize, solution: &mut Vec<Vec<i32>>, n: usize) -> bool {
if x == n - 1 && y == n - 1 {
solution[x][y] = [1];
return true;
}
if is_safe(x as i32, y as i32, n) {
solution[x][y] = 1;
// rest of the code
}
false
}Attempts:
3 left
💡 Hint
Common Mistakes
Marking the cell with 0 which means no path.
Using negative numbers which are invalid here.
✗ Incorrect
Marking the cell with 1 means it is part of the path.
3fill in blank
hardFix the error in the recursive call to move right in the maze.
DSA Rust
if solve_maze_util(maze, x, y + 1, solution, n) [1] { return true; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment '=' instead of comparison '=='.
Checking for false instead of true.
✗ Incorrect
The recursive call returns a boolean. We check if it is true to continue the path.
4fill in blank
hardFill both blanks to backtrack by unmarking the current cell and returning false.
DSA Rust
solution[x][y] = [1]; return [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning true after backtracking.
Not unmarking the cell.
✗ Incorrect
Backtracking means unmarking the cell with 0 and returning false to try other paths.
5fill in blank
hardFill all three blanks to complete the main solve_maze function that initializes the solution matrix and calls the utility function.
DSA Rust
fn solve_maze(maze: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = maze.len();
let mut solution = vec![vec![[1]; n]; n];
if solve_maze_util([2], 0, 0, &mut solution, n) {
return [3];
}
vec![vec![0; n]; n]
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use `.clone()` when returning solution.
Initializing with wrong value instead of 0.
Passing wrong argument to solve_maze_util.
✗ Incorrect
Initialize the solution matrix with 0s, pass the maze reference to the utility function, and return a clone of the solution if a path is found.
