A 1091 Acute Stroke
Problem Description
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
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
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
题目大意:
六分考英语阅读理解,四分考编程。给你一个三维空间,里边每个点的值要么为0、要么为1。求出三维空间里所有由1构成的连通域并统计每个连通域里1的个数,如果1的数量不小于T,则累加,最后输出所有连通域累加的结果。
解题思路:
遍历三维空间,遇到一个1就对这个点展开广度优先搜索,每搜索到一个1就将这个点的值由1变为0 。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define INF 0x3f3f3f3f
typedef long long ll;
int brain[60][1286][128],m,n,l,t,ans=0;
int go[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct Node{
int x,y,z;
Node(int x,int y,int z):x(x),y(y),z(z){}
};
bool judge(int a, int b, int c){
if(a<0 || a>=l || b<0 || b>=m || c<0 || c>=n){
return false;
}
return true;
}
int bfs(int a, int b, int c){
int sum=1;
brain[a][b][c]=0;
queue<Node> q;
q.push(Node(a,b,c));
while(!q.empty()){
Node now=q.front();
q.pop();
int aa=now.x,bb=now.y,cc=now.z;
for(int i=0; i<6; i++){
int na=aa+go[i][0],nb=bb+go[i][1],nc=cc+go[i][2];
if(judge(na,nb,nc)){
if(brain[na][nb][nc]==1){
brain[na][nb][nc]=0;
q.push(Node(na,nb,nc));
sum++;
}
}
}
}
if(sum<t){sum=0;}
return sum;
}
int main(int argc, char **argv){
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",&brain[i][j][k]);
}
}
}
for(int i=0; i<l; i++){
for(int j=0; j<m; j++){
for(int k=0; k<n; k++){
if(brain[i][j][k]){
ans+=bfs(i,j,k);
}
}
}
}
printf("%d",ans);
return 0;
}