Acute Stroke

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

题意:
  给出一个三维矩阵,数组元素的取值为0或1.与某一个相邻的元素为其上、下、左、右、前、后这6个方向的邻接元素。另外,若干个相邻的“1”称为一个“块”(不必两两相邻,只要与块中某一个“1”
相邻该“1”就在块中)。而如果某个块中的“1”的个数不低于T个,那么称则个块为“卒中核心区”。现在需要求解所有卒中核心区中的1的个数之和。
 1 #include<cstdio>
2 #include<queue>
3 using namespace std;
4 struct node {
5 int x,y,z; //位置(x,y,z)
6 }Node;
7 int n,m,slice,T; //矩阵为n*m,共有slice层,T为卒中核心区中1的个数的下限
8 int pixel[1290][130][61]; //三维01矩阵
9 bool inq[1290][130][61] = {false}; //记录位置(x,y,z)是否已入过队
10 int X[6] = {0,0,0,0,1,-1}; //增量矩阵
11 int Y[6] = {0,0,1,-1,0,0};
12 int Z[6] = {1,-1,0,0,0,0};
13
14 bool judge(int x,int y,int z) { //判断坐标(x,y,z)是否需要访问
15 //越界返回false
16 if(x>=n || x<0 || y>=m || y<0 || z>=slice || z<0) return false;
17 //若当前位置为0或(x,y,z)已入过队,则返回false
18 if(pixel[x][y][z] == 0 || inq[x][y][z] == true) return false;
19 //以上都不满足,返回true
20 return true;
21 }
22
23 //BFS函数访问位置(x,y,z)所在的块,将该块中所有“1”的inq都设置为true
24 int BFS(int x,int y,int z) {
25 int tot = 0; //计数当前块中1的个数
26 queue<node> Q; //定义队列
27 Node.x = x,Node.y = y,Node.z = z; //结点Node的位置为(x,y,z)
28 Q.push(Node); //将结点Node入队
29 inq[x][y][z] = true; //设置位置(x,y,z)已入过队
30 while(!Q.empty()) {
31 node top = Q.front(); // 取出队首元素
32 Q.pop(); //队首元素出队
33 tot++; //当前块中1的个数加1
34 for(int i=0; i<6; i++) { //循环6次,得到6个增量方向
35 int newX = top.x + X[i];
36 int newY = top.y + Y[i];
37 int newZ = top.z + Z[i];
38 if(judge(newX,newY,newZ)) { //新位置(newX,newY,newZ)需要访问
39 //设置Node的坐标
40 Node.x = newX,Node.y = newY,Node.z = newZ;
41 Q.push(Node); //将结点Node入队
42 inq[newX][newY][newZ] = true; //设置(newX,newY,newZ)已入过队
43 }
44 }
45 }
46 if(tot >= T) return tot; //如果超过阈值,则返回
47 else return 0; //否则不记录该块1的个数
48 }
49
50 int main(){
51 scanf("%d%d%d%d",&n,&m,&slice,&T);
52 for(int z = 0; z<slice; z++) { //注意先枚举切片层号
53 for(int x = 0; x<n; x++) {
54 for(int y = 0; y<m; y++) {
55 scanf("%d",&pixel[x][y][z]);
56 }
57 }
58 }
59 int ans = 0; //记录卒中核心区中1的个数总和
60 for(int z = 0; z<slice; z++) {
61 for(int x=0; x<n; x++) {
62 for(int y = 0; y<m; y++) {
63 //如果当前位置为1,且未被访问,则BFS当前块
64 if(pixel[x][y][z] == 1 && inq[x][y][z] == false) {
65 ans += BFS(x,y,z);
66 }
67 }
68 }
69 }
70 printf("%d\n",ans);
71 return 0;
72 }


PAT A1091——BFS的更多相关文章

  1. PAT A1091 Acute Stroke

    对于坐标平面的bfs模板题~ #include<bits/stdc++.h> using namespace std; ; ][][]={false}; ][][]; int n,m,l, ...

  2. PAT_A1091#Acute Stroke

    Source: PAT A1091 Acute Stroke (30 分) Description: One important factor to identify acute stroke (急性 ...

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

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

  4. PAT甲级1034 Head of a Gang【bfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...

  5. PAT A1076 Forwards on Weibo (30 分)——图的bfs

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  6. PAT A1106 Lowest Price in Supply Chain (25 分)——树的bfs遍历

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  7. PAT A1021 Deepest Root (25 分)——图的BFS,DFS

    A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on th ...

  8. PAT甲题题解-1076. Forwards on Weibo (30)-BFS

    题目大意:给出每个用户id关注的人,和转发最多的层数L,求一个id发了条微博最多会有多少个人转发,每个人只考虑转发一次.用BFS,同时每个节点要记录下所在的层数,由于只能转发一次,所以每个节点要用vi ...

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

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

随机推荐

  1. The art of multipropcessor programming 读书笔记-硬件基础2

    本系列是 The art of multipropcessor programming 的读书笔记,在原版图书的基础上,结合 OpenJDK 11 以上的版本的代码进行理解和实现.并根据个人的查资料以 ...

  2. 前端VUE基于gitlab的CI_CD

    目录 CI 1.Gitlab的CI 1.1 GitLab-Runner 1.2 .gitlab-ci.yml 1.3 配置.gitlab-ci.yml 1.3.1 Pipeline概念 1.3.2 S ...

  3. Spring Boot中使用PostgreSQL数据库

    在如今的关系型数据库中,有两个开源产品是你必须知道的.其中一个是MySQL,相信关注我的小伙伴们一定都不陌生,因为之前的Spring Boot关于关系型数据库的所有例子都是对MySQL来介绍的.而今天 ...

  4. node ***.js或npm run scripts的脚本命令出现Cannot find module 'react-dev-utils/getPublicUrlOrPath'报错的解决办法

    出现类似Cannot find module 'react-dev-utils/getPublicUrlOrPath'一般是项目中没有下载报错中提到的模块(可以在项目中package.json文件de ...

  5. C语言日记① 初识C

    概念 c语言是一种计算机语言 也就是人与计算机打交道的语言 在早期,因为计算机使用的二进制 所以早期写代码都是科学家来写的使用对应的功能二进制代码 需要用到手册,所以开发不方便 在后来,人们发明了汇编 ...

  6. nvidia jetson xavier 风扇开机自启动

    作者声明 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 原文链接:https://www.cnblogs.com/phoenixash/p/15 ...

  7. Python读取网页表格数据

    学会了从网格爬取数据,就可以告别从网站一页一页复制表格数据的时代了. 说个亲身经历的事: 以前我的本科毕业论文是关于"燃放烟花爆竹和空气质量"之间关系的,就要从环保局官网查资料. ...

  8. VS Code环境配置

    1.语言配置 2.Node.js安装配置 系统变量中NODE_PATH,变量值为nodejs的安装路径. 用户变量Path包含%NODE_PATH% 如果说通过CMD打开的命令行可以执行node -v ...

  9. WPF实现Win10汉堡菜单

    WPF开发者QQ群: 340500857  | 微信群 -> 进入公众号主页 加入组织 前言 有小伙伴提出需要实现Win10汉堡菜单效果. 由于在WPF中没有现成的类似UWP的汉堡菜单,所以我们 ...

  10. px,dp sp是像素、尺寸、尺寸

    px:即像素,1px代表屏幕上一个物理的像素点:px单位不被建议使用,因为同样100px的图片,在不同手机上显示的实际大小可能不同,如下图所示(图片来自android developer guide, ...