PAT甲1091. Acute Stroke (30)

#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;

struct Node
{
    int x,y,z;
    int count;

}node;
int v=0;

int X[6]={0,0,0,0,1,-1};
int Y[6]={0,0,1,-1,0,0};
int Z[6]={1,-1,0,0,0,0};
int a[1300][130][61],inq[1300][130][61]={false};
int M,N,L,T;


bool test(int x,int y,int z)
{
    if(x>=M||x<0||y>=N||y<0||z>=L||z<0)return false;
    if(a[x][y][z]==0||inq[x][y][z]==true)return false;
    return true;
}

void BFS(int x,int y,int z)
{
    queue<Node> q;
    node.x=x;
    node.y=y;
    node.z=z;
    q.push(node);
    inq[x][y][z]=true;
    int c=0;
    while(!q.empty())
    {
        Node top=q.front();
        q.pop();
        c++;
        for(int i=0;i<6;i++)
        {
            int newx=top.x+X[i];
            int newy=top.y+Y[i];
            int newz=top.z+Z[i];
            if(test(newx,newy,newz))
            {
                node.x=newx;
                node.y=newy;
                node.z=newz;
                q.push(node);
                inq[newx][newy][newz]=true;
            }
        }
    }
    if(c>=T)
    {
        v+=c;
    }
}

int main()  
{   
    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",&a[i][j][k]);
            }
        }
    }
    for(int k=0;k<L;k++)
    {
        for(int i=0;i<M;i++)
        {
            for(int j=0;j<N;j++)
            {
                if(a[i][j][k]==1&&inq[i][j][k]==false)
                {
                    BFS(i,j,k);
                }
            }
        }
    }
    printf("%d\n",v);
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yhy489275918/article/details/80136253