這題賊坑,我也是看了別人的博客提示才弄出來的
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;
}