diff --git a/challenges/maze-solver/description.md b/challenges/maze-solver/description.md index 8ca50fc..48788e5 100644 --- a/challenges/maze-solver/description.md +++ b/challenges/maze-solver/description.md @@ -12,6 +12,8 @@ Your task is to write a function `solve_maze` that takes a **2D vector of charac - You can only move **up, down, left, or right**. - Implement the function using appropriate control flow constructs **(loops, conditionals, and/or recursion)**. - Ensure your solution **handles edge cases**, such as no available path or mazes with various sizes. +- If the maze is a dead-end, return an empty vector. +- If the maze has multiple solutions, return the shortest path. ## Example diff --git a/challenges/maze-solver/tests/tests.rs b/challenges/maze-solver/tests/tests.rs index 9063689..25c222e 100644 --- a/challenges/maze-solver/tests/tests.rs +++ b/challenges/maze-solver/tests/tests.rs @@ -30,6 +30,99 @@ mod tests { ); } + #[test] + fn test_solve_maze_2() { + let maze = vec![ + vec!['S', '.', '.', '#', '#'], + vec!['#', '#', '.', '#', '.'], + vec!['#', '.', '.', '.', '.'], + vec!['#', '#', '#', '#', '.'], + vec!['#', '#', '#', 'E', '.'], + ]; + let start = (0, 0); + let end = (4, 3); + + let path = solve_maze(maze, start, end); + assert_eq!( + path, + vec![ + (0, 0), + (0, 1), + (0, 2), + (1, 2), + (2, 2), + (2, 3), + (2, 4), + (3, 4), + (4, 4), + (4, 3) + ] + ); + } + + #[test] + fn test_solve_maze_3() { + let maze = vec![ + vec!['S', '.', '#', '#', '#'], + vec!['#', '.', '.', '.', '.'], + vec!['#', '#', '#', '#', '.'], + vec!['#', 'E', '.', '.', '.'], + vec!['#', '#', '#', '#', '#'], + ]; + let start = (0, 0); + let end = (3, 1); + + let path = solve_maze(maze, start, end); + assert_eq!( + path, + vec![ + (0, 0), + (0, 1), + (1, 1), + (1, 2), + (1, 3), + (1, 4), + (2, 4), + (3, 4), + (3, 3), + (3, 2), + (3, 1) + ] + ); + } + + #[test] + fn test_solve_maze_4() { + let maze = vec![ + vec!['S', '.', '.', '#', 'E'], + vec!['#', '#', '.', '#', '#'], + vec!['#', '.', '.', '.', '#'], + vec!['#', '#', '#', '.', '#'], + vec!['#', '#', '#', '#', '#'], + ]; + let start = (0, 0); + let end = (0, 4); + + let path = solve_maze(maze, start, end); + assert_eq!(path, vec![]); + } + + #[test] + fn test_multiple_solutions() { + let maze = vec![ + vec!['S', '.', '.', '.', '.'], + vec!['.', '#', '#', '#', '.'], + vec!['.', '#', '#', '#', '.'], + vec!['.', '#', '#', '#', '.'], + vec!['.', 'E', '.', '.', '.'], + ]; + let start = (0, 0); + let end = (4, 1); + + let path = solve_maze(maze, start, end); + assert_eq!(path, vec![(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, 1)]); + } + #[test] fn test_no_path() { let maze = vec![