PAT_1091

1091 Acute Stroke

题目描述

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×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 connectedand 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.

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

思路

开始使用的是dfs,类似于dfs求解连通块的算法,但是发现dfs无论怎么修改后面两组数据总是出现段错误,但是计算后面的内存使用情况也没有超过64M,所以打算换用bfs求解,使用bfs可以得到正确结果

dfs程序只得到了25分,并没有满分,如果有修改并可以通过的地方,感觉大家的指点

程序

bfs方法(30分)

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
#include <iostream>
#include <initializer_list>
#include <algorithm>
#include <string>
#include <string.h>
#include <queue>


using namespace std;
const int maxn = 1286;
const int maxm = 128;
const int maxd = 60;
int m,n,l,k;
int mapp[maxd][maxn][maxm];
bool flag[maxd][maxn][maxm];
int dx[] = {0,1,0,-1,0,0};
int dy[] = {1,0,-1,0,0,0};
int dz[] = {0,0,0,0,1,-1};
int tx,ty,tz;
struct Node
{
int x,y,z;
};

int bfs(int x,int y,int z)
{
Node tmp;
int num = 0;
queue<Node> que;
que.emplace(Node{x,y,z});
while(!que.empty())
{
tmp = que.front();que.pop();
num ++;
for(int i = 0 ;i < 6;i ++)
{
tx = tmp.x + dx[i];ty = tmp.y + dy[i];tz = tmp.z + dz[i];
if(tx >= 0 && tx < l && ty >= 0 && ty < m && tz >= 0 && tz < n && !flag[tx][ty][tz] && mapp[tx][ty][tz])
{
flag[tx][ty][tz] = 1;
que.emplace(Node{tx,ty,tz});
}
}
}
return num>=k?num:0;
}

int main()
{
memset(flag,0,sizeof(flag));
scanf("%d%d%d%d",&m,&n,&l,&k);
for(int i = 0;i < l;i ++)
for(int j = 0;j < m;j ++)
for(int z = 0;z < n;z ++)
scanf("%d",&mapp[i][j][z]);
int ans = 0;
for(int i = 0;i < l;i ++)
for(int j = 0;j < m;j ++)
for(int z = 0;z < n;z ++)
if(!flag[i][j][z] && mapp[i][j][z])
{
flag[i][j][z] = 1;
ans += bfs(i,j,z);
}

printf("%d\n",ans);
return 0;
}

dfs方法(25分)

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
#include <iostream>
#include <initializer_list>
#include <algorithm>
#include <string>

using namespace std;
const int maxn = 1290;
const int maxm = 130;
const int maxd = 65;
int m,n,l,k;
int mapp[maxd][maxn][maxm];
bool flag[maxd][maxn][maxm];
int dx[] = {0,1,0,-1,0,0};
int dy[] = {1,0,-1,0,0,0};
int dz[] = {0,0,0,0,1,-1};
int tx,ty,tz;
int num;

void dfs(int x,int y,int z)
{
for(int i =0 ;i < 6;i ++)
{
tx = x + dx[i];ty = y + dy[i];tz = z + dz[i];

if( tx >= 0 && tx < l && ty >= 0 && ty < m && tz >= 0 && tz < n && !flag[tx][ty][tz] && mapp[tx][ty][tz])
{
// cout << x << " * " << y << " * "<< z << " * " << tx << " "<< ty << " "<< tz << endl;
flag[tx][ty][tz] = 1;
num ++;
dfs(tx,ty,tz);
}
}
return ;
}

int main()
{
scanf("%d%d%d%d",&m,&n,&l,&k);
for(int i = 0;i < l;i ++)
for(int j = 0;j < m;j ++)
for(int z = 0;z < n;z ++){
scanf("%d",&mapp[i][j][z]);
}
int ans = 0;
for(int i = 0;i < l;i ++)
for(int j = 0;j < m;j ++)
for(int z = 0;z < n;z ++)
if(!flag[i][j][z] && mapp[i][j][z])
{
num = 1;
flag[i][j][z] = 1;
dfs(i,j,z);
// cout << " num = "<< num << endl;
if(num >= k)
ans += num;
}

printf("%d\n",ans);
return 0;
}
-------------本文结束感谢您的阅读-------------
显示 Gitment 评论