微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python 矩阵深度优先搜索

如何解决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 举报,一经查实,本站将立刻删除。