Skip to content

Commit 982cae2

Browse files
authored
Update Backtracking-Rat in a Maze.cpp
1 parent eb8d573 commit 982cae2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Backtracking/Backtracking-Rat in a Maze.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
/* Rat In a Maze
2+
---------------------------------------------------------------------
3+
4+
Problem: Given a maze(2D matrix) with obstacles, starting from (0,0) you have to
5+
reach (n-1, n-1). If you are currently on (x,y), you can move to (x+1,y) or (x,y+1).
6+
You can not move to the walls.
7+
Idea: Try all the possible paths to see if you can reach (n-1,n-1)
8+
9+
--------------------------------------------------------------------------------
10+
11+
Input:
12+
----
13+
0 denotes wall, 1 denotes free path
14+
two numbers n, m
15+
Next n lines contain m numbers (0 or 1)
16+
17+
Output:
18+
--------
19+
Print 1 if rat can reach (n-1,m-1)
20+
Print 0 if it can not reach (n-1,m-1)
21+
22+
------------------------
23+
24+
Test Case 1:
25+
26+
Input:
27+
5 5
28+
1 0 1 0 1
29+
1 1 1 1 1
30+
0 1 0 1 0
31+
1 0 0 1 1
32+
1 1 1 0 1
33+
34+
Output:
35+
36+
1 0 0 0 0
37+
1 1 1 1 0
38+
0 0 0 1 0
39+
0 0 0 1 1
40+
0 0 0 0 1
41+
42+
*/
43+
44+
145
#include <iostream>
246
using namespace std;
347

@@ -68,3 +112,7 @@ int main(){
68112

69113
return 0;
70114
}
115+
116+
/* Time Complexity: O(2n)
117+
Space Complexity: O(2n)
118+
*/

0 commit comments

Comments
 (0)