11class Matrix :
22 """
3- Class to represent a 2D grid as a matrix and count the number of islands.
4- An island is a group of connected 1s (connections can be vertical, horizontal, or diagonal).
3+ Class to represent a 2D binary grid as a matrix and count the number of islands.
4+ An island is a group of connected 1s.
5+ We call cells with 1 "land" and cells with 0 "water".
6+ Connections can be vertical, horizontal, or diagonal.
57 """
68
79 def __init__ (self , row : int , col : int , graph : list [list [bool ]]) -> None :
@@ -44,23 +46,21 @@ def __init__(self, row: int, col: int, graph: list[list[bool]]) -> None:
4446 self .COL = col
4547 self .graph = graph
4648
47-
48-
4949 def is_safe (self , i : int , j : int , visited : list [list [bool ]]) -> bool :
5050 """
5151 Check if a cell (i, j) is "safe".
5252
5353 We consider a cell "safe" if:
5454 - It is within the bounds of the matrix
5555 - It has not been visited yet
56- - It contains land (i.e., value is nonzero )
56+ - It contains land (i.e., value is 1 )
5757
5858 :param i: row index
5959 :param j: column index
6060 :param visitied: 2D list indicating which cells we have visited
6161 :return: True if cell is safe, else False
6262
63- >>> m = Matrix(3, 3, [[1, 0, 0], [0, 0, 1]])
63+ >>> m = Matrix(3, 3, [[1, 0, 0], [0, 0, 1], [0, 0, 0] ])
6464 >>> visited = [[False]*3 for _ in range(3)]
6565
6666 # Within bounds, unvisited, and value is 1 (land)
@@ -71,7 +71,7 @@ def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
7171 >>> m.is_safe(0, 1, visited)
7272 False
7373
74- # Within bounds, already visited
74+ # Within bounds, land, but already visited
7575 >>> visited[0][0] = True
7676 >>> m.is_safe(0, 0, visited)
7777 False
@@ -92,15 +92,15 @@ def is_safe(self, i: int, j: int, visited: list[list[bool]]) -> bool:
9292
9393 def diffs (self , i : int , j : int , visited : list [list [bool ]]) -> None :
9494 """
95- Depth-first search (DFS) to mark all land cells (1s) connected to (i, j) as visited.
95+ Check the 8 cells connected to (i, j) and marks the land cells as visited.
96+
97+ Depth-first search (DFS) to mark land cells connected to (i, j) as visited.
9698
9799 :param i: Row index of current cell
98100 :param j: Column index of current cell
99101 :param visited: 2D list tracking visited cells
100102
101- diffs() checks the 8 cells surrounding the current cell (i, j) and marks the land cells as visited.
102-
103- >>> graph = [[1, 1, 0], [0, 1, 0], [0, 0, 1]]
103+ >>> graph = [[1, 1, 0], [0, 1, 0], [0, 0, 0]]
104104 >>> m = Matrix(3, 3, graph)
105105 >>> visited = [[False] * 3 for _ in range(3)]
106106 >>> m.diffs(0, 0, visited)
@@ -127,16 +127,21 @@ def count_islands(self) -> int:
127127
128128 :return: Number of islands found
129129
130- >>> graph[[1, 1, 0], [0, 1, 0], [1, 0, 0]]
130+ >>> graph = [[1, 1, 0], [0, 1, 0], [1, 0, 0]]
131131 >>> m = Matrix(3, 3, graph)
132132 >>> m.count_islands()
133+ 1
134+
135+ >>> graph1 = [[1, 1, 0], [0, 0, 0], [1, 0, 0]]
136+ >>> m1 = Matrix(3, 3, graph1)
137+ >>> m1.count_islands()
133138 2
134139
135140 >>> graph2 = [[0, 0, 0], [0, 0, 0]]
136141 >>> m2 = Matrix(2, 3, graph2)
137142 >>> m2.count_islands()
138143 0
139-
144+
140145 >>> graph3 = [[1]]
141146 >>> m3 = Matrix(1, 1, graph3)
142147 >>> m3.count_islands()
@@ -148,6 +153,5 @@ def count_islands(self) -> int:
148153 for j in range (self .COL ):
149154 if not visited [i ][j ] and self .graph [i ][j ] == 1 :
150155 self .diffs (i , j , visited )
151- count += 1
156+ count += 1
152157 return count
153-
0 commit comments