优化Python洪水填充函数

2024-10-01 17:26:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在二维数字数组中消除间隙,但代码运行缓慢(3-3.5秒),我想让它更快(1秒)

    spots = ((-1, 0), (0, -1), (0, 1), (1, 0))
    def floodFill(x, y):

        global cells

        que = []
        area = []
        areaType = cells[x][y]

        que.append([x, y])

        while len(que) > 0:
            area.append(que[0])
            for spot in spots:
                X = que[0][0] + spot[0]
                Y = que[0][1] + spot[1]

                if -1 < X < width and -1 < Y < height:
                    if cells[X][Y] == areaType:
                        if not [X, Y] in que and not [X, Y] in area:
                            que.append([X, Y])

            que.pop(0)

        return area, areaType

我已经尝试了我能想到的一切,需要帮助。谢谢


Tags: andinifnot数字area数组que

热门问题