-
Notifications
You must be signed in to change notification settings - Fork 0
/
176. Detect Cycles in 2D grid.cpp
40 lines (31 loc) · 1.2 KB
/
176. Detect Cycles in 2D grid.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
class Solution {
public:
vector<vector<int> > visited;
vector<vector<int> > offsets={{1,0},{0,1},{0,-1},{-1,0}};
bool containsCycle(vector<vector<char> >& grid) {
visited=grid;
for(int i=0; i<grid.size(); i++)
fill(grid[i].begin(),grid[i].end(),0);
vector<vector<int> > visited(grid.size(),vector<int>(grid.at(0).size(),0));
for(int i=0; i<grid.size(); i++)
for(int j=0; j<grid.at(0).size(); j++) {
if(visited[i][j]==0&&hasCycle(i,j,1,grid[i][j],grid)) {
return true;
}
}
return false;
}
bool hasCycle(int i,int j,int currentSteps,char &target,vector<vector<char> >&grid){
int rows=grid.size(),cols=grid.at(0).size();
if(i<0||i>=rows||j<0||j>=cols||grid[i][j]!=target)
return false;
if(visited[i][j]!=0) {
return currentSteps-visited[i][j]>=3;
}
visited[i][j]=currentSteps;
bool isCyclic=false;
for(int k=0; k<offsets.size(); k++)
isCyclic=isCyclic|hasCycle(i-offsets[k][0],j-offsets[k][1],currentSteps+1,target,grid);
return isCyclic;
}
};