|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 |
|
4 |
| -def solve_maze(maze: list[list[int]]) -> bool: |
| 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]]: |
5 | 11 | """
|
6 | 12 | This method solves the "rat in maze" problem.
|
7 |
| - In this problem we have some n by n matrix, a start point and an end point. |
8 |
| - We want to go from the start to the end. In this matrix zeroes represent walls |
9 |
| - and ones paths we can use. |
10 | 13 | Parameters :
|
11 |
| - maze(2D matrix) : maze |
| 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. |
12 | 19 | Returns:
|
13 |
| - Return: True if the maze has a solution or False if it does not. |
| 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. |
14 | 31 | >>> maze = [[0, 1, 0, 1, 1],
|
15 | 32 | ... [0, 0, 0, 0, 0],
|
16 | 33 | ... [1, 0, 1, 0, 1],
|
17 | 34 | ... [0, 0, 1, 0, 0],
|
18 | 35 | ... [1, 0, 0, 1, 0]]
|
19 |
| - >>> solve_maze(maze) |
20 |
| - [1, 0, 0, 0, 0] |
21 |
| - [1, 1, 1, 1, 0] |
22 |
| - [0, 0, 0, 1, 0] |
23 |
| - [0, 0, 0, 1, 1] |
24 |
| - [0, 0, 0, 0, 1] |
25 |
| - True |
| 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. |
26 | 46 |
|
27 | 47 | >>> maze = [[0, 1, 0, 1, 1],
|
28 | 48 | ... [0, 0, 0, 0, 0],
|
29 | 49 | ... [0, 0, 0, 0, 1],
|
30 | 50 | ... [0, 0, 0, 0, 0],
|
31 | 51 | ... [0, 0, 0, 0, 0]]
|
32 |
| - >>> solve_maze(maze) |
33 |
| - [1, 0, 0, 0, 0] |
34 |
| - [1, 0, 0, 0, 0] |
35 |
| - [1, 0, 0, 0, 0] |
36 |
| - [1, 0, 0, 0, 0] |
37 |
| - [1, 1, 1, 1, 1] |
38 |
| - True |
| 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]] |
39 | 58 |
|
40 | 59 | >>> maze = [[0, 0, 0],
|
41 | 60 | ... [0, 1, 0],
|
42 | 61 | ... [1, 0, 0]]
|
43 |
| - >>> solve_maze(maze) |
44 |
| - [1, 1, 1] |
45 |
| - [0, 0, 1] |
46 |
| - [0, 0, 1] |
47 |
| - True |
| 62 | + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) # doctest: +NORMALIZE_WHITESPACE |
| 63 | + [[0, 0, 0], |
| 64 | + [1, 1, 0], |
| 65 | + [1, 1, 0]] |
48 | 66 |
|
49 |
| - >>> maze = [[0, 1, 0], |
| 67 | + >>> maze = [[1, 0, 0], |
50 | 68 | ... [0, 1, 0],
|
51 | 69 | ... [1, 0, 0]]
|
52 |
| - >>> solve_maze(maze) |
53 |
| - No solution exists! |
54 |
| - False |
| 70 | + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) # doctest: +NORMALIZE_WHITESPACE |
| 71 | + [[1, 0, 0], |
| 72 | + [1, 1, 0], |
| 73 | + [1, 1, 0]] |
| 74 | +
|
| 75 | + >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], |
| 76 | + ... [1, 0, 1, 0, 0, 1, 1, 1], |
| 77 | + ... [0, 1, 0, 1, 0, 0, 1, 0], |
| 78 | + ... [1, 1, 1, 0, 0, 1, 0, 1], |
| 79 | + ... [0, 1, 0, 0, 1, 0, 1, 1], |
| 80 | + ... [0, 0, 0, 1, 1, 1, 0, 1], |
| 81 | + ... [0, 1, 0, 1, 0, 1, 1, 1], |
| 82 | + ... [1, 1, 0, 0, 0, 0, 0, 1]] |
| 83 | + >>> solve_maze(maze,0,2,len(maze)-1,2) # doctest: +NORMALIZE_WHITESPACE |
| 84 | + [[1, 1, 0, 0, 1, 1, 1, 1], |
| 85 | + [1, 1, 1, 0, 0, 1, 1, 1], |
| 86 | + [1, 1, 1, 1, 0, 1, 1, 1], |
| 87 | + [1, 1, 1, 0, 0, 1, 1, 1], |
| 88 | + [1, 1, 0, 0, 1, 1, 1, 1], |
| 89 | + [1, 1, 0, 1, 1, 1, 1, 1], |
| 90 | + [1, 1, 0, 1, 1, 1, 1, 1], |
| 91 | + [1, 1, 0, 1, 1, 1, 1, 1]] |
| 92 | + >>> maze = [[1, 0, 0], |
| 93 | + ... [0, 1, 1], |
| 94 | + ... [1, 0, 1]] |
| 95 | + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) |
| 96 | + Traceback (most recent call last): |
| 97 | + ... |
| 98 | + ValueError: No solution exists! |
| 99 | +
|
| 100 | + >>> maze = [[0, 0], |
| 101 | + ... [1, 1]] |
| 102 | + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) |
| 103 | + Traceback (most recent call last): |
| 104 | + ... |
| 105 | + ValueError: No solution exists! |
55 | 106 |
|
56 | 107 | >>> maze = [[0, 1],
|
57 | 108 | ... [1, 0]]
|
58 |
| - >>> solve_maze(maze) |
59 |
| - No solution exists! |
60 |
| - False |
| 109 | + >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) |
| 110 | + Traceback (most recent call last): |
| 111 | + ... |
| 112 | + ValueError: Invalid source or destination coordinates |
| 113 | +
|
| 114 | + >>> maze = [[1, 0, 0], |
| 115 | + ... [0, 1, 0], |
| 116 | + ... [1, 0, 0]] |
| 117 | + >>> solve_maze(maze,0,1,len(maze),len(maze)-1) |
| 118 | + Traceback (most recent call last): |
| 119 | + ... |
| 120 | + ValueError: Invalid source or destination coordinates |
61 | 121 | """
|
62 | 122 | size = len(maze)
|
| 123 | + # Check if source and destination coordinates are Invalid. |
| 124 | + if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1) or ( |
| 125 | + not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1) |
| 126 | + ): |
| 127 | + raise ValueError("Invalid source or destination coordinates") |
63 | 128 | # We need to create solution object to save path.
|
64 |
| - solutions = [[0 for _ in range(size)] for _ in range(size)] |
65 |
| - solved = run_maze(maze, 0, 0, solutions) |
| 129 | + solutions = [[1 for _ in range(size)] for _ in range(size)] |
| 130 | + solved = run_maze( |
| 131 | + maze, source_row, source_column, destination_row, destination_column, solutions |
| 132 | + ) |
66 | 133 | if solved:
|
67 |
| - print("\n".join(str(row) for row in solutions)) |
| 134 | + return solutions |
68 | 135 | else:
|
69 |
| - print("No solution exists!") |
70 |
| - return solved |
| 136 | + raise ValueError("No solution exists!") |
71 | 137 |
|
72 | 138 |
|
73 |
| -def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) -> bool: |
| 139 | +def run_maze( |
| 140 | + maze: list[list[int]], |
| 141 | + i: int, |
| 142 | + j: int, |
| 143 | + destination_row: int, |
| 144 | + destination_column: int, |
| 145 | + solutions: list[list[int]], |
| 146 | +) -> bool: |
74 | 147 | """
|
75 | 148 | This method is recursive starting from (i, j) and going in one of four directions:
|
76 | 149 | up, down, left, right.
|
77 | 150 | If a path is found to destination it returns True otherwise it returns False.
|
78 |
| - Parameters: |
79 |
| - maze(2D matrix) : maze |
| 151 | + Parameters |
| 152 | + maze: A two dimensional matrix of zeros and ones. |
80 | 153 | i, j : coordinates of matrix
|
81 |
| - solutions(2D matrix) : solutions |
| 154 | + solutions: A two dimensional matrix of solutions. |
82 | 155 | Returns:
|
83 | 156 | Boolean if path is found True, Otherwise False.
|
84 | 157 | """
|
85 | 158 | size = len(maze)
|
86 | 159 | # Final check point.
|
87 |
| - if i == j == (size - 1): |
88 |
| - solutions[i][j] = 1 |
| 160 | + if i == destination_row and j == destination_column and maze[i][j] == 0: |
| 161 | + solutions[i][j] = 0 |
89 | 162 | return True
|
90 | 163 |
|
91 | 164 | lower_flag = (not i < 0) and (not j < 0) # Check lower bounds
|
92 | 165 | upper_flag = (i < size) and (j < size) # Check upper bounds
|
93 | 166 |
|
94 | 167 | if lower_flag and upper_flag:
|
95 | 168 | # check for already visited and block points.
|
96 |
| - block_flag = (not solutions[i][j]) and (not maze[i][j]) |
| 169 | + block_flag = (solutions[i][j]) and (not maze[i][j]) |
97 | 170 | if block_flag:
|
98 | 171 | # check visited
|
99 |
| - solutions[i][j] = 1 |
| 172 | + solutions[i][j] = 0 |
100 | 173 |
|
101 | 174 | # check for directions
|
102 | 175 | if (
|
103 |
| - run_maze(maze, i + 1, j, solutions) |
104 |
| - or run_maze(maze, i, j + 1, solutions) |
105 |
| - or run_maze(maze, i - 1, j, solutions) |
106 |
| - or run_maze(maze, i, j - 1, solutions) |
| 176 | + run_maze(maze, i + 1, j, destination_row, destination_column, solutions) |
| 177 | + or run_maze( |
| 178 | + maze, i, j + 1, destination_row, destination_column, solutions |
| 179 | + ) |
| 180 | + or run_maze( |
| 181 | + maze, i - 1, j, destination_row, destination_column, solutions |
| 182 | + ) |
| 183 | + or run_maze( |
| 184 | + maze, i, j - 1, destination_row, destination_column, solutions |
| 185 | + ) |
107 | 186 | ):
|
108 | 187 | return True
|
109 | 188 |
|
110 |
| - solutions[i][j] = 0 |
| 189 | + solutions[i][j] = 1 |
111 | 190 | return False
|
112 | 191 | return False
|
113 | 192 |
|
114 | 193 |
|
115 | 194 | if __name__ == "__main__":
|
116 | 195 | import doctest
|
117 | 196 |
|
118 |
| - doctest.testmod() |
| 197 | + doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE) |
0 commit comments