PAT_A1091#Acute Stroke 广度优先搜索(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: PAT_A1091#Acute Stroke
 4 AC: 36:38
 5 
 6 题目大意:
 7 给出脑部各层的切片,患病几率与连续病变区域的数量有关,计算患病区域数量
 8 输入:
 9 第一行给出,各层的长度M<=1286和宽度N<=128,层数L<=60,患病阈值T(连续病变区域>=T时患病)
10 接下来L行,给出各层的信息,1病变,0正常
11 输出:
12 打印患病的病变区域数量
13 
14 基本思路:
15 BFS统计各个块的有效结点数
16 */
17 #include<cstdio>
18 #include<queue>
19 using namespace std;
20 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 }

猜你喜欢

转载自www.cnblogs.com/blue-lin/p/11126687.html