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
? ? ? 識別急性腦卒中(急性腦卒中)的一個重要因素是腦卒中核的體積。根據圖像分析的結果,在每個MRI切片中識別核心區域,你的工作就是計算中風核心的體積。
? ? ?每個輸入文件包含一個測試用例。對于每一種情況,第一行包含4個正整數:m、n、l和t,其中m和n是每片的大小(即一片像素在m乘n矩陣中,最大分辨率為1286乘128);l(<=60)是腦片的數目;t是整數閾值(如果:連接核的體積小于t,那么該核就不能被計算)。然后給出l片。每片用0和1的n矩陣表示,其中1表示筆畫的像素,0表示正態。由于切片的厚度是常數,我們只需計算1的數目就可以得到體積。然而,一個大腦中可能有幾個分離的核心區域,只有那些體積不小于t的區域才會被計數。如果它們有共同的一面,兩個像素是“連接”的,因此屬于同一個區域,如圖1所示,所有6個紅色像素都連接到藍色像素。
?
#include<cstdio> #include<queue> using namespace std;struct Node{int x,y,z; }node; int pixel[1290][130][61]; bool inq[1290][130][61] = {false};int n,m,L,T;int X[6] = {0,0,0,0,1,-1}; int Y[6] = {0,0,1,-1,0,0}; int Z[6] = {1,-1,0,0,0,0}; bool judge(int x, int y, int z){if(x >= n || x < 0 || y >= m || y < 0 || z >= L || z < 0) //x,y,z需要取到n,m,L 從零開始 return false;if(inq[x][y][z] == true || pixel[x][y][z] == 0) //if return fals if return truereturn false;return true; } int BFS(int x,int y,int z){int tot = 0;node.x = x, node.y = y, node.z = z;queue<Node> q; //q的類型是Node q.push(node);inq[x][y][z] = true;while(!q.empty()){Node top = q.front(); //top類型是Node q.pop();tot++;for(int i = 0; i < 6; i++){int newX = top.x + X[i]; //top x,y,z加上方向增量 int newY = top.y + Y[i];int newZ = top.z + Z[i];if(judge(newX,newY,newZ)){node.x = newX,node.y = newY,node.z = newZ;q.push(node);inq[newX][newY][newZ] = true; //對新節點new入隊 2 }}}if(tot >= T) return tot; //要取到等于 else return 0; } int main(){scanf("%d%d%d%d",&n,&m,&L,&T);for(int z = 0; z < L; z++){for(int x = 0; x < n; x++){for(int y = 0; y < m; y++){scanf("%d",&pixel[x][y][z]);}}}int ans = 0;for(int z = 0; z < L; z++){for(int x = 0; x < n; x++){for(int y = 0; y < m; y++){if(pixel[x][y][z] == 1 && inq[x][y][z] == false)ans += BFS(x,y,z);}}}printf("%d\n",ans);return 0; }
?