Python 矩阵深度优先搜索

问题描述

这里有一个程序中的函数,它遍历矩阵以找到低于其中心值的值,如果找不到较低的值,则其中心值增加 1。 它从矩阵的中心开始,如果任何相邻单元格的值低于该值(不包括对角线),它就会环顾该单元格,如果找到较低的值,则将该单元格值设置为 99999 并使其居中单元格并再次以原始中心运行价值。 程序对于低阶矩阵运行良好(我已经尝试并验证了高达 7x7),但对于较大的矩阵阶值,它会产生分段错误。 它与我在最后一个 if 循环中调用的 recrsuion 密切相关,任何人都可以帮助我纠正这个问题。 矩阵是随机输入的奇数矩阵

def waterRegion(grid,r,c,level,limit)
grid[r][c] = 99999
flag = 0
r1 = r + 1
c1 = c + 1
for row in range((r - 1),(r + 2)):
    if (row <= limit and row >= 0):
        for column in range((c - 1),(c + 2)):
            if (column <= limit and column >= 0):
                if ((row <= r - 1) and (column <= c - 1 or column >= c + 1)) or (
                        (row >= r + 1) and (column <= c - 1 or column >= c + 1)):
                    continue
                if int(grid[row][column]) < level:
                    flag = 1
                    waterRegion(grid,row,column,limit)
            else:
                return grid
    else:
        return grid
if ((row == r1) and (column == c1)):
    if flag == 0:
        level = level + 1
        waterRegion(grid,r1 - 1,c1 - 1,limit)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)