您的位置:首页 > 其它

PAT 1091. Acute Stroke (30)

2015-09-09 15:34 393 查看


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


这道题就相当于给了一个图,求图中点数大于阈值的连通图的点数总和。只不过这个图是三维的。和二维的做法一样,用BFS或DFS即可,只是这道题我用DFS的时候出现了段错误,所以最后还是选择用了BFS。代码如下:

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
int brain[1286][128][60];
typedef struct node{
int x,y,z;
node(int ps,int py,int pz):x(ps),y(py),z(pz){}
}node;
int N,M,L,T;
int bfs(int x,int y,int z)
{
int count=0;
queue<node> myqueue;
myqueue.push(node(x,y,z));
while(myqueue.size())
{
node temp=myqueue.front();
myqueue.pop();
if(brain[temp.x][temp.y][temp.z])
count++;
else
continue;
brain[temp.x][temp.y][temp.z]=0;
if(temp.x-1>=0)
myqueue.push(node(temp.x-1,temp.y,temp.z));
if(temp.x+1<N)
myqueue.push(node(temp.x+1,temp.y,temp.z));
if(temp.y-1>=0)
myqueue.push(node(temp.x,temp.y-1,temp.z));
if(temp.y+1<M)
myqueue.push(node(temp.x,temp.y+1,temp.z));
if(temp.z-1>=0)
myqueue.push(node(temp.x,temp.y,temp.z-1));
if(temp.z+1<L)
myqueue.push(node(temp.x,temp.y,temp.z+1));

}
return count;
}
int main(void)
{
memset(brain,0,sizeof(brain));
cin>>M>>N>>L>>T;
for(int i=0;i<L;i++)
for(int j=0;j<M;j++)
for(int k=0;k<N;k++)
scanf("%d",&brain[k][j][i]);
int count=0;
for(int i=0;i<L;i++)
for(int j=0;j<M;j++)
for(int k=0;k<N;k++)
{
int max=bfs(k,j,i);
if(max>=T)
count+=max;
}
cout<<count;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: