ZOJ 1063 Space Station Shielding


Space Station Shielding

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem Description
Roger Wilco is in charge of the design of a low orbiting space station for the planet Mars. To simplify construction, the station is made up of a series of Airtight Cubical Modules (ACM's), which are connected together once in space. One problem that concerns Roger is that of (potentially) lethal bacteria that may reside in the upper atmosphere of Mars. Since the station will occasionally fly through the upper atmosphere, it is imperative that extra shielding be used on all faces of the ACM's touch, either edge to edge or face to face, that joint is sealed so no bacteria can sneak through. Any face of an ACM shared by another ACM will not need shielding, of course, nor will a face which cannot be reached from the outside. Roger could just put extra shielding on all of the faces of every ACM, but the cost would be prohibitive. Therefore, he wants to know the exact number of ACM faces which need the extra shielding. 

Input

Input consists of multiple problem instances. Each instance consists of a specification of a space station. We assume that each space station can fit into an n x m x k grid (1 <= n, m, k <= 60), where each grid cube may or may not contain an ACM. We number the grid cubes 0, 1, 2, 锟斤拷, kmn-1 as shown in the diagram below. Each space station specification then consists of the following: the first line contains four positive integers n m k l, where n, m and k are as described above and l is the number of ACM's in the station. This is followed by a set of lines which specify the l grid locations of the ACM's. Each of these lines contain 10 integers (except possibly the last). Each space station is fully connected (i.e., an astronaut can move from one ACM to any other ACM in the station without leaving the station). Values of n = m = k = l = 0 terminate input.

ZOJ 1063 Space Station Shielding - qhn999 - 码代码的猿猿

Output

For each problem instance, you should output one line of the form

The number of faces needing shielding is s.

Sample Input

2 2 1 3
0 1 3
3 3 3 26
0 1 2 3 4 5 6 7 8 9
10 11 12 14 15 16 17 18 19 20
21 22 23 24 25 26
0 0 0 0

Sample Output

The number of faces needing shielding is 14.
The number of faces needing shielding is 54.

 

Source
East Central North America 2001
 



#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int d_x[6]={0,0,0,0,1,-1};
const int d_y[6]={0,0,1,-1,0,0};
const int d_z[6]={1,-1,0,0,0,0};

int cube[66][66][66];
int ans;

struct Node
{
    int xx,yy,zz;
};

int n,m,k;
int l;

void floodfill(int x,int y,int z)
{
    Node p,w;
    p.xx=x;p.yy=y;p.zz=z;
    queue<Node> q;
    q.push(p);
    while(!q.empty())
    {
        w=q.front();
        q.pop();
        cube[w.xx][w.yy][w.zz]=1;
        for(int i=0;i<6;i++)
        {
             int tx=w.xx+d_x;
             int ty=w.yy+d_y;
             int tz=w.zz+d_z;

             p.xx=tx;  p.yy=ty;  p.zz=tz;

             if(tx>=0&&tx<=x+2&&ty>=0&&ty<=y+2&&tz>=0&&tz<=z+2)
             {
                 if(cube[tx][ty][tz]==1) continue;
                 else if(cube[tx][ty][tz]==0)
                 {
                    q.push(p);
                    cube[tx][ty][tz]=1;
                 }
                 else if(cube[tx][ty][tz]==2)
                 {
                         ans++;
                 }
             }
        }
    }
}

int main()
{
while(scanf("%d%d%d%d",&n,&m,&k,&l)!=EOF)
{
    if(n==0&&m==0&&k==0&&l==0) break;

    memset(cube,0,sizeof(cube));

    int num;  int x,y,z;
    for(int i=0;i<l;i++)
    {
        scanf("%d",&num);
        z=num/(m*n);
        y=(num-z*m*n)/n;
        x=num-z*m*n-n*y;
        //cout<<x<<","<<y<<","<<z<<endl;
        cube[x+1][y+1][z+1]=2;
    }
    cube[n+2][y+2][k+2]=1;

    ans=0;
    floodfill(n+2,m+2,k+2);

    printf("The number of faces needing shielding is %d. ",ans);
}

    return 0;
}

原文地址:https://www.cnblogs.com/CKboss/p/3350978.html