A1091. 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.
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 <stdio.h> 2 #include <stdlib.h> 3 #include <iostream> 4 #include <string.h> 5 #include <math.h> 6 #include <algorithm> 7 #include <string> 8 #include <stack> 9 #include <queue> 10 using namespace std; 11 struct node{ 12 int x,y,z; 13 }Node; 14 int n,m,slice,T; 15 int pixel[1290][130][61]; 16 int inq[1290][130][61]={false}; 17 int X[6]={0,0,0,0,1,-1}; 18 int Y[6]={0,0,1,-1,0,0}; 19 int Z[6]={1,-1,0,0,0,0}; 20 bool judge(int x,int y,int z)//判断是否有访问的必要 ,inq表示是否入队列,如果可以访问,就入队 21 { 22 if(x>=m||x<0||y>=n||y<0||z>=slice||z<0)return false; 23 if(pixel[x][y][z]==0||inq[x][y][z]==true)return false; 24 return true; 25 } 26 27 int BFS(int x,int y,int z)//进行宽度优先搜索,目的是搜索出相连的一块1 28 { 29 int total=0;//当前块数为0; 30 queue<node> Q;//定义当前队列; 31 Node.x=x;Node.y=y;Node.z=z; 32 Q.push(Node); 33 inq[x][y][z]=true; 34 while(!Q.empty()) 35 { 36 node top=Q.front(); 37 Q.pop(); 38 total++; 39 for(int i=0;i<6;i++) 40 { 41 int newx=top.x+X[i]; 42 int newy=top.y+Y[i]; 43 int newz=top.z+Z[i]; 44 if(judge(newx,newy,newz)==true) 45 { 46 Node.x=newx; 47 Node.y=newy; 48 Node.z=newz; 49 Q.push(Node); 50 inq[newx][newy][newz]=true; 51 } 52 } 53 54 } 55 if(total>=T) return total; 56 else return 0; 57 58 } 59 int main(){ 60 scanf("%d %d %d %d",&m,&n,&slice,&T); 61 for(int z=0;z<slice;z++) 62 { 63 for(int x=0;x<m;x++) 64 { 65 for(int y=0;y<n;y++) 66 { 67 scanf("%d",&pixel[x][y][z]); 68 } 69 70 } 71 } 72 int ans=0; 73 for(int z=0;z<slice;z++) 74 { 75 for(int x=0;x<m;x++) 76 { 77 for(int y=0;y<n;y++) 78 { 79 if(pixel[x][y][z]==1&&inq[x][y][z]==false) 80 { 81 ans+=BFS(x,y,z); 82 } 83 } 84 85 } 86 } 87 printf("%d\n",ans); 88 return 0; 89 }