forked from srgnk/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connected-cell-in-a-grid.py
38 lines (32 loc) · 976 Bytes
/
connected-cell-in-a-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
37
38
def find_adjacent(grid, i, j):
count = 0
if i < 0 or j < 0 or i >= n or j >= m:
return 0
if grid[i][j] == 0:
return 0
count += 1
grid[i][j] -= 1
count += find_adjacent(grid, i-1, j-1)
count += find_adjacent(grid, i-1, j)
count += find_adjacent(grid, i-1, j+1)
count += find_adjacent(grid, i+1, j-1)
count += find_adjacent(grid, i+1, j)
count += find_adjacent(grid, i+1, j+1)
count += find_adjacent(grid, i, j-1)
count += find_adjacent(grid, i, j+1)
return count
def get_biggest_region(grid):
biggest = 0
for i in range(n):
for j in range(m):
result = find_adjacent(grid, i, j)
if result > biggest:
biggest = result
return biggest
n = int(input().strip())
m = int(input().strip())
grid = []
for grid_i in range(n):
grid_t = list(map(int, input().strip().split(' ')))
grid.append(grid_t)
print(get_biggest_region(grid))