forked from ghostmkg/dsa-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRat in a Maze.cpp
More file actions
50 lines (41 loc) · 1.41 KB
/
Rat in a Maze.cpp
File metadata and controls
50 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
int n,m;
vector<string>result;
void solve(vector<vector<int>>& maze, int i, int j, string &temp,vector<vector<bool>>&visited){
if (i>=n || j>=n || i<0 || j<0 || maze[i][j]==0 || visited[i][j]) {
return ;
}
if (i==n-1 && j ==m-1) {
result.push_back(temp);
return ;
}
visited[i][j]=true;
temp.push_back('D');
solve(maze , i+1,j,temp , visited);
temp.pop_back();
temp.push_back('R');
solve(maze , i,j+1,temp ,visited);
temp.pop_back();
temp.push_back('U');
solve(maze , i-1,j,temp,visited);
temp.pop_back();
temp.push_back('L');
solve(maze , i,j-1,temp, visited);
temp.pop_back();
visited[i][j]=false;
}
vector<string> ratInMaze(vector<vector<int>>& maze) {
// code here
n= maze.size();
m= maze[0].size();
string temp = "";
if (maze[0][0] == 0) {
return {}; // Return an empty vector immediately
}
vector<vector<bool>>visited(n,vector<bool>(m,0));
solve(maze,0,0,temp,visited);
sort(result.begin(),result.end());
return result;
}
};