PAT 1091. Acute Stroke (30)(孤岛问题)

官网

题目

1091. Acute Stroke (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.第一行M,N,L,T,M,N代表每一片的大小,最高是1286×128,L<=60是切片的数量,T是阈值如果connected core的volume小于T,则不用计算。
  • 2.接下来给出L个切片,每个切片的1代表stroke,0代表正常,这里有几个分离的core regions,只计算他们volumes值不小于T的。
  • 3.输出中stroke core 的个数,感觉就是求孤岛的个数,图差评。

解题思路

  • 1.还是孤岛问题,只是换成三维了。

AC代码

#include<iostream>
#include<deque>
#include<fstream>

using namespace std;
//fstream in("1.txt");
//#define cin in
int m, n, l, t;
int slices[1287][130][61], visited[1287][130][61];
int s, sum = 0;
struct node{
    int x, y, z;
    node(){}
    node(int _x, int _y, int _z){
        this->x = _x;
        this->y = _y;
        this->z = _z;
    }
};
void bfs(int r, int c, int lever)
{
    deque<node> dq;
    dq.push_back(node(r, c, lever));
    while (!dq.empty())
    {
        node now;
        now = dq.front();
        visited[now.x][now.y][now.z] = 1;
        dq.pop_front();
        //上下左右前后
        if (now.z + 1 < l && slices[now.x][now.y][now.z + 1] == 1 && visited[now.x][now.y][now.z + 1]==0)
        {
            dq.push_back(node(now.x, now.y, now.z + 1));
            visited[now.x][now.y][now.z + 1] = 1;
            s++;
        }
        if (now.z - 1 >= 0 && slices[now.x][now.y][now.z - 1] == 1 && visited[now.x][now.y][now.z - 1] == 0)
        {
            dq.push_back(node(now.x, now.y, now.z - 1));
            visited[now.x][now.y][now.z - 1] = 1;
            s++;
        }
        if (now.x + 1 < m && slices[now.x + 1][now.y][now.z] == 1 && visited[now.x + 1][now.y][now.z] == 0)
        {
            dq.push_back(node(now.x + 1, now.y, now.z));
            visited[now.x + 1][now.y][now.z] = 1;
            s++;
        }
        if (now.x - 1 >= 0 && slices[now.x - 1][now.y][now.z] == 1 && visited[now.x - 1][now.y][now.z] == 0)
        {
            dq.push_back(node(now.x - 1, now.y, now.z));
            visited[now.x - 1][now.y][now.z] = 1;
            s++;
        }
        if (now.y + 1 < n && slices[now.x][now.y + 1][now.z] == 1 && visited[now.x][now.y + 1][now.z] == 0)
        {
            dq.push_back(node(now.x, now.y + 1, now.z));
            visited[now.x][now.y + 1][now.z] = 1;
            s++;
        }
        if (now.y - 1 >= 0 && slices[now.x][now.y - 1][now.z] == 1 && visited[now.x][now.y - 1][now.z] == 0)
        {
            dq.push_back(node(now.x, now.y - 1, now.z));
            visited[now.x][now.y - 1][now.z] = 1;
            s++;
        }

    }
}
int main()
{
    cin >> 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 >> slices[j][k][i];
            }
        }
    }
    for (int i = 0; i < l; i++)
    {
        ;
        for (int j = 0; j < m; j++)
        {
            ;
            for (int k = 0; k < n; k++)
            {
                //bfs搜索是孤岛及其成员的个数
                if (visited[j][k][i]==0&&slices[j][k][i]==1)
                {
                    s = 1;
                    bfs(j, k, i);
                }
                //如果成员个数大于要求,则记入总数
                if (s>=t)
                {
                    sum += s;;
                    s = 0;
                }
            }
        }
    }
    cout << sum << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值