http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=63
题目意思是:在一个N*M*K的三维立方体中,有些块是住着人的,而立方体外是有毒气体,现在必须给每个块的六面装上防毒墙,问需要多少面防毒强。当然连在一起的可以不装防毒强。这是一道经典的floodfill,但是这道题目有一个特别的地方是,如果没有住人的块,毒气通过外部会扩散来,但是没有住人的块,若六面都被住人的包围,就不会被扩散。
对外层的方块做标记,住人的方块若和外面相连则sum++。做标记时深搜会爆栈,用广搜。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <queue>
#include <time.h>
#include <list>
#include <stack>
using namespace std;
const double eps=1e-7;
const long long INF=(1<<30)-1;
#define N 500005
#define M 1000000000
typedef struct pp{int x,y,z;} point;
bool a[62][62][62],b[62][62][62],s[62][62][62];
int n,m,k,sum,c[70][70][70];
void zb(int num,int &x,int &y,int &z)
{
z=num/(n*m);
num%=(n*m);
y=num/n;
num%=n;
x=num;
x++;
y++;
z++;
}
void sur(point p1)
{
stack<point> p;
p.push(p1);
point p2,p3;
while (!p.empty())
{
p3=p.top();
p.pop();
int x,y,z;
x=p3.x;
y=p3.y;
z=p3.z;
if (x>0 && c[x-1][y][z]==0)
{
p2=p3;
c[x-1][y][z]=2;
p2.x=x-1;
p.push(p2);
}
if (x<n+1 && c[x+1][y][z]==0)
{
p2=p3;
c[x+1][y][z]=2;
p2.x=x+1;
p.push(p2);
}
if (y>0 && c[x][y-1][z]==0)
{
p2=p3;
c[x][y-1][z]=2;
p2.y=y-1;
p.push(p2);
}
if (y<m+1 && c[x][y+1][z]==0)
{
p2=p3;
c[x][y+1][z]=2;
p2.y=y+1;
p.push(p2);
}
if (z>0 && c[x][y][z-1]==0)
{
p2=p3;
c[x][y][z-1]=2;
p2.z=z-1;
p.push(p2);
}
if (z<k+1 && c[x][y][z+1]==0)
{
p2=p3;
c[x][y][z+1]=2;
p2.z=z+1;
p.push(p2);
}
}
}
void cou(int x,int y,int z)
{
if (c[x-1][y][z]==2) sum++;
if (c[x+1][y][z]==2) sum++;
if (c[x][y-1][z]==2) sum++;
if (c[x][y+1][z]==2) sum++;
if (c[x][y][z-1]==2) sum++;
if (c[x][y][z+1]==2) sum++;
}
int main()
{
//freopen("a","r",stdin);
int l,i,j,ii;
while(1)
{
scanf("%d%d%d%d",&n,&m,&k,&l);
if (n==0 && m==0 && k==0 && l==0) break;
sum=0;
memset(c,0,sizeof(c));
int x,y,z;
for (i=1;i<=l;i++)
{
scanf("%d",&j);
zb(j,x,y,z);
c[x][y][z]=1;
}
point p1;
p1.x=p1.y=p1.z=0;
sur(p1);
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
for (ii=1;ii<=k;ii++)
if (c[i][j][ii]==1) cou(i,j,ii);
printf("The number of faces needing shielding is %d.\n",sum);
}
return 0;
}