连通块问题 (这里用dfs节省一点空间)
dfs对连通块染色
按层,行,列的顺序遍历一遍,发现如果值为1则进入dfs,将所有1染成0,染色数量大于t则记录
python版本
# 急性中风
def dfs(z,x,y):
global res
board[z][x][y] = 0
res += 1
for var in ways:
z_n = z + var[0]
x_n = x + var[1]
y_n = y + var[2]
if z_n >= 0 and z_n < l and x_n >= 0 and x_n < n and y_n >= 0 and y_n < m and board[z_n][x_n][y_n] == 1:
dfs(z_n,x_n,y_n)
n,m,l,t = map(int,input().split())
board = [[0]*(n + 1) for i in range(l + 1)]
ways = [(0,1,0),(0,-1,0),(0,0,1),(0,0,-1),(-1,0,0),(1,0,0)]
for i in range(l):
for j in range(n):
board[i][j] = [int(i) for i in input().split()]
ans = 0
for i in range(l):
for j in range(n):
for k in range(m):
res = 0
if board[i][j][k] == 1:
dfs(i,j,k)
if res >= t:
ans += res
print(ans)