@@ -9,120 +9,120 @@ def solve_maze(
9
9
destination_column : int ,
10
10
) -> bool :
11
11
"""
12
- This method solves the "rat in maze" problem.
13
- Parameters :
14
- - maze(2D matrix) : maze
15
- - source_row (int): The row index of the starting point.
16
- - source_column (int): The column index of the starting point.
17
- - destination_row (int): The row index of the destination point.
18
- - destination_column (int): The column index of the destination point.
19
- Returns:
20
- Return: True if the maze has a solution or False if it does not.
21
- Description:
22
- This method navigates through a maze represented as an n by n matrix,
23
- <<<<<<< HEAD
24
- starting from a specified source cell and
25
- aiming to reach a destination cell.
26
- =======
27
- starting from a specified source cell (default: top-left corner) and
28
- aiming to reach a destination cell (default: bottom-right corner).
29
- >>>>>>> origin/new_branch
30
- The maze consists of walls (1s) and open paths (0s).
31
- By providing custom row and column values, the source and destination
32
- cells can be adjusted.
33
- >>> maze = [[0, 1, 0, 1, 1],
34
- ... [0, 0, 0, 0, 0],
35
- ... [1, 0, 1, 0, 1],
36
- ... [0, 0, 1, 0, 0],
37
- ... [1, 0, 0, 1, 0]]
38
- >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
39
- [1, 0, 0, 0, 0]
40
- [1, 1, 1, 1, 0]
41
- [0, 0, 0, 1, 0]
42
- [0, 0, 0, 1, 1]
43
- [0, 0, 0, 0, 1]
44
- True
45
-
46
- Note:
47
- In the output maze, the ones (1s) represent one of the possible
48
- paths from the source to the destination.
49
-
50
- >>> maze = [[0, 1, 0, 1, 1],
51
- ... [0, 0, 0, 0, 0],
52
- ... [0, 0, 0, 0, 1],
53
- ... [0, 0, 0, 0, 0],
54
- ... [0, 0, 0, 0, 0]]
55
- >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
56
- [1, 0, 0, 0, 0]
57
- [1, 0, 0, 0, 0]
58
- [1, 0, 0, 0, 0]
59
- [1, 0, 0, 0, 0]
60
- [1, 1, 1, 1, 1]
61
- True
62
-
63
- >>> maze = [[0, 0, 0],
64
- ... [0, 1, 0],
65
- ... [1, 0, 0]]
66
- >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
67
- [1, 1, 1]
68
- [0, 0, 1]
69
- [0, 0, 1]
70
- True
71
-
72
- >>> maze = [[1, 0, 0],
73
- ... [0, 1, 0],
74
- ... [1, 0, 0]]
75
- >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1)
76
- [0, 1, 1]
77
- [0, 0, 1]
78
- [0, 0, 1]
79
- True
80
-
81
- >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1],
82
- ... [1, 0, 1, 0, 0, 1, 1, 1],
83
- ... [0, 1, 0, 1, 0, 0, 1, 0],
84
- ... [1, 1, 1, 0, 0, 1, 0, 1],
85
- ... [0, 1, 0, 0, 1, 0, 1, 1],
86
- ... [0, 0, 0, 1, 1, 1, 0, 1],
87
- ... [0, 1, 0, 1, 0, 1, 1, 1],
88
- ... [1, 1, 0, 0, 0, 0, 0, 1]]
89
- >>> solve_maze(maze,0,2,len(maze)-1,2)
90
- [0, 0, 1, 1, 0, 0, 0, 0]
91
- [0, 0, 0, 1, 1, 0, 0, 0]
92
- [0, 0, 0, 0, 1, 0, 0, 0]
93
- [0, 0, 0, 1, 1, 0, 0, 0]
94
- [0, 0, 1, 1, 0, 0, 0, 0]
95
- [0, 0, 1, 0, 0, 0, 0, 0]
96
- [0, 0, 1, 0, 0, 0, 0, 0]
97
- [0, 0, 1, 0, 0, 0, 0, 0]
98
- True
99
-
100
-
101
- >>> maze = [[1, 0, 0],
102
- ... [0, 1, 1],
103
- ... [1, 0, 0]]
104
- >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1)
105
- No solution exists!
106
- False
107
-
108
- >>> maze = [[0, 1],
109
- ... [1, 0]]
110
- >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
111
- No solution exists!
112
- False
113
-
114
- >>> maze = [[0, 1],
115
- ... [1, 0]]
116
- >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1)
117
- Invalid source coordinates
118
- False
119
-
120
- >>> maze = [[1, 0, 0],
121
- ... [0, 1, 1],
122
- ... [1, 0, 0]]
123
- >>> solve_maze(maze,0,1,len(maze),len(maze)-1)
124
- Invalid destination coordinates
125
- False
12
+ This method solves the "rat in maze" problem.
13
+ Parameters :
14
+ - maze(2D matrix) : maze
15
+ - source_row (int): The row index of the starting point.
16
+ - source_column (int): The column index of the starting point.
17
+ - destination_row (int): The row index of the destination point.
18
+ - destination_column (int): The column index of the destination point.
19
+ Returns:
20
+ Return: True if the maze has a solution or False if it does not.
21
+ Description:
22
+ This method navigates through a maze represented as an n by n matrix,
23
+ <<<<<<< HEAD
24
+ starting from a specified source cell and
25
+ aiming to reach a destination cell.
26
+ =======
27
+ starting from a specified source cell (default: top-left corner) and
28
+ aiming to reach a destination cell (default: bottom-right corner).
29
+ >>>>>>> origin/new_branch
30
+ The maze consists of walls (1s) and open paths (0s).
31
+ By providing custom row and column values, the source and destination
32
+ cells can be adjusted.
33
+ >>> maze = [[0, 1, 0, 1, 1],
34
+ ... [0, 0, 0, 0, 0],
35
+ ... [1, 0, 1, 0, 1],
36
+ ... [0, 0, 1, 0, 0],
37
+ ... [1, 0, 0, 1, 0]]
38
+ >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
39
+ [1, 0, 0, 0, 0]
40
+ [1, 1, 1, 1, 0]
41
+ [0, 0, 0, 1, 0]
42
+ [0, 0, 0, 1, 1]
43
+ [0, 0, 0, 0, 1]
44
+ True
45
+
46
+ Note:
47
+ In the output maze, the ones (1s) represent one of the possible
48
+ paths from the source to the destination.
49
+
50
+ >>> maze = [[0, 1, 0, 1, 1],
51
+ ... [0, 0, 0, 0, 0],
52
+ ... [0, 0, 0, 0, 1],
53
+ ... [0, 0, 0, 0, 0],
54
+ ... [0, 0, 0, 0, 0]]
55
+ >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
56
+ [1, 0, 0, 0, 0]
57
+ [1, 0, 0, 0, 0]
58
+ [1, 0, 0, 0, 0]
59
+ [1, 0, 0, 0, 0]
60
+ [1, 1, 1, 1, 1]
61
+ True
62
+
63
+ >>> maze = [[0, 0, 0],
64
+ ... [0, 1, 0],
65
+ ... [1, 0, 0]]
66
+ >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
67
+ [1, 1, 1]
68
+ [0, 0, 1]
69
+ [0, 0, 1]
70
+ True
71
+
72
+ >>> maze = [[1, 0, 0],
73
+ ... [0, 1, 0],
74
+ ... [1, 0, 0]]
75
+ >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1)
76
+ [0, 1, 1]
77
+ [0, 0, 1]
78
+ [0, 0, 1]
79
+ True
80
+
81
+ >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1],
82
+ ... [1, 0, 1, 0, 0, 1, 1, 1],
83
+ ... [0, 1, 0, 1, 0, 0, 1, 0],
84
+ ... [1, 1, 1, 0, 0, 1, 0, 1],
85
+ ... [0, 1, 0, 0, 1, 0, 1, 1],
86
+ ... [0, 0, 0, 1, 1, 1, 0, 1],
87
+ ... [0, 1, 0, 1, 0, 1, 1, 1],
88
+ ... [1, 1, 0, 0, 0, 0, 0, 1]]
89
+ >>> solve_maze(maze,0,2,len(maze)-1,2)
90
+ [0, 0, 1, 1, 0, 0, 0, 0]
91
+ [0, 0, 0, 1, 1, 0, 0, 0]
92
+ [0, 0, 0, 0, 1, 0, 0, 0]
93
+ [0, 0, 0, 1, 1, 0, 0, 0]
94
+ [0, 0, 1, 1, 0, 0, 0, 0]
95
+ [0, 0, 1, 0, 0, 0, 0, 0]
96
+ [0, 0, 1, 0, 0, 0, 0, 0]
97
+ [0, 0, 1, 0, 0, 0, 0, 0]
98
+ True
99
+
100
+
101
+ >>> maze = [[1, 0, 0],
102
+ ... [0, 1, 1],
103
+ ... [1, 0, 0]]
104
+ >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1)
105
+ No solution exists!
106
+ False
107
+
108
+ >>> maze = [[0, 1],
109
+ ... [1, 0]]
110
+ >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1)
111
+ No solution exists!
112
+ False
113
+
114
+ >>> maze = [[0, 1],
115
+ ... [1, 0]]
116
+ >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1)
117
+ Invalid source coordinates
118
+ False
119
+
120
+ >>> maze = [[1, 0, 0],
121
+ ... [0, 1, 1],
122
+ ... [1, 0, 0]]
123
+ >>> solve_maze(maze,0,1,len(maze),len(maze)-1)
124
+ Invalid destination coordinates
125
+ False
126
126
"""
127
127
size = len (maze )
128
128
# Check if source and destination coordinates are Invalid.
0 commit comments