这题贼坑,我也是看了别人的博客提示才弄出来的
1.理解题意,大概是要求联通的1的个数大于T的区域有多少,联通的三维的联通(6个方向)
2.不能用递归的DFS,测试案例里联通区域有几十万这么大,用递归系统的栈会溢出的,最后5分会过不去,用BFS就能过了
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
short V[62][1300][130];
short isread[62][1300][130];
struct BFS{
int t1,t2,t3;
}B[10000000];
int N,M,L,T,coo,top=0,end1=0;
void GetLevel(BFS s){
BFS temp,t;
isread[s.t1][s.t2][s.t3]=1;
B[top++]=s;
while(top!=end1){
t=B[end1++];coo++;
int a=t.t1,b=t.t2,c=t.t3;
if(isread[a-1][b][c]==0&&V[a-1][b][c]==1){
temp=t;
temp.t1--;
isread[temp.t1][temp.t2][temp.t3]=1;
B[top++]=temp;
}
if(isread[a+1][b][c]==0&&V[a+1][b][c]==1){
temp=t;
temp.t1++;
isread[temp.t1][temp.t2][temp.t3]=1;
B[top++]=temp;
}
if(isread[a][b-1][c]==0&&V[a][b-1][c]==1){
temp=t;
temp.t2--;
isread[temp.t1][temp.t2][temp.t3]=1;
B[top++]=temp;
}
if(isread[a][b+1][c]==0&&V[a][b+1][c]==1){
temp=t;
temp.t2++;
isread[temp.t1][temp.t2][temp.t3]=1;
B[top++]=temp;
}
if(isread[a][b][c-1]==0&&V[a][b][c-1]==1){
temp=t;
temp.t3--;
isread[temp.t1][temp.t2][temp.t3]=1;
B[top++]=temp;
}
if(isread[a][b][c+1]==0&&V[a][b][c+1]==1){
temp=t;
temp.t3++;
isread[temp.t1][temp.t2][temp.t3]=1;
B[top++]=temp;
}
}
}
int main(){
int i,j,k,count=0,temp;
BFS s;
cin>>M>>L>>N>>T;
for(i=0;i<=N+1;i++)
for(j=0;j<=M+1;j++)
for(k=0;k<=L+1;k++){
V[i][j][k]=0;
}
for(i=1;i<=N;i++)
for(j=1;j<=M;j++)
for(k=1;k<=L;k++){
scanf("%d",&V[i][j][k]);
}
for(i=1;i<=N;i++)
for(j=1;j<=M;j++)
for(k=1;k<=L;k++){
if(isread[i][j][k]==0&&V[i][j][k]==1){
coo=0;
s.t1=i;
s.t2=j;
s.t3=k;
top=0;
end1=0;
GetLevel(s);
if(coo>=T)
count+=coo;
}
}
cout<<count;
}