1091. Acute Stroke (30)
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.
![](http://www.patest.cn/upload/g0_nf8qpnrr0f1.jpg)
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 0Sample Output:
26
此题的题目理解很是关键,关键是关于题目中的connnected的理解。题目中的每一个M*N的矩阵可以看做一层,然后总共有L层。我们可以将每一个1/0看做一个点,与它相连的就是他的上下左右前后六个方向的点,例如第一个点的坐标为(0,0,0)那么与它相连的有(1,0,0) (0,1,0) (0,0,1)其他的三个方向不存在。那么我们可以将其看做一个三维的图,两个相邻的都为1的就是存在一条边,然后采用DFS算出每一个连通分量中1的个数就是volume,然后就可以得到答案了
1 #include <cstdio> 2 #include <iostream> 3 #include <queue> 4 5 using namespace std; 6 7 int m, n, l; 8 int point[1286][128][60]; 9 10 struct pixel 11 { 12 int x; 13 int y; 14 int z; 15 pixel(int xx, int yy, int zz){ x = xx; y = yy; z = zz; } 16 }; 17 18 int DFS(int x, int y, int z) 19 { 20 int volume = 0; 21 queue<pixel> que; 22 pixel p(x, y, z); 23 point[x][y][z] = 0; 24 que.push(p); 25 while (!que.empty()) 26 { 27 pixel p = que.front(); 28 volume++; 29 que.pop(); 30 if (p.x - 1 >= 0 && point[p.x - 1][p.y][p.z]) 31 { 32 que.push(pixel(p.x - 1, p.y, p.z)); 33 point[p.x - 1][p.y][p.z] = 0; 34 } 35 if (p.x + 1 < m && point[p.x + 1][p.y][p.z]) 36 { 37 que.push(pixel(p.x + 1, p.y, p.z)); 38 point[p.x + 1][p.y][p.z] = 0; 39 } 40 if (p.y - 1 >= 0 && point[p.x][p.y - 1][p.z]) 41 { 42 que.push(pixel(p.x, p.y - 1, p.z)); 43 point[p.x][p.y - 1][p.z] = 0; 44 } 45 if (p.y + 1 < n && point[p.x][p.y + 1][p.z]) 46 { 47 que.push(pixel(p.x, p.y + 1, p.z)); 48 point[p.x][p.y + 1][p.z] = 0; 49 } 50 if (p.z - 1 >= 0 && point[p.x][p.y][p.z - 1]) 51 { 52 que.push(pixel(p.x, p.y, p.z - 1)); 53 point[p.x][p.y][p.z - 1] = 0; 54 } 55 if (p.z + 1 < l && point[p.x][p.y][p.z + 1]) 56 { 57 que.push(pixel(p.x, p.y, p.z + 1)); 58 point[p.x][p.y][p.z + 1] = 0; 59 } 60 } 61 return volume; 62 } 63 64 int main() 65 { 66 int threshold; 67 scanf("%d%d%d%d", &m, &n, &l, &threshold); 68 for (int k = 0; k < l; k++) 69 for (int i = 0; i < m; i++) 70 for (int j = 0; j < n; j++) 71 scanf("%d", &point[i][j][k]); 72 int volume = 0; 73 for (int k = 0; k < l; k++) 74 { 75 for (int i = 0; i < m; i++) 76 { 77 for (int j = 0; j < n; j++) 78 { 79 if (point[i][j][k]) 80 { 81 int vol = DFS(i, j, k); 82 if (vol >= threshold) 83 volume += vol; 84 } 85 } 86 } 87 } 88 printf("%d", volume); 89 }