标签:ble *** ret split spl 示例 一个 pre 计算
大小为N*M的菜园,因为下雨有积水,八连通的积水被认为连在一起构成一个水洼。
计算菜园里有多少个水洼。(1 <= N <= 100; 1 <= M <= 100)
八连通指的是
***
*W*
***
示例:
输入:
N = 10, M = 12
园子如下图(‘W‘ 表示积水, ‘.‘表示没有积水)
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
输出:
3
# 解
a, b = map(int, input().split())
c = []
for i in range(a):
d = map(str, input())
c.append(list(d))
def main(table, n, m):
count = 0
for i in range(n):
for j in range(m):
if table[i][j] == ‘W‘:
dfs(table, i, j, n, m)
count+=1
return count
def dfs(table, x, y, n, m):
table[x][y] = ‘.‘
# 八个方位
for i in range(-1, 2): # -1, 0, 1
for j in range(-1, 2): # -1, 0, 1
if i == 0 == j:
continue
if 0<= x+i <= n-1 and 0 <= y+j <= m-1:
if table[x+i][y+j] == ‘W‘:
dfs(table, x+i,y+j, n, m)
print(main(c, a, b))
标签:ble *** ret split spl 示例 一个 pre 计算
原文地址:https://www.cnblogs.com/EdenWu/p/14394737.html