Acute Stroke (30)
时间限制 1000 ms 内存限制 655360 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
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.
输入描述:
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
输出描述:
For each case, output in a line the total volume of the stroke core.
输入例子:
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
输出例子:
26
就是给一个三维空间,由0,1构成,只是求出连通区域中1的个数大于等于t,就加到sum上去,最后得出volume
dfs 会爆栈,所以用bfs,都得用一个标记来标志是否走过。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int vis[1286][128][60];
//这里应用网上的一个小方法,通过三个数组来控制方向
int xx[6] = {1,-1,0,0,0,0};
int yy[6] = {0,0,1,-1,0,0};
int zz[6] = {0,0,0,0,1,-1};
int m,n,l,t;
typedef struct{
int x,y,z;
}node;
int bfs(int k,int i,int j)
{
queue<node>que;
node ptr;ptr.x = k;ptr.y = i;ptr.z = j;
que.push(ptr);
vis[k][i][j] = 0;
int cnt = 1;
while(!que.empty())
{
node tt = que.front();
for(int a = 0;a < 6;++a)
{
int tx = tt.x + xx[a];
int ty = tt.y + yy[a];
int tz = tt.z + zz[a];
if((tx >= 0 && tx < m && ty >= 0 && ty < n && tz >= 0 && tz < l) && vis[tx][ty][tz] == 1)
{
cnt++;
node pp;
vis[tx][ty][tz] = 0;
pp.x = tx;pp.y = ty;pp.z = tz;
que.push(pp);
}
}
que.pop();
}
return cnt;
}
int main()
{
cin >> m >> n >> l >> t;
for(int k = 0;k < l;++k)
{
for(int i = 0;i < m;++i)
{
for(int j = 0;j < n;++j)
{
scanf("%d",&vis[i][j][k]);
}
}
}
long long sum = 0;
for(int k = 0;k < l;++k)
{
for(int i = 0;i < m;++i)
{
for(int j = 0;j < n;++j)
{
if(vis[i][j][k] == 1){
int cnt = bfs(i,j,k);
if(cnt >= t)
sum += cnt;
}
}
}
}
cout << sum << endl;
return 0;
}