-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ratfrommaze
52 lines (43 loc) · 1.28 KB
/
Ratfrommaze
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
class Solution{
public:
#include <bits/stdc++.h>
void helper(vector < vector < int >> &arr,int i , int j , string s ,vector <string> &ans , int n, vector < vector <int> > &vis)
{
if(i == n - 1 && j == n - 1)
{
ans.push_back(s) ;
// return ;
}
int x[4] = {1 , 0 , -1 , 0} ;
int y[4] = {0 , -1 , 0 , 1} ;
string temp[4] = {"D" , "L" , "U" , "R"} ;
for(int k = 0 ; k < 4 ; k++)
{
int xcor = i + x[k] ;
int ycor = j + y[k] ;
if(xcor >= 0 && xcor < n && ycor >=0 && ycor <n && vis[xcor][ycor] == 0 && arr[xcor][ycor] == 1 )
{
vis[xcor][ycor] = 1;
helper(arr , xcor , ycor , s + temp[k], ans , n , vis) ;
vis[xcor][ycor] = 0 ;
}
}
return ;
}
vector < string > searchMaze(vector < vector < int >> & arr, int n) {
// Write your code here.
vector <string> ans ;
vector < vector <int> > vis(n , vector <int> (n , 0)) ;
if(arr[0][0] == 0)
{
return ans;
}
vis[0][0] = 1;
helper(arr, 0 , 0 , "" ,ans ,n , vis) ;
return ans ;
}
vector<string> findPath(vector<vector<int>> &m, int n) {
// Your code goes here
return searchMaze(m , n) ;
}
};