PAT (Advanced Level) Practice - 1091 Acute Stroke(30 分)

简介: PAT (Advanced Level) Practice - 1091 Acute Stroke(30 分)

题目链接:点击打开链接

题目大意:给定一个三维数组,0表示正常1表示有肿瘤,肿瘤块的大小大于等于 st 才算作是肿瘤,让计算所有满足肿瘤块的大小。

解题思路:三维的广度优先搜索。X、Y、Z三个数组判断方向,对每一个点广度优先累计肿瘤块的大小,如果大于等于 st 就把结果累加。用 vis 数组标记当前的点有没有被访问过,被访问过的结点是不会再访问的。judge 判断是否超过了边界,或者是否当前结点为 0 不是肿瘤。

AC 代码

#include<bits/stdc++.h>#include<cmath>#define mem(a,b) memset(a,b,sizeof a)#define ssclr(ss) ss.clear(), ss.str("")#define INF 0x3f3f3f3f#define MOD 1000000007usingnamespacestd;
typedeflonglongll;
constintX[6]={1,0,0,-1,0,0};
constintY[6]={0,1,0,0,-1,0};
constintZ[6]={0,0,1,0,0,-1};
intm,n,l,st;
intg[1300][130][70], vis[1300][130][70];
structnode{
intx,y,z;
node(){}
node(intx,inty,intz):x(x),y(y),z(z){}
};
intjde(intx,inty,intz)
{
if(x<0||y<0||z<0||x>=m||y>=n||z>=l) return0;
if(vis[x][y][z]==1||g[x][y][z]==0) return0;
return1;
}
intbfs(intx,inty,intz)
{
intcnt=0,dx,dy,dz;
queue<node>q;
q.push(node(x,y,z));
vis[x][y][z]=1;
nodetp;
while(!q.empty())
    {
tp=q.front(); q.pop();
cnt++;
for(inti=0;i<6;i++)
        {
dx=X[i]+tp.x;
dy=Y[i]+tp.y;
dz=Z[i]+tp.z;
if(jde(dx,dy,dz))
vis[dx][dy][dz]=1, q.push(node(dx,dy,dz));
        }
    }
returncnt>=st?cnt:0;
}
intmain()
{
intrs=0;
scanf("%d%d%d%d",&m,&n,&l,&st);
for(intk=0;k<l;k++)
for(inti=0;i<m;i++)
for(intj=0;j<n;j++)
scanf("%d",&g[i][j][k]);
for(intk=0;k<l;k++)
for(inti=0;i<m;i++)
for(intj=0;j<n;j++)
if(!vis[i][j][k] &&g[i][j][k]==1)
rs+=bfs(i,j,k);
printf("%d\n",rs);
return0;
}
目录
相关文章
PAT (Advanced Level) Practice - 1045 Favorite Color Stripe(30 分)
PAT (Advanced Level) Practice - 1045 Favorite Color Stripe(30 分)
95 0
PAT (Advanced Level) Practice - 1045 Favorite Color Stripe(30 分)
|
算法
PAT (Advanced Level) Practice - 1033 To Fill or Not to Fill(25 分)
PAT (Advanced Level) Practice - 1033 To Fill or Not to Fill(25 分)
93 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
130 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
119 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
138 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
117 0
PAT (Advanced Level) Practice - 1053 Path of Equal Weight(30 分)
PAT (Advanced Level) Practice - 1053 Path of Equal Weight(30 分)
126 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
140 0
PAT (Advanced Level) Practice - 1130 Infix Expression(25 分)
PAT (Advanced Level) Practice - 1130 Infix Expression(25 分)
125 0
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
102 0