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 ...
随机推荐
- jQuery 插件开发——GridData(表格)
导读:我个人认为做开发最幸福的事之一就是设计一套属于自己的控件,老早之前就想去做这样的事情,一直碍于事件的冲突和个人的想法,最终没有定论,最近难得抽出一些空隙,去完成这件事情.其实自定义控件并不是难事 ...
- win8使用every'thing无法显示搜索结果的解决方法
关键词: win8,everything,无搜索结果 进入everything ,tools->option右下角有个 restore defaults 如果安全软件阻拦,点击 允许 就行了, ...
- 更新XML的Attribute(属性)
有一个XML文档,一个属性"pk"错了,正确是2.我们怎样把它更改正确?原XML文档如下: <?xml version="1.0" encoding=&q ...
- C:简单的学生信息处理程序实现
描述 在一个学生信息处理程序中,要求实现一个代表学生的类,并且所有成员变量都应该是私有的. (注:评测系统无法自动判断变量是否私有.我们会在结束之后统一对作业进行检查,请同学们严格按照题目要求完成,否 ...
- AngularJS(二)——常见指令以及下拉框实现
前言 学完AngularJS,总体上感觉没什么新鲜的东西,但是又感觉每一步都很新鲜,因为没有见过,又因为学到的语法函数和JavaScript差不多,本篇主要介绍一些AngularJS的指令,常见指令和 ...
- Jmeter-逻辑控制器之Foreach
ForEach 作用:用来遍历当前元素的所有可执行场景:在用户自定义变量中读取一系列相关的变量,该控制器下的采样器或控制器都会被执行一次或多次,每次读取不同的变量值: 输入变量前缀:在其中输入需要遍历 ...
- Generic detail view PostDetailView must be called with either an object pk or a slug.解决
Django 使用DetailView有这个问题,url,和模板统一调用模型时,用pk,而不是id 如果不是用DetailView,只是简单的视图,则用pk 或者id都可以. urls.py: url ...
- manjaro linux KDE桌面网易云音乐点击托盘图标无反应
这是一个很奇怪的问题,它的解决方法更奇怪... 找到网易云,右键,选择“编辑应用程序” 在打开的窗口中选中应用程序一栏 将“命令”的内容替换为: 1 env XDG_CURRENT_DESKTOP=D ...
- .net mvc 框架实现后台管理系统 3
左侧菜单实现 默认html <div class="layui-side layui-bg-black h-slide"> <div class="la ...
- 【转】Automated Testing Detail Test Plan
Automated Testing Detail Test PlanAutomated Testing DTP Overview This Automated Testing Detail Test ...