PAT_A1091 # Acute Stroke BFS (Breadth First Search)

Source:

PAT A1091 Acute Stroke (30 分)

Description:

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, Land 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 (≤) 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.

figstroke.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 0

Sample Output:

26

Keys:

Code:

. 1  / * 
2  Time: 2019-07-03 14:33:07
 . 3  problem: Acute # PAT_A1091 the Stroke
 . 4  the AC: 36:38
 . 5  
. 6  subject to the effect:
 7  gives the brain slice layers, a continuous risk of illness and disease For the number of regions, calculating the number of diseased area
 8  input:
 9  given in the first line, the length of the layers M <= 1286 and the width N <= 128, the number of layers L <= 60, diseased threshold value T (continuous lesion area > sick when T =)
 10  next L lines, give information layers, a lesion, 0 normal
 11  output:
 12  number of diseased lesion area of the print
 13 is  
14  basic ideas:
 15  the BFS statistically valid number of nodes in each block
 16  * / 
. 17 #include <cstdio>
 18 is #include <Queue>
 . 19  the using  namespace STD;
 20 is  const int M=1300,N=130,L=65;
21 int l,m,n,T,ans=0,data;
22 struct node
23 {
24     int data;
25     int state;
26     int x,y,z;
27     node() {}
28     node (int _z,int _x,int _y,int _state,int _data):
29         z(_z),x(_x),y(_y),state(_state),data(_data) {}
30 }image[L][M][N];
31 int X[6]={0,0,0,0,1,-1},Y[6]={0,0,1,-1,0,0},Z[6]={1,-1,0,0,0,0};
32 
33 void BFS(node s)
34 {
35     image[s.z][s.x][s.y].state=0;
36     queue<node> q;
37     q.push(s);
38     int cnt=0;
39     while(!q.empty())
40     {
41         s = q.front();
42         q.pop();
43         cnt++;
44         for(int i=0; i<6; i++)
45         {
46             int nowX=s.x+X[i];
47             int nowY=s.y+Y[i];
48             int nowZ=s.z+Z[i];
49             if(nowX<0 || nowX>=m || nowY<0 || nowY>=n || nowZ<0 || nowZ>=l)
50                 continue;
51             node t = image[nowZ][nowX][nowY];
52             if(t.state==1 && t.data==1){
53                 q.push(t);
54                 image[t.z][t.x][t.y].state=0;
55             }
56         }
57     }
58     if(cnt>=T)
59         ans+=cnt;
60 }
61 
62 int main()
63 {
64 #ifdef ONLINE_JUDGE
65 #else
66     freopen("Test.txt", "r", stdin);
67 #endif // ONLINE_JUDGE
68 
69     scanf("%d%d%d%d", &m,&n,&l,&T);
70     for(int i=0; i<l; i++)
71         for(int j=0; j<m; j++)
72             for(int k=0; k<n; k++){
73                 scanf("%d", &data);
74                 image[i][j][k]=node(i,j,k,1,data);
75             }
76     for(int i=0; i<l; i++)
77         for(int j=0; j<m; j++)
78             for(int k=0; k<n; k++)
79                 if(image[i][j][k].state==1 && image[i][j][k].data==1)
80                     BFS(image[i][j][k]);
81     printf("%d", ans);
82 
83     return 0;
84 }

 

Guess you like

Origin www.cnblogs.com/blue-lin/p/11126687.html