One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.
Each input file contains one test case. For each case, the first line contains 4 positive integers: M, N, L and T, where M and N are the sizes of each slice (i.e. pixels of a slice are in an M×N matrix, and the maximum resolution is 1286 by 128); L(≤60) is the number of slices of a brain; and T is the integer threshold (i.e. if the volume of a connected core is less than T, then that core must not be counted).
Then L slices are given. Each slice is represented by an M×N matrix of 0’s and 1’s, where 1 represents a pixel of stroke, and 0 means normal. Since the thickness of a slice is a constant, we only have to count the number of 1’s to obtain the volume. However, there might be several separated core regions in a brain, and only those with their volumes no less than T are counted. Two pixels are connected and hence belong to the same region if they share a common side, as shown by Figure 1 where all the 6 red pixels are connected to the blue one.
For each case, output in a line the total volume of the stroke core.
# 读入数据与数据结构定义 M, N, L, T = list(map(int, input().split())) three_d_image = [[Nonefor _ in range(M)] for _ in range(L)] visited = [[[Falsefor _ in range(N)] for _ in range(M)] for _ in range(L)] ans = 0 for i in range(L): for j in range(M): condition = list(map(int, input().split())) three_d_image[i][j] = condition # 开始 BFS for i in range(L): for j in range(M): for k in range(N): # 出现了一个新块 if three_d_image[i][j][k] == 1andnot visited[i][j][k]: count = 0 stk =  stk.append([i, j, k]) visited[i][j][k] = True while stk: old = stk.pop() count += 1 # 使用zip来遍历六个方向上的点 for increment in zip([1, 0, 0, -1, 0, 0], [0, 1, 0, 0, -1, 0], [0, 0, 1, 0, 0, -1]): new = [x + y for x, y in zip(increment, old)] # 判断这个邻居坐标在图像范围内，没有被访问过，同时状况是1 if0 <= new < L and0 <= new < M and0 <= new < N and three_d_image[new][new][ new] == 1andnot visited[new][new][new]: visited[new][new][new] = True stk.append([new, new, new]) if count >= T: ans += count print(ans)