Skip to content

Commit cedaf35

Browse files
fixed whitespaces error,improved matrix visual.
1 parent b59a85d commit cedaf35

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

backtracking/rat_in_maze.py

+29-14
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def solve_maze(
1818
- destination_column (int): The column index of the destination point.
1919
Returns:
2020
- solution (list[list[int]]): A 2D matrix representing the solution path
21-
... if it exists.
21+
if it exists.
2222
Raises:
23-
- ValueError: If no solution exists or if the source or\
24-
destination coordinates are invalid.
23+
- ValueError: If no solution exists or if the source or
24+
destination coordinates are invalid.
2525
Description:
2626
This method navigates through a maze represented as an n by n matrix,
2727
starting from a specified source cell and
@@ -35,8 +35,11 @@ def solve_maze(
3535
... [0, 0, 1, 0, 0],
3636
... [1, 0, 0, 1, 0]]
3737
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
38-
[[0, 1, 1, 1, 1], [0, 0, 0, 0, 1], [1, 1, 1, 0, 1],\
39-
[1, 1, 1, 0, 0], [1, 1, 1, 1, 0]]
38+
[[0, 1, 1, 1, 1],
39+
[0, 0, 0, 0, 1],
40+
[1, 1, 1, 0, 1],
41+
[1, 1, 1, 0, 0],
42+
[1, 1, 1, 1, 0]]
4043
4144
Note:
4245
In the output maze, the zeros (0s) represent one of the possible
@@ -48,20 +51,27 @@ def solve_maze(
4851
... [0, 0, 0, 0, 0],
4952
... [0, 0, 0, 0, 0]]
5053
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
51-
[[0, 1, 1, 1, 1], [0, 1, 1, 1, 1], [0, 1, 1, 1, 1],\
52-
[0, 1, 1, 1, 1], [0, 0, 0, 0, 0]]
54+
[[0, 1, 1, 1, 1],
55+
[0, 1, 1, 1, 1],
56+
[0, 1, 1, 1, 1],
57+
[0, 1, 1, 1, 1],
58+
[0, 0, 0, 0, 0]]
5359
5460
>>> maze = [[0, 0, 0],
5561
... [0, 1, 0],
5662
... [1, 0, 0]]
5763
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
58-
[[0, 0, 0], [1, 1, 0], [1, 1, 0]]
64+
[[0, 0, 0],
65+
[1, 1, 0],
66+
[1, 1, 0]]
5967
6068
>>> maze = [[1, 0, 0],
6169
... [0, 1, 0],
6270
... [1, 0, 0]]
6371
>>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1)
64-
[[1, 0, 0], [1, 1, 0], [1, 1, 0]]
72+
[[1, 0, 0],
73+
[1, 1, 0],
74+
[1, 1, 0]]
6575
6676
>>> maze = [[1, 1, 0, 0, 1, 0, 0, 1],
6777
... [1, 0, 1, 0, 0, 1, 1, 1],
@@ -72,9 +82,14 @@ def solve_maze(
7282
... [0, 1, 0, 1, 0, 1, 1, 1],
7383
... [1, 1, 0, 0, 0, 0, 0, 1]]
7484
>>> solve_maze(maze,0,2,len(maze)-1,2)
75-
[[1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1],\
76-
[1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1],\
77-
[1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1]]
85+
[[1, 1, 0, 0, 1, 1, 1, 1],
86+
[1, 1, 1, 0, 0, 1, 1, 1],
87+
[1, 1, 1, 1, 0, 1, 1, 1],
88+
[1, 1, 1, 0, 0, 1, 1, 1],
89+
[1, 1, 0, 0, 1, 1, 1, 1],
90+
[1, 1, 0, 1, 1, 1, 1, 1],
91+
[1, 1, 0, 1, 1, 1, 1, 1],
92+
[1, 1, 0, 1, 1, 1, 1, 1]]
7893
>>> maze = [[1, 0, 0],
7994
... [0, 1, 1],
8095
... [1, 0, 1]]
@@ -98,7 +113,7 @@ def solve_maze(
98113
ValueError: Invalid source or destination coordinates
99114
100115
>>> maze = [[1, 0, 0],
101-
... [0, 1, 1],
116+
... [0, 1, 0],
102117
... [1, 0, 0]]
103118
>>> solve_maze(maze,0,1,len(maze),len(maze)-1)
104119
Traceback (most recent call last):
@@ -180,4 +195,4 @@ def run_maze(
180195
if __name__ == "__main__":
181196
import doctest
182197

183-
doctest.testmod()
198+
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)

0 commit comments

Comments
 (0)