假设有一个数组
position = [
[1,3,2,3,5],
[3,4,5,6,3],
[2,7,4,3,3],
[5,2,2,3,1],
]
制高点是可以一直向下走到东西南北四条边的,向下走的过程不能爬坡,只能走比当前点低的点或者跟当前点一样高的点。比如7就是制高点。
position = [
[1,3,2,3,5],
[3,4,5,6,3],
[2,7,4,3,3],
[5,2,2,3,1],
]
def bfs(set,m,n,matrix):
# 每一个运动的方向
dir = [[0,1],[1,0],[0,-1],[-1,0]]
# set用来保留结果,因此需要一个新的队列来循环
quene = list(set)
while len(quene) > 0:
x,y = quene.pop()
for d in dir:
nx = x + d[0]
ny = y + d[1]
# 判断是否越界
if 0 <= nx and nx < m and 0<=ny and ny<n:
# 是否符合调用规则
if matrix[nx][ny]>=matrix[x][y]:
if (nx,ny) not in set:
quene.append((nx,ny))
set.add((nx,ny))
def solve(matrix):
if not matrix:
return False
m = len(matrix)
n = len(matrix[0])
topPonit = set([(0,y) for y in range(n)])
leftPonit = set([(x, 0) for x in range(m)])
bottomPonit = set([(m-1, y) for y in range(n)])
rightPonit = set([(x, n-1) for x in range(m)])
bfs(topPonit,m,n,matrix)
bfs(leftPonit, m, n, matrix)
bfs(bottomPonit,m,n,matrix)
bfs(rightPonit,m,n,matrix)
print(topPonit)
print(leftPonit)
print(bottomPonit)
print(rightPonit)
return topPonit & leftPonit & bottomPonit & rightPonit
s = solve(position)
print(s)