您的位置:首页 > 其它

1091. Acute Stroke (30)--BFS数多少块儿

2018-03-08 13:43 417 查看
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>

#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>

using namespace std;

struct node
{
int x,y,z;
};
int N,M,L,T;
int g[2000][200][100]={0};
int book[2000][200][100]={0};
int X[6]={1,-1,0,0,0,0};
int Y[6]={0,0,1,-1,0,0};
int Z[6]={0,0,0,0,1,-1};

bool judge(node a)
{
int x=a.x;
int y=a.y;
int z=a.z;

if( (x<0||y<0||z<0)||(x>=M||y>=N||z>=L) )//出界
return 0;
if(g[x][y][z]==0)//点是0
return 0;
if(book[x][y][z]==1)//已被访问过
return 0;

return 1;
}

int bfs(int x,int y,int z)
{
int cou=0;

book[x][y][z]=1;//原点先标记
cou++;

node tnode;//搜索的元素
tnode.x=x;  tnode.y=y;  tnode.z=z;
queue<node> q;
q.push(tnode);
while(!q.empty())
{
for(int i=0;i<6;i++)
{
int nx=q.front().x+X[i];
int ny=q.front().y+Y[i];
int nz=q.front().z+Z[i];
node tnode2;//周围的node
tnode2.x=nx;  tnode2.y=ny;  tnode2.z=nz;

if(judge(tnode2)==1)
{
q.push(tnode2);
book[nx][ny][nz]=1;
cou++;
}
}

q.pop();
}

return cou;
}
int main()
{
// freopen("in.txt","r",stdin);

scanf("%d %d %d %d",&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",&g[i][j][k]);
}
}
}

int ans=0;
for(int k=0;k<L;k++)
{
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
if(g[i][j][k]==1&&book[i][j][k]==0)
{
int cou=bfs(i,j,k);//对每个点进行搜索
if(cou>=T)
{
ans+=cou;
}

}
}
}
}

printf("%d",ans);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: