-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path490.cpp
More file actions
40 lines (34 loc) · 1.28 KB
/
490.cpp
File metadata and controls
40 lines (34 loc) · 1.28 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
class Solution {
public:
vector<vector<bool>> visited;
bool dfs(vector<vector<int>>& maze, vector<int> start, vector<int>& destination){
if(visited[start[0]][start[1]])
return false;
if(start[0]==destination[0] && start[1] == destination[1])
return true;
visited[start[0]][start[1]] = 1;
int r = start[1] + 1, l = start[1] - 1 , u = start[0] - 1, d = start[0] + 1;
while(r<maze[0].size() && maze[start[0]][r]==0)
r++;
if(dfs(maze,{start[0],r-1},destination))
return true;
while(l>=0 && maze[start[0]][l]==0)
l--;
if(dfs(maze,{start[0],l+1},destination))
return true;
while(u>=0 && maze[u][start[1]]==0)
u--;
if(dfs(maze,{u+1,start[1]},destination))
return true;
while(d<maze.size() && maze[d][start[1]]==0)
d++;
if(dfs(maze,{d-1,start[1]},destination))
return true;
return false;
}
bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
int n = maze.size(), m = maze[0].size();
visited.resize(n,vector<bool>(m,false));
return dfs(maze,start, destination);
}
};