PAT 1091 Acute Stroke [难][bfs]

1091 Acute Stroke (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×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.

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

题目大意:先给出4个数,M行,N列,L共有几块,T阈值。1表示中风,0表正常,如果两块有相同的边,那么它们就是相连的。肿瘤块大小大于T才是肿瘤。

//这个真看不太懂,以前看过的,都不理解,什么意思,什么是块连在一起了呢?真不理解啊。

代码转自:https://www.liuchuo.net/archives/2307

#include <cstdio>
#include <queue>
using namespace std;
struct node {
    int x, y, z;
};
int m, n, l, t;
int X[6] = {1, 0, 0, -1, 0, 0};//方向依次是:同一个平面上:下,右,往下一个平面正下,
int Y[6] = {0, 1, 0, 0, -1, 0};//同一个平面,上,左,往上一个平面上
int Z[6] = {0, 0, 1, 0, 0, -1};
int arr[1300][130][80];
bool visit[1300][130][80];
bool judge(int x, int y, int z) {
    if(x < 0 || x >= m || y < 0 || y >= n || z < 0 || z >= l) return false;
    if(arr[x][y][z] == 0 || visit[x][y][z] == true) return false;
    return true;
}
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);
    visit[x][y][z] = true;
    while(!q.empty()) {
        node top = q.front();
        q.pop();
        cnt++;//这里对块进行计数。
        for(int i = 0; i < 6; i++) {
            int tx = top.x + X[i];//6个方向进行判断。
            int ty = top.y + Y[i];
            int tz = top.z + Z[i];
            if(judge(tx, ty, tz)) {
        //这里需要判断下标是否是合法的,并且数组是否是1或者未被访问过。
                visit[tx][ty][tz] = true;
                temp.x = tx, temp.y = ty, temp.z = tz;
                q.push(temp);
            }
        }
    }
    if(cnt >= t)
        return cnt;
    else
        return 0;//否则返回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++)//使用三维数组存储。
                scanf("%d", &arr[j][k][i]);//先读入进来。
    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(arr[j][k][i] == 1 && visit[j][k][i] == false)
                    ans += bfs(j, k, i);//i=0时,也就是从第0块开始。
            }
        }
    }
    printf("%d", ans);
    return 0;
}

//代码是看懂了,但是有点疑问,就是如果l不是5呢?为什么它们就有公共的边呢?这里也没有进行判断啊,还不不明白。

1.使用三维数据进行输入,并且标记是否被访问过,使用bfs进行遍历。

2.对六个方向都进行了判断,并且使用了一个judge函数判断下标是否越界,并且是否可以访问该点,十分厉害,值得学习。

3.使用bfs也就是需要用到队列,判断条件是队列是否为空,学习了。

原文地址:https://www.cnblogs.com/BlueBlueSea/p/9693094.html

时间: 2024-11-05 18:45:03

PAT 1091 Acute Stroke [难][bfs]的相关文章

PAT 1091. Acute Stroke (30)

1091. Acute Stroke (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

PAT甲题题解-1091. Acute Stroke (30)-BFS

题意:给定三维数组,0表示正常,1表示有肿瘤块,肿瘤块的区域>=t才算是肿瘤,求所有肿瘤块的体积和 这道题一开始就想到了dfs或者bfs,但当时看数据量挺大的,以为会导致栈溢出,所以并没有立刻写,想有没有别的办法.然而结果是,实在想不出别的办法了,所以还是尝试写写dfs.bfs. 一开始先用了dfs,最后两个样例段错误,估计是栈溢出了.之所以dfs栈溢出,因为dfs的时候每个状态都会存储在堆栈里,就好比dfs的第一个状态,一直保存到最后整个dfs结束.而bfs是存储在队列中,每次队列都会有状态取

PAT甲级——1091 Acute Stroke (广度优先搜索BFS)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/94207638 1091 Acute Stroke (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

1091. Acute Stroke (30)【搜索】——PAT (Advanced Level) Practise

题目信息 1091. Acute Stroke (30) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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,

【PAT甲级】1091 Acute Stroke (30分)

1091 Acute Stroke (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

1091. Acute Stroke (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 Spec

PAT (Advanced Level) 1091. Acute Stroke (30)

BFS求连通块.递归会爆栈. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; int M,N,L,T; int A[70][1300][133]; bo

1091 acute stroke

三维空间的DFS,挺新颖的一题 AC代码: #include <vector> #include <cstdio> #include <queue> using namespace std; int x[] = {0,0,1,0,-1,0}; int y[] = {0,1,0,-1,0,0}; int z[] = {-1,0,0,0,0,1}; class node{ public: int x; int y; int z; node(int xx,int yy,int

pat1091. 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 iden