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

提交代码

方法一:二维数组做法。注意DFS要段错误的。

 #include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int m,n,l,t;
bool isin(int x,int y,int part)
{
if(x<||x/m!=part||y<||y>=n)
{
return false;
}
return true;
}
bool vis[*][],ma[*][];
int dx[]= {-,,,},dy[]= {,,-,};
/*void DFS(int x,int y,int &count){
int part=x/m;//块的编号
vis[x][y]=true;
int i,xx,yy;
for(i=0;i<4;i++){
xx=x+dx[i];
yy=y+dy[i];
if(isin(xx,yy,part)&&ma[xx][yy]&&!vis[xx][yy]){
DFS(xx,yy,++count);
}
}
xx=x+dx[i];
yy=y+dy[i];
if(xx>=0&&ma[xx][yy]&&!vis[xx][yy]){
DFS(xx,yy,++count);
}
i++;
xx=x+dx[i];
yy=y+dy[i];
if(xx<l&&ma[xx][yy]&&!vis[xx][yy]){
DFS(xx,yy,++count);
}
}*/
void BFS(int x,int y,int &count)
{
queue<pair<int,int> > q;
vis[x][y]=true;
pair<int,int> p;
q.push(make_pair(x,y));
int xx,yy,i;
while(!q.empty())
{
p=q.front();
q.pop();
x=p.first;
y=p.second;
int part=x/m;
for(i=; i<; i++)
{
xx=x+dx[i];
yy=y+dy[i];
if(isin(xx,yy,part)&&ma[xx][yy]&&!vis[xx][yy])
{
vis[xx][yy]=true;
count++;
q.push(make_pair(xx,yy));//DFS(xx,yy,++count);
}
}
xx=x+dx[i];
yy=y+dy[i];
if(xx>=&&ma[xx][yy]&&!vis[xx][yy])
{
vis[xx][yy]=true;
count++;
q.push(make_pair(xx,yy));//DFS(xx,yy,++count);
}
i++;
xx=x+dx[i];
yy=y+dy[i];
if(xx<l&&ma[xx][yy]&&!vis[xx][yy])
{
vis[xx][yy]=true;
count++;
q.push(make_pair(xx,yy));//DFS(xx,yy,++count);
}
}
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
scanf("%d %d %d %d",&m,&n,&l,&t);
l*=m;
dx[]=-m;
dy[]=;
dx[]=m;
dy[]=;
int i,j,num;
memset(vis,false,sizeof(vis));
memset(ma,false,sizeof(ma));
for(i=; i<l; i++)
{
for(j=; j<n; j++)
{
scanf("%d",&num);
if(num)
{
ma[i][j]=true;
}
}
}
int count,sum=;
for(i=; i<l; i++)
{
for(j=; j<n; j++)
{
if(ma[i][j]&&!vis[i][j])
{
count=; BFS(i,j,count);
if(count>=t)
{
sum+=count;
}
}
}
}
printf("%d\n",sum);
return ;
}

方法二:三维数组。

 #include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int m,n,l,t;
bool isin(int x,int y,int z)
{
if(x<||x>=m||y<||y>=n||z<||z>=l)
{
return false;
}
return true;
}
struct node{
int x,y,z;
node(){}
node(int _x,int _y,int _z):x(_x),y(_y),z(_z){}
};
bool vis[][][],ma[][][];
int dx[]= {-,,,,,},dy[]= {,,-,,,},dz[]= {,,,,-,};
void BFS(int x,int y,int z,int &count)
{
queue<node> q;
vis[z][x][y]=true;
q.push(node(x,y,z));
int xx,yy,zz,i;
node p;
while(!q.empty())
{
p=q.front();
q.pop();
x=p.x;
y=p.y;
z=p.z;
for(i=; i<; i++)
{
xx=x+dx[i];
yy=y+dy[i];
zz=z+dz[i];
if(isin(xx,yy,zz)&&ma[zz][xx][yy]&&!vis[zz][xx][yy])
{
vis[zz][xx][yy]=true;
count++;
q.push(node(xx,yy,zz));//DFS(xx,yy,++count);
}
}
}
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
scanf("%d %d %d %d",&m,&n,&l,&t);
int i,j,k,num;
memset(vis,false,sizeof(vis));
memset(ma,false,sizeof(ma));
for(k=; k<l; k++)
{
for(i=; i<m; i++)
{
for(j=; j<n; j++)
{
scanf("%d",&num);
if(num)
{
ma[k][i][j]=true;
}
}
}
} int count,sum=;
for(k=; k<l; k++)
{
for(i=; i<m; i++)
{
for(j=; j<n; j++)
{
if(ma[k][i][j]&&!vis[k][i][j])
{
count=;
BFS(i,j,k,count);
if(count>=t)
{
sum+=count;
}
}
}
}
}
printf("%d\n",sum);
return ;
}

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

  1. PAT1091:Acute Stroke

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

  2. 1091. Acute Stroke (30)

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

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

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

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

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

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

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

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

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

  7. PAT_A1091#Acute Stroke

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

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

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

  9. PAT甲级——A1091 Acute Stroke【30】

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

随机推荐

  1. for循环 break

    for (int i = 1; i <= 8;i++) {} for (int i=10; i>=1;i--) i的起始值是10 着次递减 for(){}嵌套放便控制行列的长短 break ...

  2. Web Server 在iis下部署asp网站在iis下

    Web Server 在iis下部署asp网站在iis下 一.参考地址: win7 http://jingyan.baidu.com/article/636f38bb1bbcadd6b846108b. ...

  3. win10下安装配置mysql-8.0.13--实战可用

    1.下载mysql-8.0.13安装包 1 https://dev.mysql.com/downloads/mysql/ 选择zip安装包下载就好. 2.解压到你要安装的目录 3.创建my.ini配置 ...

  4. (原创)团体程序设计天梯赛-练习集 L1-048 矩阵A乘以B (15 分)

    给定两个矩阵A和B,要求你计算它们的乘积矩阵AB.需要注意的是,只有规模匹配的矩阵才可以相乘.即若A有R​a​​行.C​a​​列,B有R​b​​行.C​b​​列,则只有C​a​​与R​b​​相等时,两 ...

  5. 转载 【Linux】Linux中常用操作命令

    [Linux]Linux中常用操作命令     https://www.cnblogs.com/laov/p/3541414.html#vim   Linux简介及Ubuntu安装 常见指令 系统管理 ...

  6. kuangbin专题16B(kmp模板)

    题目链接: https://vjudge.net/contest/70325#problem/B 题意: 输出模式串在主串中出现的次数 思路: kmp模板 在 kmp 函数中匹配成功计数加一, 再令 ...

  7. k8s标签

    一.标签是什么 标签是k8s特色的管理方式,便于分类管理资源对象. 一个标签可以对应多个资源,一个资源也可以有多个标签,它们是多对多的关系. 一个资源拥有多个标签,可以实现不同维度的管理. 可以使用标 ...

  8. 「杂录」CQOI 2018 背板记

    背景 经过一天天的等待,终于迎来了\(CQOI2018\),想想\(NOIp\)过后到现在,已经有了快要半年了,曾经遥遥无期,没想到时间一转眼就过去了-- 日志 \(Day0\) 因为明天就要考试了, ...

  9. CF959C Mahmoud and Ehab and the wrong algorithm 构造

    Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is: Given an un ...

  10. CF431C k-Tree dp

    Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired ...