您的位置:首页 > 其它

1091. Acute Stroke (30)

2016-07-28 16:34 465 查看
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


题目意思太难搞懂了,我也不好描述这些医学的东西。换种数学的说法来说,就是一个三维图形,其中相连的点组成一个集合,如果集合大小大于等于t就是符合条件。求符合条件的集合的大小的总和。先用三维数组储存点的信息,然后用dfs遍历,求出所有连通集合,然后将大小大于等于t的集合的大小累加得到答案,这样最后两个case过不了,可能是因为六个方向递归搜索有点吃力。改用bfs即可通过。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;

int m,n,l,t;
vector<vector<vector<int> > >a;
vector<vector<vector<bool> > >isvis;

int dir1[6]={1,0,0,-1,0,0};
int dir2[6]={0,1,0,0,-1,0};
int dir3[6]={0,0,1,0,0,-1};
/*
void dfs(int i,int j,int k,int &count)
{
isvis[i][j][k]=true;
count++;
for(int p=0;p<6;p++)
{
int x=i+dir1[p],y=j+dir2[p],z=k+dir3[p];
if(x>=0&&y>=0&&z>=0&&x<l&&y<m&&z<n&&!isvis[x][y][z]&&a[x][y][z]==1)
{
dfs(x,y,z,count);
}
}
}
*/

struct point
{
int x,y,z;
point(int xx,int yy,int zz):x(xx),y(yy),z(zz){}
};

int main()
{
cin>>m>>n>>l>>t;
a.resize(l,vector<vector<int> >(m,vector<int>(n,0)));
for(int i=0;i<l;i++)
{
for(int j=0;j<m;j++)
{
for(int k=0;k<n;k++)
{
cin>>a[i][j][k];
}
}
}
isvis.resize(l,vector<vector<bool> >(m,vector<bool>(n,false)));
int res=0;
for(int i=0;i<l;i++)
{
for(int j=0;j<m;j++)
{
for(int k=0;k<n;k++)
{
if(!isvis[i][j][k]&&a[i][j][k]==1)
{
int count=0;
//dfs(i,j,k,count);
queue<point>que;
que.push(point(i,j,k));
isvis[i][j][k]=true;
while(!que.empty())
{
point tmp=que.front();
que.pop();
count++;
for(int p=0;p<6;p++)
{
int x=tmp.x+dir1[p],y=tmp.y+dir2[p],z=tmp.z+dir3[p];
if(x>=0&&y>=0&&z>=0&&x<l&&y<m&&z<n&&!isvis[x][y][z]&&a[x][y][z]==1)
{
isvis[x][y][z]=true;
que.push(point(x,y,z));
}
}
}
if(count>=t) res+=count;
}
}
}
}
cout<<res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs bfs