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 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

 

代码:

(DFS:只有25分,测试点4、5段错误)

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
//DFS写法<br>#include<stdio.h>
#include<iostream>
using namespace std;
int m, n, l, t, v = 0, ans = 0;
bool a[80][1300][130], visited[80][1300][130]={0};
int L[] = {1, -1, 0, 0, 0, 0};
int M[] = {0, 0, 1, -1, 0, 0};
int N[] = {0, 0, 0, 0, 1, -1};
void dfs(int ll, int mm, int nn){
    bool flag = 0;
    for(int w = 0; w < 6; w++){
        int tl = ll + L[w];
        int tm = mm + M[w];
        int tn = nn + N[w];
        if(tl <= l && tm <= m && tn <= n &&
           tl >= 0 && tm >= 0 && tn >= 0 &&
           a[tl][tm][tn] == 1 && visited[tl][tm][tn] == 0){
            visited[tl][tm][tn] = 1;
            v++;
            flag = 1;
            // cout<<tl<<" "<<tm<<" "<<tn<<endl;
            dfs(tl, tm, tn);
        }
    }
    if(!flag){
        return;
    }
}
int main(){
    scanf("%d%d%d%d", &m, &n, &l, &t);
    for(int i = 0; i < l; i++){
        for(int j = 0; j < m; j++){
            for(int k = 0; k < n; k++){
                cin>>a[i][j][k];
            }
        }
    }
    for(int i = 0; i < l; i++){
        for(int j = 0; j < m; j++){
            for(int k = 0; k < n; k++){
                if(a[i][j][k] == 1 && visited[i][j][k] == 0){
                    visited[i][j][k] = 1;
                    v++;
                    dfs(i, j, k);
                    if(v >= t){
                        ans += v;
                    }
                    v = 0;
                }
            }
        }
    }
    cout<<ans<<endl;
}

 

(BFS:满分)

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
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int m, n, l, t;
bool a[80][1300][130], visited[80][1300][130]={0};
int L[] = {1, -1, 0, 0, 0, 0};
int M[] = {0, 0, 1, -1, 0, 0};
int N[] = {0, 0, 0, 0, 1, -1};
struct node{
    int x, y, z; 
};
int bfs(int x, int y, int z){
    int cnt = 0;
    node temp;
    temp.x = x, temp.y = y, temp.z = z;
    queue<node> q;
    q.push(temp);
    visited[x][y][z] = true;
    while(!q.empty()){
        node top = q.front();
        q.pop();
        cnt++;
        for(int w = 0; w < 6; w++){
            int tl = top.x + L[w];
            int tm = top.y + M[w];
            int tn = top.z + N[w];
            if(tl < l && tm < m && tn < n &&
               tl >= 0 && tm >= 0 && tn >= 0 &&
               a[tl][tm][tn] == 1 && visited[tl][tm][tn] == 0){
                visited[tl][tm][tn] = true;
                temp.x = tl, temp.y = tm, temp.z = tn;
                q.push(temp);
            }
        }
    }
    if(cnt >= t){
        return cnt;
    }else{
        return 0;
    }
}
int main(){
    scanf("%d%d%d%d", &m, &n, &l, &t);
    for(int i = 0; i < l; i++){
        for(int j = 0; j < m; j++){
            for(int k = 0; k < n; k++){
                cin>>a[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(a[i][j][k] == 1 && visited[i][j][k] == 0){
                    ans += bfs(i, j, k);
                }
            }
        }
    }
    cout<<ans<<endl;
}

 

思路:

使用bfs广度优先搜索代替dfs深度优先搜索从而实现优化。

 

易错点:

1、注意只有volume大于等于t才能计入结果

2、bfs中此处记住是top.x 不是 x(从队列头部元素出发)

int tl = top.x + L[w];
int tm = top.y + M[w];
int tn = top.z + N[w];

3、数组大小开错了(注意数组每一维度是什么及其对应的大小)会导致测试点4,5出错

 

posted @   Yohoc  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示