pat1091. Acute Stroke (30)
1091. Acute Stroke (30)
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)的更多相关文章
- PAT1091:Acute Stroke
1091. Acute Stroke (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One impo ...
- 1091. Acute Stroke (30)
题目如下: One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given ...
- 1091 Acute Stroke (30)(30 分)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...
- PAT (Advanced Level) 1091. Acute Stroke (30)
BFS求连通块.递归会爆栈. #include<cstdio> #include<cstring> #include<cmath> #include<algo ...
- PAT甲题题解-1091. Acute Stroke (30)-BFS
题意:给定三维数组,0表示正常,1表示有肿瘤块,肿瘤块的区域>=t才算是肿瘤,求所有肿瘤块的体积和 这道题一开始就想到了dfs或者bfs,但当时看数据量挺大的,以为会导致栈溢出,所以并没有立刻写 ...
- 【PAT甲级】1091 Acute Stroke (30 分)(BFS)
题意: 输入四个正整数M,N,K,T(K<=60,M<=1286,N<=128),代表每片的高度和宽度,片数和最小联通块大小.输出一共有多少个单元满足所在联通块大小大于等于T. tr ...
- PAT_A1091#Acute Stroke
Source: PAT A1091 Acute Stroke (30 分) Description: One important factor to identify acute stroke (急性 ...
- 【PAT】1091 Acute Stroke(30 分)
1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...
- PAT甲级——A1091 Acute Stroke【30】
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...
随机推荐
- Kotlin 控制台交互式操作
kotlin 可以使用和控制台进行交互式操作. 还是蛮有意思. 使用Kotlin的交互式操作首先必须要正确的安装JDK和配置JDK的环境. 可以参考这篇文长 安装完成之后就要下载一个Kotlin的交互 ...
- BS总结篇
学习Web开发差不多三个月了,这个阶段的学习给自己带来的更多的是视觉的盛宴.从CS的世界一下子来到了BS的地盘,心中除了惊喜还是惊喜.在这里还是希望自己对这三月所学的东西做一个阶段性的总结. 话不多说 ...
- html二
超链接 超链接有三种形式: 1.外部链接:链接到外部文件.举例: <a href="new.html">点击进入到新网页</a> a是英语anchor“锚” ...
- Python中的Numpy包
通过本次学习你可以掌握Numpy Numpy介绍(获取地址)更多Numpy函数 numpy的主要对象是同质多维数组.也就是在一个元素(通常是数字)表中,元素的类型都是相同的. numpy的数组类被成为 ...
- IdentityServer4 学习笔记[2]-用户名密码验证
回顾 上一篇介绍了IdentityServer4客户端授权的方式,今天来看看IdentityServer4的基于密码验证的方式,与客户端验证相比,主要是配置文件调整一下,让我们来看一下 配置修改 pu ...
- getTasksWithCompletionHandler的用法
最近在学习iOS的NSSession的后台下载,使用getTasksWithCompletionHandler获取下载任务时候,发现一些问题,希望分享一下: 第一次写博客有点乱,大家不要见怪-- NS ...
- [HAOI2012]音量调节 BZOJ2748 dp
题目描述 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都需要改变一次音量.在演出开始之前,他已经做好一个列表,里面写着每首歌开始之前他想要改变的音量是多少. ...
- springcloud系列九 整合Hystrix Dashboard
Hystrix Dashboard是Hystrix的仪表盘组件,主要用来实时监控Hystrix的各项指标信息,通过界面反馈的信息可以快速发现系统中存在的问题. 整合快速体验: pom.xml(这个是F ...
- mysql不支持emoji表情的问题的解决方法
最近财神圈项目集成微信登录功能的过程中,当保存用户有昵称含有表情符号时后台服务抛出异常,原来是数据库默认字符集不支持emoji表情字符.找到问题的原因后,因为之前也没有遇到过这样的问题,也没思路,迅速 ...
- Mybatis学习笔记(二) —— mybatis入门程序
一.mybatis下载 mybaits的代码由github.com管理,下载地址:https://github.com/mybatis/mybatis-3/releases 下载完后的目录结构: 二. ...