-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctci-connected-cell-in-a-grid.py
104 lines (85 loc) · 2.44 KB
/
ctci-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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
DFS: Connected Cell in a Grid
https://www.hackerrank.com/challenges/ctci-connected-cell-in-a-grid
"""
import unittest
def getBiggestRegion(grid):
dx = [-1, -1, 0, 1, 1, 1, 0, -1]
dy = [0, -1, -1, -1, 0, 1, 1, 1]
max_x = len(grid[0])
max_y = len(grid)
max_region = 0
visited = [[0] * max_x for _ in range(max_y)]
for row in range(max_y):
for col in range(max_x):
if visited[row][col] == 1:
continue
if grid[row][col] == 0:
continue
stk = [(row, col)]
visited[row][col] = 1
region_count = 0
while len(stk) > 0:
y, x = stk.pop(-1)
region_count += 1
if region_count > max_region:
max_region = region_count
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= max_x:
continue
if ny < 0 or ny >= max_y:
continue
if visited[ny][nx] != 0:
continue
if grid[ny][nx] == 1:
visited[ny][nx] = 1
stk.append((ny, nx))
return max_region
if __name__ == '__main__':
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(getBiggestRegion(grid))
class TestBiggestRegion(unittest.TestCase):
def test_get_biggest_region(self):
grid = [
[1, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[1, 0, 0, 0]
]
self.assertEqual(
5,
getBiggestRegion(grid)
)
def test_one_region(self):
grid = [
[0, 0, 1, 1],
[0, 0, 1, 0],
[0, 1, 1, 0],
[0, 1, 0, 0],
[1, 1, 0, 0]
]
self.assertEqual(
8,
getBiggestRegion(grid)
)
def test_diagonal_region(self):
grid = [
[1, 1, 1, 0, 1],
[0, 0, 1, 0, 0],
[1, 1, 0, 1, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 1, 0]
]
self.assertEqual(
9,
getBiggestRegion(grid)
)