加快充水

对于我的任务,我必须创建洪水填充功能,以供以后在扫雷游戏中使用。这是一个练习。

这是我的代码:

def floodfill(planet, x1, y1):

    width = len(planet[0])
    height = len(planet)
    list = [(y1, x1)]
    if planet[y1][x1] == " ":
        for y, x in list:
            planet[y][x] = "0"
            list.pop()
            for x2 in range(min(max(x - 1, 0), width), min(max(x + 2, 0), width)):
                for y2 in range(min(max(y - 1, 0), height), min(max(y + 2, 0), heigth)):
                    if planet[y2][x2] == " ":
                        list.append((y2, x2))

执行时间太长。我怎样才能使其更快?也许有一个我看不到的错误?

编辑:

planet = [
[" ", " ", " ", "x", " ", " ", " ", " ", " ", " ", " ", "x", " "], 
[" ", " ", "x", "x", " ", " ", " ", "x", " ", " ", " ", "x", " "], 
[" ", "x", "x", " ", " ", " ", " ", "x", " ", " ", "x", "x", " "], 
["x", "x", "x", "x", "x", " ", " ", "x", " ", "x", " ", " ", " "], 
["x", "x", "x", "x", " ", " ", " ", " ", "x", " ", "x", " ", " "], 
[" ", " ", "x", " ", " ", " ", " ", " ", " ", "x", " ", " ", " "]
]

或类似的检查程序

   0 1 2 3 4
 0   x   x x
 1     x x  
 2 x        
 3 x x   x  
 4 x x x    
 5   x x x x

结果:

0 x 0 x x
0 0 x x 0
x 0 0 0 0
x x 0 x 0
x x x 0 0
  x x x x

我的代码给出:

0 x 0 x x
0 0 x x  
x 0     0
x x 0 x 0
x x x 0  
  x x x x

在给定的(x,y)下,应该像扫雷游戏一样用0填充空白。