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 (≤) 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

题意:

每一个二维矩阵代表一个切片,矩阵中“1”代表病变,“0”代表没有发生病变,只有连在一起的病变块的数目达到一定值时才被计入其中。输出有多少病变。

思路:

刚开始想到的是用模拟的方法来解决这道题,但是做到搜索的时候就没办法再往下做了。然后看了一下别人的代码,发现题目可以抽象成一个三维矩阵的BFS。

Code:

#include<iostream>
#include<vector>
#include<queue> using namespace std; int m, n, l, t;
int count = 0, rang = 0, total = 0;
vector<vector<vector<int> > > matrix;
vector<vector<vector<int> > > visited;
vector<int> dir = {1, 0, 0, -1, 0, 0, 1, 0}; bool inMatrix(int i, int j, int k) {
if (i >= 0 && i < l && j >= 0 && j < m && k >= 0 && k < n)
if (matrix[i][j][k] == 1 && visited[i][j][k] == 0) return true;
return false;
} void BFS(int x, int y, int z) {
visited[x][y][z] = 1;
count++;
for (int i = 0; i < 6; ++i) {
if (inMatrix(x+dir[i], y+dir[i+1], z+dir[i+2]))
BFS(x+dir[i], y+dir[i+1], z+dir[i+2]);
}
} int main() {
cin >> m >> n >> l >> t; matrix = vector<vector<vector<int> > >(l, vector<vector<int> >(m, vector<int>(n, 0)));
visited = vector<vector<vector<int> > >(l, vector<vector<int> >(m, vector<int>(n, 0))); for (int i = 0; i < l; ++i) {
for (int j = 0; j < m; ++j) {
for (int k = 0; k < n; ++k) {
int p;
cin >> p;
matrix[i][j][k] = p;
}
}
}
for (int i = 0; i < l; ++i) {
for (int j = 0; j < m; ++j) {
for (int k = 0; k < n; ++k) {
count = 0;
if (visited[i][j][k] == 0 && matrix[i][j][k] == 1)
BFS(i, j, k);
if (count >= t) total += count;
}
}
} cout << total << endl; return 0;
}

  

提交之后有两组数据显示“Segmentation Fault”。

1091 Acute Stroke的更多相关文章

  1. 【PAT】1091 Acute Stroke(30 分)

    1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...

  2. PAT 1091 Acute Stroke [难][bfs]

    1091 Acute Stroke (30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the ...

  3. 1091. Acute Stroke (30)

    题目如下: One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given ...

  4. 1091 Acute Stroke (30)(30 分)

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

  5. PAT 1091. Acute Stroke (bfs)

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

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

    BFS求连通块.递归会爆栈. #include<cstdio> #include<cstring> #include<cmath> #include<algo ...

  7. PAT甲级1091 Acute Stroke【三维bfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805375457411072 题意: 求三维的连通块 思路: 简单b ...

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

    题意:给定三维数组,0表示正常,1表示有肿瘤块,肿瘤块的区域>=t才算是肿瘤,求所有肿瘤块的体积和 这道题一开始就想到了dfs或者bfs,但当时看数据量挺大的,以为会导致栈溢出,所以并没有立刻写 ...

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

    题意: 输入四个正整数M,N,K,T(K<=60,M<=1286,N<=128),代表每片的高度和宽度,片数和最小联通块大小.输出一共有多少个单元满足所在联通块大小大于等于T. tr ...

随机推荐

  1. C++教程01:计算机系统的组成

    教程首发 | 公众号:lunvey 学习C++之前,需要先了解一点基础的计算机知识.毕竟C++是跑在计算机系统上的,我们写的程序都是一段段的指令集. 首台计算机ENIAC问世之后,缺少原理指导.冯诺依 ...

  2. 基于docker快速搭建hbase集群

    一.概述 HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文"Bigtable:一个结构化数据的分布式存储系统".就像Bigt ...

  3. wxWidgets源码分析(2) - App主循环

    目录 APP主循环 MainLoop 消息循环对象的创建 消息循环 消息派发 总结 APP主循环 MainLoop 前面的wxApp的启动代码可以看到,执行完成wxApp::OnInit()函数后,接 ...

  4. Java 开发工具

    开发工具·Eclipse 常见开发工具介绍 * A:操作系统自带的记事本软件 * B:高级记事本软件 * C:集成开发环境 IDE     * (Integrated Development Envi ...

  5. mysql 使用sleep操作 update 来停止一段时间执行语句 [骚操作]

    update mytestTable inner join(select '2' as id, sleep(5)) a on mytestTable.id=a.id set mytestTable.n ...

  6. 线上MySQL读写分离,出现写完读不到问题如何解决

    大家好,我是历小冰. 今天我们来详细了解一下主从同步延迟时读写分离发生写后读不到的问题,依次讲解问题出现的原因,解决策略以及 Sharding-jdbc.MyCat 和 MaxScale 等开源数据库 ...

  7. pandas函数高级

    一.处理丢失数据 有两种丢失数据: None np.nan(NaN) 1. None None是Python自带的,其类型为python object.因此,None不能参与到任何计算中. #查看No ...

  8. android底部导航栏小结

    android自带的有TabHost,但好像无法满足要求, 本文只记录使用 TabLayout + Fragment  和 android 自带的 BottomNavigationView + Fra ...

  9. .NET 6 Preview 2 发布

    前言 在 2021 年 3 月 11 日, .NET 6 Preview 2 发布,这次的改进主要涉及到 MAUI.新的基础库和运行时.JIT 改进. .NET 6 正式版将会在 2021 年 11 ...

  10. PTA 数组元素的区间删除

    6-6 数组元素的区间删除 (20 分)   给定一个顺序存储的线性表,请设计一个函数删除所有值大于min而且小于max的元素.删除后表中剩余元素保持顺序存储,并且相对位置不能改变. 函数接口定义: ...