forked from ccgcv/Cplus-plus-for-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRat in a maze.cpp
94 lines (79 loc) · 2.88 KB
/
Rat in a maze.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Statement
Consider a rat placed at (0, 0) in a square matrix of order N * N.
It has to reach the destination at (N - 1, N - 1).
Find all possible paths that the rat can take to reach from source to destination.
The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right).
Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1
at a cell in the matrix represents that rat can be travel through it.
Note: In a path, no cell can be visited more than one time.
If the source cell is 0, the rat cannot move to any other cell.
// Example :
Input:N = 4
m[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 1}}
Output: DDRDRR DRDDRR
Explanation:The rat can reach the destination at (3, 3) from (0, 0) by two paths - DRDDRR
and DDRDRR, when printed in sorted order we get DDRDRR DRDDRR.
// code
// #include <bits/stdc++.h>
class Solution{
private:
bool isPossible(int x ,int y, int n, vector<vector<int>> &visited, vector<vector<int>>&m){
if((x>=0 && x<n) && (y>=0 && y<n) && (visited[x][y]==0) && (m[x][y]==1)){
return true;
}
return false;
}
void solve(vector<vector<int>> &m,vector<vector<int>> visited,string temp,int n,int x,int y,vector<string> &res){
if(x==n-1 && y==n-1){
res.push_back(temp);
return;
}
visited[x][y]=1;
string save=temp;
int newx=x+1;
int newy=y;
if(isPossible(newx,newy,n,visited,m)==true){
temp+="D";
solve(m,visited,temp,n,newx,newy,res);
temp=save;
}
newx=x-1;
newy=y;
if(isPossible(newx,newy,n,visited,m)==true){
temp+="U";
solve(m,visited,temp,n,newx,newy,res);
temp=save;
}
newx=x;
newy=y+1;
if(isPossible(newx,newy,n,visited,m)==true){
temp+="R";
solve(m,visited,temp,n,newx,newy,res);
temp=save;
}
newx=x;
newy=y-1;
if(isPossible(newx,newy,n,visited,m)==true){
temp+="L";
solve(m,visited,temp,n,newx,newy,res);
temp=save;
}
visited[x][y]=0;
}
public:
vector<string> findPath(vector<vector<int>> &m, int n) {
vector<string> res;
if(m[0][0]==0){
return res;
}
vector<vector<int>> visited(n,vector<int>(n,0));
string temp="";
int x=0,y=0;
solve(m,visited,temp,n,x,y,res);
sort(res.begin(),res.end());
return res;
}
};