-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilities.py
42 lines (37 loc) · 1.62 KB
/
Utilities.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
37
38
39
40
41
42
class IslandFinder:
# Courtesy of https://medium.com/analytics-vidhya/number-of-islands-day-5-python-5c12783515b2
min_island_size = 49
current_island_size = 0
new_island = False
grid = None
def __init__(self, grid):
self.grid = grid
def isSafe(self, grid, row, col, visited):
return(0 <= row < len(grid) and
0 <= col < len(grid[0]) and
grid[row][col] == 0 and visited[row][col] == False)
def dfs(self, row, col, visited, grid):
valid_row = [-1, 0, 0, 1]
valid_col = [0, -1, 1, 0]
visited[row][col] = True
for neighbour in range(len(valid_row)):
new_row = row + valid_row[neighbour]
new_col = col + valid_col[neighbour]
if(self.isSafe(grid, new_row, new_col, visited)):
self.current_island_size += 1
self.dfs(new_row, new_col, visited, grid)
def get_min_island_size(self):
visited = [[False for col in range(len(self.grid[0]))]
for row in range(len(self.grid))]
count = 0
for row in range(len(self.grid)):
for col in range(len(self.grid[0])):
if(self.grid[row][col] == 0 and visited[row][col] == False):
self.current_island_size = 1
self.dfs(row, col, visited, self.grid)
self.min_island_size = min(
self.min_island_size, self.current_island_size)
count += 1
return self.min_island_size
def set_grid(self, board):
self.grid = board