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. Winform定时启动

    System.Timers.Timer t; ; int qian; int bai; int shi; int ge; public 测试定时启动() { InitializeComponent() ...

  2. wpf跳转网页

    如果是本地磁盘上的网页,可以考虑利用操作系统的文件关联自动调用操作系统默认浏览器: System.Diagnostics.Process.Start("explorer.exe", ...

  3. ssh 免密码远程登录

    背景: 公司有两台服务器A与B,经常会碰到代码中的配置文件不一致的情况...............,为了反面让两台服务器配置统一,所以需要写个shell脚本,用到的linux命令主要是scp 1.在 ...

  4. 写一个Android输入法01——最简步骤

    本文演示用Android Studio写一个最简单的输入法.界面和交互都很简陋,只为剔肉留骨,彰显写一个Android输入法的要点. 1.打开Android Studio创建项目,该项目和普通APP的 ...

  5. 分层最短路-2018南京网赛L

    大概题意: 题意:N个点,M条带权有向边,求将K条边权值变为0的情况下,从点1到点N的最短路. 拓展:可以改变K条边的权值为x 做法:把每个点拆成k个点,分别表示还能使用多少次机会,构造新图. 实际写 ...

  6. [SinGuLaRiTy] NOIP 膜你赛-Day 2

    [SinGuLaRiTy-1031] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 对于所有题目: Time Limit: 1s | Mem ...

  7. silverlight browse information

    public class Browser { /// <summary> /// During static instantiation, only the Netscape flag i ...

  8. python3查询mysql数据

    python3不支持MySQLdb,代替的是import pymysql 连接数据库查表: import pymysqlconn= pymysql.connect( host='xx.xx.xx.xx ...

  9. stl仿函数和适配器

    所谓的适配器就是底层利用仿函数,然后修改仿函数的接口,达到自己的目的: 例如:template<class operation> class binder1st的适配器,其本质是一个类,它 ...

  10. asp.net mvc 中的 controller和asp.net web api 的apicontroller有什么区别?(转)

    本质上区别不大,一个返回html/text类型的response,一个返回json/text或者xml/text类型的response,对于api环境而言,apicontroller更智能一点,他可以 ...