题目
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 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
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
题目的表达真是够坑的……
给出的01数据,每m*n个是一个平面,依次堆叠(z轴方向变化一个单位)。
需要找的是符合要求的1的数量:
即与该1相连的1(包括本身)大于等于T。
相连指上下左右前后。然后,是需要延伸的,即相连某格是1,那么他的相连是1的格也要计算进去……
实际就是算所有总成员数大于T的所有连通域的总成员数。
dfs,统计连通域内成员数,大于T时累加即可。
代码:
#include <iostream>
#include <vector>
using namespace std;
struct pos //保存位置
{
int i,j,k;
pos(int i1=0,int i2=0,int i3=0):i(i1),j(i2),k(i3){}
};
int main()
{
int m,n,l,t;
cin>>m>>n>>l>>t;
vector<vector<vector<int>>> data(m,vector<vector<int>>(n,vector<int>(l,0))); //输入的扫描信息
for(int k=0;k<l;k++) //输入
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
scanf("%d",&data[i][j][k]);
vector<vector<vector<int>>> test(m,vector<vector<int>>(n,vector<int>(l,0))); //相应单元是否已经探测标记(仅标记值是1的格)
vector<pos> stack; //dfs用栈
pos pos1; //临时位置
int count=0,connect; //总的坏块数,连接在一起的坏单元数
for(int i=0;i<m;i++) //扫描
for(int j=0;j<n;j++)
for(int k=0;k<l;k++)
{
if(data[i][j][k]==1&&test[i][j][k]==0) //找到新的单元时dfs
{
stack.clear();
stack.push_back(pos(i,j,k)); //压入新的点
test[i][j][k]=1;
connect=1;
while(!stack.empty()) //dfs
{
pos1=stack.back();
stack.pop_back();
if(pos1.i>0&&data[pos1.i-1][pos1.j][pos1.k]==1&&test[pos1.i-1][pos1.j][pos1.k]==0)
{
stack.push_back(pos(pos1.i-1,pos1.j,pos1.k));
test[pos1.i-1][pos1.j][pos1.k]=1;
connect++;
}
if(pos1.i<m-1&&data[pos1.i+1][pos1.j][pos1.k]==1&&test[pos1.i+1][pos1.j][pos1.k]==0)
{
stack.push_back(pos(pos1.i+1,pos1.j,pos1.k));
test[pos1.i+1][pos1.j][pos1.k]=1;
connect++;
}
if(pos1.j>0&&data[pos1.i][pos1.j-1][pos1.k]==1&&test[pos1.i][pos1.j-1][pos1.k]==0)
{
stack.push_back(pos(pos1.i,pos1.j-1,pos1.k));
test[pos1.i][pos1.j-1][pos1.k]=1;
connect++;
}
if(pos1.j<n-1&&data[pos1.i][pos1.j+1][pos1.k]==1&&test[pos1.i][pos1.j+1][pos1.k]==0)
{
stack.push_back(pos(pos1.i,pos1.j+1,pos1.k));
test[pos1.i][pos1.j+1][pos1.k]=1;
connect++;
}
if(pos1.k>0&&data[pos1.i][pos1.j][pos1.k-1]==1&&test[pos1.i][pos1.j][pos1.k-1]==0)
{
stack.push_back(pos(pos1.i,pos1.j,pos1.k-1));
test[pos1.i][pos1.j][pos1.k-1]=1;
connect++;
}
if(pos1.k<l-1&&data[pos1.i][pos1.j][pos1.k+1]==1&&test[pos1.i][pos1.j][pos1.k+1]==0)
{
stack.push_back(pos(pos1.i,pos1.j,pos1.k+1));
test[pos1.i][pos1.j][pos1.k+1]=1;
connect++;
}
}
if(connect>=t)
count+=connect;
}
}
cout<<count;
return 0;
}