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

最新文章

  1. mysql [ERROR] Fatal error: Can&#39;t open and lock privilege tables: Table &#39;mysql.host&#39; doesn&#39;t exist (转载)
  2. window.showModalDialog的简单实践
  3. 双参数Bellman-ford带队列优化类似于背包问题的递推
  4. Android——KEYCODE列表
  5. LeetCode 274
  6. Remote Direct Memory Access (RDMA)
  7. 什么是staging server
  8. JAVA中,数组的操作与排序
  9. Jquery中获取iframe的代码方法
  10. PAT (Advanced Level) 1111. Online Map (30)
  11. 2751: [HAOI2012]容易题(easy)
  12. Angular4 表单处理
  13. python 打开浏览器的方法 Python打开默认浏览器
  14. linux下elasticsearch 安装、配置及示例
  15. HttpClient4.5简单使用
  16. CSS2.1SPEC:视觉格式化模型之width属性详解(下)
  17. HBase(十)HBase性能调优总结
  18. CSDN日报20170404 ——《不不过写代码,而是完毕作品》
  19. hdu3401 Trade 单调队列优化dp
  20. java Graphics2d消除锯齿,使字体平滑显示

热门文章

  1. 行业动态 | 通过使用Apache Cassandra实现实时供应链管理
  2. Vue3.0+Electron聊天室|electron跨平台仿QQ客户端|vue3.x聊天应用
  3. k8s v1.18.2 centos7 下环境搭建
  4. Java数组练习(打印杨辉数组)
  5. POJ-1797(最短路变形-dijkstra)
  6. SpringMVC-01 什么是SpringMVC
  7. ArrayList源码分析笔记
  8. InterJ idea 回滚提交的代码
  9. python中函数与方法的区别
  10. P4285 [SHOI2008]汉诺塔 题解 (乱搞)