[英]2D matrix containing only 0s and 1s - python
With a given 2D matrix containing only 0s and 1s.给定的二维矩阵只包含 0 和 1。 Each 0 represents land, and each 1 represents part of a river.每个 0 代表土地,每个 1 代表河流的一部分。 A river consists of any number of 1s that are either horizontally or vertically adjacent (but not diagonally adjacent).一条河流由任意数量的 1 组成,这些 1 水平或垂直相邻(但不是对角相邻)。 The number of adjacent 1s forming a river determine its size.形成一条河流的相邻 1 的数量决定了它的大小。 Write a function that returns an array of the sizes of all rivers represented in the input matrix.编写一个 function,它返回一个数组,该数组包含输入矩阵中表示的所有河流的大小。
Input:
array =[
[0, 0, 1, 0, 1, 0],
[1, 0, 0, 1, 1, 0],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 1, 1],
]
output:
There are 4 rivers in the given matrix, and their sizes are: [1, 3, 3, 4]
but actually im expecting to have: [1, 2, 2, 2, 3, 3]但实际上我希望拥有: [1, 2, 2, 2, 3, 3]
because im looking for only rivers which are horizontally or vertically adjacent but not a river which is both.因为我只寻找水平或垂直相邻的河流,而不是两者兼而有之的河流。 (as my algorith output brings). (正如我的算法 output 带来的)。
MY algorithm uses DFS:我的算法使用 DFS:
def riverSizes(matrix):
sizes = [] # # It would contain all of our river sizes
visited = [[False for value in row] for row in matrix]
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if visited[i][j]:
continue
traverseNode(i, j, matrix, visited, sizes)
return sizes
def traverseNode(i, j, matrix, visited, sizes):
currentRiverSize = 0
nodesToExplore = [[i, j]]
while len(nodesToExplore):
currentNode = nodesToExplore.pop()
i = currentNode[0]
j = currentNode[1]
if visited[i][j]:
continue
visited[i][j] = True
if matrix[i][j] == 0:
continue
currentRiverSize += 1
unvisitedNeighbors = getUnvisitedNeighbors(i, j, matrix, visited)
for neighbor in unvisitedNeighbors:
nodesToExplore.append(neighbor)
if currentRiverSize > 0:
sizes.append(currentRiverSize)
def getUnvisitedNeighbors(i, j, matrix, visited):
unvisitedNeighbors = []
if i > 0 and not visited[i - 1][j]:
unvisitedNeighbors.append([i - 1, j])
if i < len(matrix) - 1 and not visited[i + 1][j]:
unvisitedNeighbors.append([i + 1, j])
if j > 0 and not visited[i][j - 1]:
unvisitedNeighbors.append([i, j - 1])
if j < len(matrix[0]) - 1 and not visited[i][j + 1]:
unvisitedNeighbors.append([i, j + 1])
return unvisitedNeighbors
how can i fix it?我该如何解决?
The algorithm is giving you the right result for the problem [1, 3, 3, 4].该算法为您提供了问题 [1, 3, 3, 4] 的正确结果。 A river is composed of 1s that are horizontally or vertically adjacent to each other (a 1 can not be horizontally and vertically adjacent to another 1 at the same time, in the case the confusion is the 'either' 'or' of the problem statement)).一条河流由水平或垂直相邻的 1 组成(一个 1 不能同时与另一个 1 水平和垂直相邻,在这种情况下混淆是问题陈述的 'either' 'or' ))。 That is why getUnvisitedNeighbors looks into all vertical and horizontal directions from a field containing a 1. In other words, a river could have an L shape resulting matrix这就是为什么 getUnvisitedNeighbors 从包含 1 的字段中查看所有垂直和水平方向的原因。换句话说,河流可能有一个 L 形状的结果矩阵
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.