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 ...
随机推荐
- .NET 图片上传接收类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- JDK Linux下安装
下载jdk-6u45-linux-x64.bin到/root目录下执行./jdk-6u45-linux-x64.bin 会在/root目录下生成 jdk1.6.0_45 文件 之后配置环境变量 编辑/ ...
- Ubuntu小记
一. Ubuntu分区记忆 参考教程调整: 1. /boot用于安装grub,设为主分区 2. /根目录20G一般足够 3. /home剩下的给home 4. swap空间=物理内存 挂载点 大小 类 ...
- Liunx基础优化配置
1: 为系统添加操作用户,并授予sudo权限 [root@localhost ~]# groupadd cai [root@localhost ~]# useradd cai -g cai [roo ...
- LB 负载均衡的层次结构(转)
http://blog.csdn.net/mindfloating/article/details/51020767 作为后端应用的开发者,我们经常开发.调试.测试完我们的应用并发布到生产环境,用户就 ...
- springboot jpa mongodb 整合mysql Field in required a bean of type that could not be found Failed to load ApplicationContext
1.完整报错 *************************** APPLICATION FAILED TO START *************************** Descripti ...
- 小程序首页不显示tabBar
app.json中配置了tabBar,但是首页不想显示,首页跳转时使用 wx.redirectTo和wx.navigateTo无法完成跳转 这时用到了 wx.switchTab 可以实现我们的需求,首 ...
- 如何进bat
既然是要谈如何进入BAT,那么咱们就从面试的角度来谈学习这件事,会谈谈一流互联网公司对于Java后端程序员的要求,相应的,也会谈谈如何达到这样的要求. 为了简单起见,这些要求分为三个层次,分别为基本要 ...
- Educational Codeforces Round 13 B
Description The girl Taylor has a beautiful calendar for the year y. In the calendar all days are gi ...
- C#工具类之日期扩展类
/// <summary> /// DateTimeHelper /// </summary> public static class DateTimeHelper { /// ...