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×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×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.
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
刚开始题目没看懂,后来发现就是一个三维的广度优先搜索,给定一个三维立体图,求所有值为1的块的大小大于等于T的体积之和。
将二维的bfs加一维,对一个点上下左右前后6个方向进行遍历。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int inf = 999999999;
const int maxn = 10010;
int v[1300][130][62];
bool visit[1300][130][62];
int x[6]={1,-1,0,0,0,0};
int y[6]={0,0,1,-1,0,0};
int z[6]={0,0,0,0,1,-1};
struct pos{
int x,y,z;
pos(int a,int b,int c){
x=a;
y=b;
z=c;
}
};
int main()
{
int m,n,l,t;
scanf("%d %d %d %d",&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",&v[j][k][i]);
}
}
}
queue<pos>q;
int cnt=0;
for(int i=0;i<l;i++){
for(int j=0;j<n;j++){
for(int k=0;k<m;k++){
if(visit[k][j][i]==false){
int temp=0;
q.push(pos(k,j,i));
visit[k][j][i]=true;
while(!q.empty()){
pos now=q.front();
q.pop();
if(v[now.x][now.y][now.z]==1){
temp++;
for(int d=0;d<6;d++){
int a=now.x+x[d],b=now.y+y[d],c=now.z+z[d];
if(a>=0&&a<m&&b>=0&&b<n&&c>=0&&c<l&&visit[a][b][c]==false){
q.push(pos(a,b,c));
visit[a][b][c]=true;
}
}
}
}
if(temp>=t) cnt+=temp;
}
}
}
}
printf("%d",cnt);
return 0;
}