-
Notifications
You must be signed in to change notification settings - Fork 4
/
irl.py
71 lines (64 loc) · 2.37 KB
/
irl.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
'''Question 1: Given a graph grid of water and land, write a function that
return the number of islands on the grid. An island is a vertically or
horizontally connected region of Land.
'''
from collections import deque
class Problem:
def __init__(self):
pass
def islandCount(self, grid) -> int:
num_islands = 0
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == 'L':
num_islands += 1
grid[row][col] = 'E'
q = deque([(row,col)])
while len(q) > 0:
curr_row, curr_col = q.popleft()
if curr_row < len(grid)-1:
if grid[curr_row+1][curr_col] == 'L':
grid[curr_row+1][curr_col] = 'E'
q.append((curr_row+1, curr_col))
if curr_row > 0:
if grid[curr_row-1][curr_col] == 'L':
grid[curr_row-1][curr_col] = 'E'
q.append((curr_row-1, curr_col))
if curr_col < len(grid[0])-1:
if grid[curr_row][curr_col+1] == 'L':
grid[curr_row][curr_col+1] = 'E'
q.append((curr_row, curr_col+1))
if curr_col > 0:
if grid[curr_row][curr_col-1] == 'L':
grid[curr_row][curr_col-1] = 'E'
q.append((curr_row, curr_col-1))
return num_islands
def main():
sol = Problem()
test_case1 = [
['W', 'W'],
['W', 'W'],
['W', 'W'],
]
test_case2 = [
['L', 'L', 'L'],
['L', 'L', 'L'],
['L', 'L', 'L'],
]
test_case3 = [
['L', 'L', 'L'],
['L', 'W', 'L'],
['L', 'L', 'L'],
]
test_case4 = [
['L', 'W', 'L'],
['W', 'L', 'W'],
['L', 'W', 'L'],
]
print(sol.islandCount(test_case1))
print(sol.islandCount(test_case2))
print(sol.islandCount(test_case3))
print(sol.islandCount(test_case4))
if __name__ == "__main__":
print('IRL')
main()