python代码全部re求助
查看原帖
python代码全部re求助
596772
suxiaorui楼主2021/12/21 11:09
def bfs(a,b):
    que = [(a,b)]
    while que:
        x,y = que.pop(0)
        g[x][y] = 0
        for i in range(4):
            n_x = x + dx[i]
            n_y = y + dy[i]
            if 0<= n_x < n and 0<= n_y < m and g[n_x][n_y] != 0:
                que.append((n_x,n_y))
if __name__=="__main__":
    n, m = map(int, input().split())
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]
    g = []
    st = []
    res = 0
    for i in range(n):
        g.append(list(map(int, input())))
    for i in range(n):
        for j in range(m):
            if g[i][j] == 0:
                continue
            bfs(i,j)
            res += 1
    print(res)
2021/12/21 11:09
加载中...