This method solves the "rat in maze" problem. Parameters : - maze: A two dimensional matrix of zeros and ones. - source_row: The row index of the starting point. - source_column: The column index of the starting point. - destination_row: The row index of the
(
maze: list[list[int]],
source_row: int,
source_column: int,
destination_row: int,
destination_column: int,
)
| 2 | |
| 3 | |
| 4 | def solve_maze( |
| 5 | maze: list[list[int]], |
| 6 | source_row: int, |
| 7 | source_column: int, |
| 8 | destination_row: int, |
| 9 | destination_column: int, |
| 10 | ) -> list[list[int]]: |
| 11 | """ |
| 12 | This method solves the "rat in maze" problem. |
| 13 | Parameters : |
| 14 | - maze: A two dimensional matrix of zeros and ones. |
| 15 | - source_row: The row index of the starting point. |
| 16 | - source_column: The column index of the starting point. |
| 17 | - destination_row: The row index of the destination point. |
| 18 | - destination_column: The column index of the destination point. |
| 19 | Returns: |
| 20 | - solution: A 2D matrix representing the solution path if it exists. |
| 21 | Raises: |
| 22 | - ValueError: If no solution exists or if the source or |
| 23 | destination coordinates are invalid. |
| 24 | Description: |
| 25 | This method navigates through a maze represented as an n by n matrix, |
| 26 | starting from a specified source cell and |
| 27 | aiming to reach a destination cell. |
| 28 | The maze consists of walls (1s) and open paths (0s). |
| 29 | By providing custom row and column values, the source and destination |
| 30 | cells can be adjusted. |
| 31 | >>> maze = [[0, 1, 0, 1, 1], |
| 32 | ... [0, 0, 0, 0, 0], |
| 33 | ... [1, 0, 1, 0, 1], |
| 34 | ... [0, 0, 1, 0, 0], |
| 35 | ... [1, 0, 0, 1, 0]] |
| 36 | >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) # doctest: +NORMALIZE_WHITESPACE |
| 37 | [[0, 1, 1, 1, 1], |
| 38 | [0, 0, 0, 0, 1], |
| 39 | [1, 1, 1, 0, 1], |
| 40 | [1, 1, 1, 0, 0], |
| 41 | [1, 1, 1, 1, 0]] |
| 42 | |
| 43 | Note: |
| 44 | In the output maze, the zeros (0s) represent one of the possible |
| 45 | paths from the source to the destination. |
| 46 | |
| 47 | >>> maze = [[0, 1, 0, 1, 1], |
| 48 | ... [0, 0, 0, 0, 0], |
| 49 | ... [0, 0, 0, 0, 1], |
| 50 | ... [0, 0, 0, 0, 0], |
| 51 | ... [0, 0, 0, 0, 0]] |
| 52 | >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) # doctest: +NORMALIZE_WHITESPACE |
| 53 | [[0, 1, 1, 1, 1], |
| 54 | [0, 1, 1, 1, 1], |
| 55 | [0, 1, 1, 1, 1], |
| 56 | [0, 1, 1, 1, 1], |
| 57 | [0, 0, 0, 0, 0]] |
| 58 | |
| 59 | >>> maze = [[0, 0, 0], |
| 60 | ... [0, 1, 0], |
| 61 | ... [1, 0, 0]] |
nothing calls this directly
no test coverage detected