给定一个由 '1'
(陆地)和 '0'
(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
示例 1:
输入: 11110 11010 11000 00000 输出: 1
示例 2:
输入: 11000 11000 00100 00011 输出: 3
在上一节队列的基础上,使用BFS广度优先搜索实现
1. 找到1个1,将其坐标入队
2. 检查起上下左右,将其中为1的坐标入队,并将该坐标出队
3. 依次检查队首的点的上下左右,将其中为1的入队,并将该坐标出队
4. 当队列为空时,即找到一个岛
从(0,0)开始找,
(0,0)----->
|11110
|11010
|11000
↓00000
队列实现如下
class MyCircularQueue:
def __init__(self, k: int): #and
"""
Initialize your data structure here. Set the size of the queue to be k.
"""
self.__length = k
self.queue = [0] * k
self.__head = 0
self.__tail = -1
self.__count = 0
def enQueue(self, value) -> bool:
"""
Insert an element into the circular queue. Return true if the operation is successful.
"""
if self.isFull() == False:
if self.__tail == self.__length-1: #尾指到最后一个空间了
self.__tail = 0
else:
self.__tail += 1
self.queue[self.__tail] = value
self.__count += 1
return True
else:
return False
def deQueue(self) -> bool:
"""
Delete an element from the circular queue. Return true if the operation is successful.
"""
if self.isEmpty() == False :
self.queue[self.__head] = 0
self.__count -= 1
if self.__head != self.__tail:
if self.__head == self.__length-1:
self.__head = 0
else:
self.__head += 1
else:
self.__head = 0
self.__tail = -1
return True
else:
return False
def Front(self) -> int:
"""
Get the front item from the queue.
"""
if self.isEmpty():
return -1
else:
return self.queue[self.__head]
def Rear(self) -> int:
"""
Get the last item from the queue.
"""
if self.isEmpty():
return -1
else:
return self.queue[self.__tail]
def isEmpty(self) -> bool:
"""
Checks whether the circular queue is empty or not.
"""
if self.__count == 0:
return True
else:
return False
def isFull(self) -> bool:
"""
Checks whether the circular queue is full or not.
"""
if self.__count == self.__length:
return True
else:
return False
题解如下
class Solution:
def numIslands(self, grid) -> int:
x = len(grid) # 行数
if x == 0:
return 0
y = len(grid[0]) #列数
q = MyCircularQueue(20)
i, j, temp_x, temp_y = 0, 0, 0, 0
nums = 0
while i < x:
while j < y:
# try:
if grid[i][j] == "1":
q.enQueue((i,j))
grid[i][j] = "0"
while q.isEmpty() == False:
temp_x, temp_y = q.Front()
# print("q_front:", temp_x, ",", temp_y)
if temp_x > 0 :
if grid[temp_x-1][temp_y] == "1":
q.enQueue((temp_x-1,temp_y))
grid[temp_x-1][temp_y] = "0"
if temp_x < x-1:
if grid[temp_x+1][temp_y] == "1":
q.enQueue((temp_x+1,temp_y))
grid[temp_x+1][temp_y] = "0"
if temp_y > 0 :
if grid[temp_x][temp_y-1] == "1":
q.enQueue((temp_x,temp_y-1))
grid[temp_x][temp_y-1] = "0"
if temp_y < y-1:
if grid[temp_x][temp_y+1] == "1":
q.enQueue((temp_x,temp_y+1))
grid[temp_x][temp_y+1] = "0"
q.deQueue()
nums += 1
# except:
# print("Error:", i, ",", j)
j += 1
j = 0
i += 1
return nums
test = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
test1 = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
test2 = []
test3 = [["1"]]
test4 = [["1"],["1"]]
a = Solution()
rrr = a.numIslands(test1)
print("rrr:",rrr)
'''
11110
11010
11000
00000
'''
'''
11000
11000
00100
00011
'''