题目:
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.
【插图】
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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 600 ms
内存限制: 64 MB
题意:
对由“0”和 1 构成的三维数组进行计数统计。计数规则如下:如果相邻的1的个数大于给定的T则把这些1的个数纳入统计。相邻的含义是前后左右上下挨着的都叫相邻。最后输出统计的1 的总个数。
思路:
1、用BFS进行搜索,要走的方向有6个分别是前后左右与上下。可以令三个坐标增量数组X[6]、Y[6]、Z[6]。分别存储相应移动方向的坐标增量。搜索方法是核心。
2、其次,用bool inq 进行记录配合BFS的工作。BFS每搜索完一次,都返回与某点相邻的所有的1的个数,若大于等于T则返回计数值。
有注意事项在底部。
#include<cstdio>
#include<queue>
using namespace std;
int N,M,L,T;
int matrix[1290][190][61];
bool inq[1290][190][61];
struct Node{
int x,y,z;
}node;
int X[6]={0,0,-1,1,0,0};
int Y[6]={1,-1,0,0,0,0};
int Z[6]={0,0,0,0,-1,1};
bool judge(int x,int y,int z){
if(x<0||x>=N||y<0||y>=M||z<0||z>=L) return false;
if(!matrix[x][y][z]||inq[x][y][z]) return false;
return true;
}
int BFS(int x,int y,int z){
int cnt=0;
queue<Node> q;
node.x=x,node.y=y,node.z=z;
inq[x][y][z]=1;
q.push(node);
while(!q.empty()){
Node top=q.front();
cnt++;
q.pop();
for(int i=0;i<6;++i){
int newX=top.x+X[i];
int newY=top.y+Y[i];
int newZ=top.z+Z[i];
if(judge(newX,newY,newZ)){
node.x=newX,node.y=newY,node.z=newZ;
inq[newX][newY][newZ]=1;
q.push(node);
}
}
}
if(cnt>=T) return cnt;
return 0;
}
int main(){
scanf("%d%d%d%d",&N,&M,&L,&T);
for(int z=0;z<L;++z){
for(int x=0;x<N;++x){
for(int y=0;y<M;++y){
scanf("%d",&matrix[x][y][z]);
}
}
}
int cnt=0;
for(int z=0;z<L;++z){
for(int x=0;x<N;++x){
for(int y=0;y<M;++y){
if(matrix[x][y][z]&&!inq[x][y][z]){
cnt+=BFS(x,y,z);
}
}
}
}
printf("%d\n",cnt);
return 0;
}
注:
如果 bool judge()写成:
bool judge(int x,int y, int z){
if(x<0||x>=M||y<0||y>=N||z<0||z>=L) return 0;
if(!matrix[x][y][z]||inq[x][y][z]) return 0;
return 1;
}
虽然写成return 0 return 1编写方便,但是由于int 与bool转换需要时间,会导致最后两组数据超时。数据量小的时候这样写无妨。
如果 BFS() 写成:其中sum 是全局变量。
void BFS(int x,int y,int z){
int cnt=0;
queue<Node> q;
node.x=x,node.y=y,node.z=z;
inq[x][y][z]=1;
q.push(node);
while(!q.empty()){
Node top=q.front();
cnt++;
q.pop();
for(int i=0;i<6;++i){
int newX=top.x+X[i];
int newY=top.y+Y[i];
int newZ=top.z+Z[i];
if(judge(newX,newY,newZ)){
node.x=newX,node.y=newY,node.z=newZ;
inq[newX][newY][newZ]=1;
q.push(node);
}
}
}
if(cnt>=T) sum+=cnt;
}
还是会导致超时。
如有新想法即及时更新。