forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetect Cycles in 2D Grid.py
36 lines (32 loc) · 1.52 KB
/
Detect Cycles in 2D Grid.py
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
class Solution:
def containsCycle(self, grid: List[List[str]]) -> bool:
def getNeighbours(row,col,char):
neighbours = []
if row > 0 and grid[row-1][col] == char and not visited[row-1][col]:
neighbours.append([row-1,col])
if col > 0 and grid[row][col-1] == char and not visited[row][col-1]:
neighbours.append([row,col-1])
if row < len(grid)-1 and grid[row+1][col] == char and not visited[row+1][col]:
neighbours.append([row+1,col])
if col < len(grid[0])-1 and grid[row][col+1] == char and not visited[row][col+1]:
neighbours.append([row,col+1])
return neighbours
def dfs(row,col,char,cyclePresent):
if visited[row][col] or cyclePresent:
cyclePresent = True
return cyclePresent
visited[row][col] = True
neighbours = getNeighbours(row,col,char)
for r,c in neighbours:
cyclePresent = dfs(r,c,char,cyclePresent)
return cyclePresent
visited = [[False for _ in range(len(grid[0]))] for _ in range(len(grid))]
cyclePresent = False
for row in range(len(grid)):
for col in range(len(grid[0])):
if cyclePresent:
return True
if visited[row][col]:
continue
cyclePresent = dfs(row,col,grid[row][col],cyclePresent)
return cyclePresent