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.
Input Specification:
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 by 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 by 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.
Figure 1
Output Specification:
For each case, output in a line the total volume of the stroke core.
Sample Input:
3 4 5 2 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 0 1 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0
Sample Output:
26
1 #include <iostream> 2 #include <algorithm> 3 #include <queue> 4 using namespace std; 5 const int LEN = 1290, WID = 130, HIG = 61; 6 int space[LEN][WID][HIG], M, N, L, T; 7 bool inq[LEN][WID][HIG];//值初始化为false 8 struct Node{ 9 int x, y, z; 10 }; 11 queue<Node> Q; 12 //增量数组 13 int incX[6] = {1, -1, 0, 0, 0, 0}; 14 int incY[6] = {0, 0, 1, -1, 0, 0}; 15 int incZ[6] = {0, 0, 0, 0, 1, -1}; 16 17 bool inBound(Node x){ 18 if(x.x >= 0 && x.x < M && x.y >= 0 && x.y < N && x.z >= 0 && x.z < L) return true; 19 else return false; 20 } 21 22 int bfs(int x, int y, int z){ 23 int ret = 0; 24 Node t = {x, y, z}; 25 Q.push(t); inq[x][y][z] = true; 26 while(!Q.empty()){ 27 t = Q.front(); Q.pop(); 28 ++ ret; 29 for(int i = 0; i < 6; ++ i){//6个方向 30 Node next; 31 next.x = t.x + incX[i]; next.y = t.y + incY[i]; next.z = t.z + incZ[i]; 32 if(inBound(next) && !inq[next.x][next.y][next.z] && space[next.x][next.y][next.z] == 1){ 33 //边界判断一定要放在前面前, 否则出现段错误 34 //这是一段错误的易发地点,切记 35 //形如while(i >= 0 && s[i--]); 36 Q.push(next); inq[next.x][next.y][next.z] = true; 37 } 38 } 39 } 40 return ret; 41 } 42 43 int main() 44 { 45 scanf("%d%d%d%d", &M, &N, &L, &T); 46 for(int k = 0; k < L; ++ k){//层号 -----注意这里的顺序 47 for(int i = 0; i < M; ++ i){//行号 48 for(int j = 0; j < N; ++ j){//列号 49 scanf("%d", &space[i][j][k]); 50 } 51 } 52 } 53 //本题采用dfs会爆栈 54 int ans = 0; 55 for(int k = 0; k < L; ++ k){ 56 for(int i = 0; i < M; ++ i){ 57 for(int j = 0; j < N; ++ j){ 58 if(!inq[i][j][k] && space[i][j][k] == 1){ 59 int ret = bfs(i, j, k); 60 if(ret >= T) ans += ret; 61 } 62 } 63 } 64 } 65 printf("%d", ans); 66 return 0; 67 }