-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathRat In A Maze.cpp
More file actions
56 lines (49 loc) · 1.37 KB
/
Rat In A Maze.cpp
File metadata and controls
56 lines (49 loc) · 1.37 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
51
52
53
54
55
56
class Solution {
public:
void pushing(int x, int y, vector<vector<int>> &v,string temp, vector<string> &ans, vector<vector<int>> &vis) {
int n = v.size();
if (x == n-1 && y == n-1) {
ans.push_back(temp);
return;
}
if (y+1<n && v[x][y+1] && vis[x][y+1] == 0) {
vis[x][y+1] = 1;
temp += "R";
pushing(x,y+1,v,temp,ans,vis);
temp.pop_back();
vis[x][y+1] = 0;
}
if (y-1>=0 && v[x][y-1] && vis[x][y-1] == 0) {
vis[x][y-1] = 1;
temp += "L";
pushing(x,y-1,v,temp,ans,vis);
temp.pop_back();
vis[x][y-1] = 0;
}
if (x+1<n && v[x+1][y] && vis[x+1][y] == 0) {
vis[x+1][y] = 1;
temp += "D";
pushing(x+1,y,v,temp,ans,vis);
temp.pop_back();
vis[x+1][y] = 0;
}
if (x-1>=0 && v[x-1][y] && vis[x-1][y] == 0) {
vis[x-1][y] = 1;
temp += "U";
pushing(x-1,y,v,temp,ans,vis);
temp.pop_back();
vis[x-1][y] = 0;
}
return;
}
vector<string> ratInMaze(vector<vector<int>>& maze) {
int n = maze.size();
string ans = "";
vector<string> v;
vector<vector<int>> vis(n,vector<int>(n,0));
vis[0][0] = 1;
pushing(0,0,maze,ans,v,vis);
sort(v.begin(),v.end());
return v;
}
};