-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsland.py
More file actions
64 lines (51 loc) · 1.46 KB
/
Island.py
File metadata and controls
64 lines (51 loc) · 1.46 KB
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
import argparse
class Island:
"""
Island class for counting number of islands
"""
def parseArgs(self) -> str:
# Create an ArgumentParser object
parser = argparse.ArgumentParser()
# Add an input file argument
parser.add_argument("input", help="input file")
# Parse the command line arguments
args = parser.parse_args()
# Return the input file name
return args.input
def numIslands(self, grid: list[list[int]]) -> int:
if not grid:
return 0
rows = len(grid)
cols = len(grid[0])
# Recursively calls itself and marks all adjacent cells as visited
def dfs(i, j):
if i < 0 or j < 0 or i >= rows or j >= cols or grid[i][j] == 0:
return
grid[i][j] = 0
dfs(i + 1, j)
dfs(i - 1, j)
dfs(i, j + 1)
dfs(i, j - 1)
dfs(i + 1, j + 1)
dfs(i + 1, j - 1)
dfs(i - 1, j + 1)
dfs(i - 1, j - 1)
count = 0
# Visit each cell in the grid once (Time complexity: O(m * n))
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
count += 1
dfs(i, j)
return count
if __name__ == "__main__":
# Create new island object
island = Island()
input_file = island.parseArgs()
try:
with open(input_file, 'r') as file:
grid = [[int(c) for c in line.strip()] for line in file]
print(island.numIslands(grid))
except FileNotFoundError:
print("Error: input file does not exist")
exit(1)