题目如下:

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

这道题的叙述相当的费解,而且图有些抽象,我读了两次都没有读懂,后来看了Uncle_Sugar的叙述和解法才明白了题意,其解法简洁、高效、易懂,下面进行介绍。

题目的本质就是对一个三维数组中1的连通区域中所有1进行计数。因为是三维坐标,因此对1计数不再是简单的四个方向,而是六个,分别是前后左右上下,也就是图中给出的六个红色。个人认为这张图过于抽象,说白了就是进行三维广度或者深度搜索,只不过邻接点的判断是通过六个方向是否为1来确定的。

Uncle_Sugar的算法巧妙地给出了六个方向的BFS实现,对于某个位置(x,y,z),我们先判断(x+1,y,z),接着判断(x-1,y,z),然后是(x,y+1,z)...以此类推,他巧妙的利用三个一维数组实现了用一个循环实现六个方向的遍历,然后判断是否是合法范围,是则计数,最后即可得到1的总数。

下面是Uncle_Sugar的代码:

# include <cstdio>
# include <queue>
using std::queue; int map[1286][128][60];
struct loca
{
int x,y,z;
loca(int _x,int _y,int _z):x(_x),y(_y),z(_z){}
}; int m,n,l,t;
int dx[6] = {1,-1,0,0,0,0};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {0,0,0,0,1,-1};
int ans = 0;
int InRange(int x,int y,int z)
{
return x < m && x >=0 && y < n&&y >= 0 && z < l && z >= 0;
}
void bfs(int x,int y,int z)
{
int ret = 0;
queue<loca> que;
que.push(loca(x,y,z));
map[x][y][z] = 0;ret++;
while (!que.empty())
{
loca tp = que.front();que.pop();
x = tp.x;
y = tp.y;
z = tp.z;
for (int i=0;i<6;i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
int nz = z + dz[i];
if (InRange(nx,ny,nz) && map[nx][ny][nz] == 1)
{
map[nx][ny][nz] = 0;ret++;
que.push(loca(nx,ny,nz));
}
}
}
if (ret>=t)
ans += ret;
}
int main()
{
scanf("%d%d%d%d",&m,&n,&l,&t);
for (int k=0;k<l;k++)
for (int i=0;i<m;i++)
for (int j=0;j<n;j++)
scanf("%d",&map[i][j][k]);
for (int k=0;k<l;k++)
for (int i=0;i<m;i++)
for (int j=0;j<n;j++)
if (map[i][j][k]==1)
bfs(i,j,k);
printf("%d\n",ans);
return 0;
}

1091. Acute Stroke (30)的更多相关文章

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

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

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

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

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

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

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

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

  5. pat1091. Acute Stroke (30)

    1091. Acute Stroke (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One impo ...

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

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

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

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

  8. PAT 1091. Acute Stroke (bfs)

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

  9. 1091 Acute Stroke

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

随机推荐

  1. ●BZOJ 4596 [Shoi2016]黑暗前的幻想乡

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4596 题解: 容斥,矩阵树定理,矩阵行列式 先说说容斥:(一共有 N-1个公司) 令 f[i ...

  2. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  3. bzoj1073[SCOI2007]kshort

    1073: [SCOI2007]kshort Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1483  Solved: 373[Submit][Sta ...

  4. [hdu4694]Important Sisters

    来自FallDream的博客,未经允许,请勿转载,谢谢. 给定一张图,求每个点到第n个点必须经过的点的编号之和.n<=50000 一道支配树裸题 然后统计答案的时候可以正着推,ans[i]=an ...

  5. bzoj3173[Tjoi2013]最长上升子序列 平衡树+lis

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2253  Solved: 1136[Submit][S ...

  6. Python3 sys.argv[ ]的用法解释

    sys.argv[]说白了就是一个从程序外部获取参数的桥梁,这个"外部"很关键,所以那些试图从代码来说明它作用的解释一直没看明白.因为我们从外部取得的参数可以是多个,所以获得的是一 ...

  7. Android自定义模糊匹配搜索控件(二)

    在项目中遇到一个需要通过某个字的值筛选匹配带出其他信息的需求,在这里将实现思路整理出来. 源码地址:https://github.com/whieenz/SearchSelect 先看效果图 上图中的 ...

  8. Linux学习之CentOS(五)--CentOS下VMware-Tools安装

    已经进入到了Linux学习之CentOS的第六篇随笔了,所以这里就介绍一下VMware-Tools的安装. VMware-Tools的安装 VMware-Tools 主要的功能就是让用户在虚拟机和真实 ...

  9. Linux下常用设置文件和文件夹读写权限操作

    1.查看权限 ls -l xxx.xxx (xxx.xxx是文件名)   2.常见权限 -rw------- (600) 只有所有者才有读和写的权限 -rw-r--r-- (644) 只有所有者才有读 ...

  10. 整理spring定时器corn表达式

    1.结构 corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份 2.各字段的含义   字段 允许值 允许的特殊字符 秒 0~59 - * / 分 0~59 - * / ...