A1091 Acute Stroke (30 point(s))

BFS应用于三维数组,统计值周围一片值都为1的元素个数

1. 原文

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 Nare 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 (≤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×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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:

1
26

2. 解析

  • 初始化

    1
    2
    visit[i][j][k]=false;
    matrix[i][j][k]=0;
  • 判断边界

    1
    2
    3
    if (x<0||x>=l||y<0||y>=m||z<0||z>=n)
    if (visit[x][y][z])
    if (matrix[x][y][z]==0)
  • BFS

    1
    2
    3
    4
    q.push(node(x,y,z));
    if (judge(nowx,nowy,nowz)){
    q.push(node(nowx,nowy,nowz));
    }

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<cstdio>
#include<queue>
using namespace std;
int matrix[70][1300][130];
bool visit[70][1300][130];
int X[]={1,-1,0,0,0,0};
int Y[]={0,0,1,-1,0,0};
int Z[]={0,0,0,0,1,-1};
int m,n,l,t;
struct node
{
int x,y,z;
node(int X,int Y,int Z): x(X),y(Y),z(Z){};
};
bool judge(int x,int y,int z)
{
if (x<0||x>=l||y<0||y>=m||z<0||z>=n)
{
return false;
}
if (visit[x][y][z])
{
return false;
}
if (matrix[x][y][z]==0)
{
return false;
}
return true;
}
void init()
{
for (int i = 0; i < l; ++i)
{
for (int j = 0; j < m; ++j)
{
for (int k = 0; k < n; ++k)
{
visit[i][j][k]=false;
matrix[i][j][k]=0;
}
}
}
}
int bfs(int x,int y,int z){
int ans=0;
queue<node> q;
q.push(node(x,y,z));
visit[x][y][z]=true;
while(!q.empty()){
node temp=q.front();
ans++;
q.pop();
for (int i = 0; i < 6; ++i)
{
int nowx=temp.x+X[i];
int nowy=temp.y+Y[i];
int nowz=temp.z+Z[i];
if (judge(nowx,nowy,nowz))
{
q.push(node(nowx,nowy,nowz));
visit[nowx][nowy][nowz]=true;
}
}
}
if (ans<t)
{
return 0;
}else{
return ans;
}
}
int main()
{
scanf("%d%d%d%d",&m,&n,&l,&t);
init();
for (int i = 0; i < l; ++i)
{
for (int j = 0; j < m; ++j)
{
for (int k = 0; k < n; ++k)
{
scanf("%d",&matrix[i][j][k]);
}
}
}
int ans=0;
for (int i = 0; i < l; ++i)
{
for (int j = 0; j < m; ++j)
{
for (int k = 0; k < n; ++k)
{
if(visit[i][j][k]==false&&matrix[i][j][k]==1){
ans+=bfs(i,j,k);
}
}
}
}
printf("%d\n", ans);
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1091/
  • 发布时间: 2019-09-03 12:31
  • 更新时间: 2021-10-29 14:06
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!