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
题目大意:给定一个三维数组,0 表示正常, 1 表示有肿瘤,肿瘤块的大小大于等于 t 才算作是肿瘤,求所有满足大小大于 t 的肿瘤块的总大小。
分析:BFS。总共有 6 个方向,从某个值为 1 的点开始进行 BFS,返回这个块的大小。遍历完整个三维数组即可。
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
#define NUMBER_OF_THREADS 10
using namespace std;int matrix[65][1290][130],flag[65][1290][130];
int dir_z[]={1,-1,0,0,0,0},dir_x[]={0,0,1,-1,0,0},dir_y[]={0,0,0,0,1,-1};typedef struct node
{int x,y,z;
}node;int bfs(int z,int x,int y)
{int cnt=0;node sta;sta.x=x,sta.y=y,sta.z=z;queue<node>que;que.push(sta);while(!que.empty()){node temp=que.front();que.pop();cnt++;for(int i=0;i<6;++i){node val=temp;val.x+=dir_x[i],val.y+=dir_y[i],val.z+=dir_z[i];if(matrix[val.z][val.x][val.y]==1&&flag[val.z][val.x][val.y]==0)flag[val.z][val.x][val.y]=1,que.push(val);}}return cnt;
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint m,n,l,t;scanf("%d%d%d%d",&m,&n,&l,&t);for(int z=1;z<=l;++z)for(int x=1;x<=m;++x)for(int y=1;y<=n;++y)scanf("%d",&matrix[z][x][y]),flag[z][x][y]=0;int ans=0;for(int z=1;z<=l;++z){for(int x=1;x<=m;++x){for(int y=1;y<=n;++y){if(!flag[z][x][y]){flag[z][x][y]=1;if(matrix[z][x][y]==1){int cnt=bfs(z,x,y);if(cnt>=t)ans+=cnt;}}}}}printf("%d\n",ans);#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位#endif //testreturn 0;
}