1091 Acute Stroke (30)(30 分)

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

\ 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

就是求连通区域,如果连通区域内1的个数少于t就不算再内,计算总的1有多少,数有点大,容易内存超限,好在每个数不是0就是1,是确定的范围,可以选占内存小的数据类型。
bfs求。用dfs最后两个点爆了。。。
代码:
#include <stdio.h>
#include <stdlib.h>
struct pos {
    int x,y,z;
}q[10000],temp;
char mp[60][1300][130],vis[60][1300][130];
int n,m,l,t,all;
int dir[6][3] = {0,0,1,0,1,0,1,0,0,0,0,-1,0,-1,0,-1,0,0};
int bfs(int x,int y,int z) {
    q[0].x = x,q[0].y = y,q[0].z = z;
    int head = 0,tail = 1,sum = 1;
    while(head != tail) {
        for(int i = 0;i < 6;i ++) {
            int tx = q[head].x + dir[i][0];
            int ty = q[head].y + dir[i][1];
            int tz = q[head].z + dir[i][2];
            if(tx < 0 || ty < 0 || tz < 0 || tx >= l || ty >= n || tz >= m)continue;
            if(vis[tx][ty][tz] || !mp[tx][ty][tz])continue;
            vis[tx][ty][tz] = 1;
            sum ++;
            temp.x = tx,temp.y = ty,temp.z = tz;
            q[tail ++] = temp;
            tail %= 10000;
        }
        head ++;
        head %= 10000;
    }
    return sum;
}
int main() {
    scanf("%d%d%d%d",&n,&m,&l,&t);
    for(int i = 0;i < l;i ++) {
        for(int j = 0;j < n;j ++) {
            for(int k = 0;k < m;k ++) {
                scanf("%d",&mp[i][j][k]);
            }
        }
    }
    for(int i = 0;i < l;i ++) {
        for(int j = 0;j < n;j ++) {
            for(int k = 0;k < m;k ++) {
                if(vis[i][j][k] || !mp[i][j][k])continue;
                vis[i][j][k] = 1;
                int sum = bfs(i,j,k);
                if(sum >= t)all += sum;
            }
        }
    }
    printf("%d",all);
}

猜你喜欢

转载自www.cnblogs.com/8023spz/p/9281287.html
今日推荐