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.
figstroke.jpg
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
题解
此题输入是个三维图形,然后统计三维图形中标记的符合条件的面积总和。
注意结合图片,图片说明了点之间的联系方式。
深搜递归
因为递归层数过多出现了段错误。
#include<iostream>
using namespace std;
long long a[65][1300][130];
long long M,N,L,T,cnt;
int b[][3]={{0,-1,0},{0,1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};
void def(int x,int y,int z){
for(int i=0;i<6;i++){
if(x+b[i][0]<M&&y+b[i][1]<N&&x+b[i][0]>=0&&y+b[i][1]>=0&&z+b[i][2]<L&&z+b[i][2]>=0&&a[z+b[i][2]][x+b[i][0]][y+b[i][1]]==1){
cnt++;
a[z+b[i][2]][x+b[i][0]][y+b[i][1]]=0;
def(x+b[i][0],y+b[i][1],z+b[i][2]);
}
}
}
int main(){
cin>>M>>N>>L>>T;
long long sum=0;
for(int i=0;i<L;i++)
for(int j=0;j<M;j++)
for(int k=0;k<N;k++)
cin>>a[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(a[i][j][k]==1){
cnt=1;
a[i][j][k]=0;
def(j,k,i);
if(cnt>=T)
sum+=cnt;
}
}
cout<<sum;
return 0;
}
广搜队列
队列存储了递归的待定模式,然后通过从队列提取数据来运行待定的递归。
由点及点(深搜),变为由点及面(广搜)。
#include<iostream>
#include<queue>
using namespace std;
long long a[65][1300][130];
long long M,N,L,T,cnt;
struct node{
long long x,y,z;
};
// 使用队列可以代替bfs递归,因为递归层数太多会导致段错误。
queue<node> q;
int b[][3]={{0,-1,0},{0,1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};
void def(int x,int y,int z){
node item={x,y,z};
q.push(item);
while(!q.empty()){
item=q.front();
q.pop();
x=item.x;
y=item.y;
z=item.z;
for(int i=0;i<6;i++){
if(x+b[i][0]<M&&y+b[i][1]<N&&x+b[i][0]>=0&&y+b[i][1]>=0&&z+b[i][2]<L&&z+b[i][2]>=0&&a[z+b[i][2]][x+b[i][0]][y+b[i][1]]==1){
cnt++;
a[z+b[i][2]][x+b[i][0]][y+b[i][1]]=0;
item={x+b[i][0],y+b[i][1],z+b[i][2]};
q.push(item);
}
}
}
}
int main(){
cin>>M>>N>>L>>T;
long long sum=0;
for(int i=0;i<L;i++)
for(int j=0;j<M;j++)
for(int k=0;k<N;k++)
cin>>a[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(a[i][j][k]==1){
cnt=1;
a[i][j][k]=0;
def(j,k,i);
if(cnt>=T)
sum+=cnt;
}
}
cout<<sum;
return 0;
}