天天看点

Space Station Shielding(洪泛算法)

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. 

Space Station Shielding(洪泛算法)

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.      

第一次接触洪泛算法+搜索

洪泛算法:

图形学中Flood Fill是满水法填充,是用来填充区域的。就好比在一个地方一直到水,水会往四周满延开,直到高地阻挡。Flood Fill就是从一个点开始往四周寻找相同的点填充,直到有不同的点为止。

我们用的Flood Fill和这个差不多原理,就是BFS的一种形式.

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<stack>
#include<algorithm>

using namespace std;

int n,m,l,k,ans;
int arr[70][70][70],flag[70][70][70];

struct node
{
    int x,y,z;
};

void bfs()
{
    ans=0;
    queue<node>q;
    node now,next;
    now.x=0;  now.y=0; now.z=0;
    q.push(now);
    while(!q.empty())
    {
        next=q.front();
        q.pop();
        if(flag[next.x][next.y][next.z])
        continue;
        flag[next.x][next.y][next.z]=1;
        if(next.x-1>=0)//向左
        {
            if(arr[next.x-1][next.y][next.z]==0)
            {
                if(!flag[next.x-1][next.y][next.z])
                {
                    now.x=next.x-1; now.y=next.y; now.z=next.z;
                    q.push(now);
                }
            }
            else
            ans++;
        }
        if(next.x<=n)//向右
        {
            if(arr[next.x+1][next.y][next.z]==0)
            {
                if(!flag[next.x+1][next.y][next.z])
                {
                    now.x=next.x+1; now.y=next.y; now.z=next.z;
                    q.push(now);
                }
            }
            else
            ans++;
        }
        if(next.y-1>=0)//向后
        {
            if(arr[next.x][next.y-1][next.z]==0)
            {
                if(!flag[next.x][next.y-1][next.z])
                {
                    now.x=next.x; now.y=next.y-1; now.z=next.z;
                    q.push(now);
                }
            }
            else
            ans++;
        }
        if(next.y<=m)//向前
        {
            if(arr[next.x][next.y+1][next.z]==0)
            {
                if(!flag[next.x][next.y+1][next.z])
                {
                    now.x=next.x; now.y=next.y+1; now.z=next.z;
                    q.push(now);
                }
            }
            else
            ans++;
        }
        if(next.z-1>=0)//向下
        {
            if(arr[next.x][next.y][next.z-1]==0)
            {
                if(!flag[next.x][next.y][next.z-1])
                {
                    now.x=next.x; now.y=next.y; now.z=next.z-1;
                    q.push(now);
                }
            }
            else
            ans++;
        }
        if(next.z<=k)//向上
        {
            if(arr[next.x][next.y][next.z+1]==0)
            {
                if(!flag[next.x][next.y][next.z+1])
                {
                    now.x=next.x; now.y=next.y; now.z=next.z+1;
                    q.push(now);
                }
            }
            else
            ans++;
        }
    }
}

int main()
{
    while(cin>>n>>m>>k>>l)
    {
        if(n==0 && m==0 && k==0 && l==0)
        break;
        int a;
        memset(arr,0,sizeof(arr));
        memset(flag,0,sizeof(flag));
//        count=0;
        for(int i=0;i<l;i++)
        {
            cin>>a;
            arr[a%(m*n)%n+1][a%(m*n)/n+1][a/(m*n)+1]=1;//转化为i*i*i的形式
        }
        bfs();
        cout<<"The number of faces needing shielding is "<<ans<<".\n";
    }
    return 0;
}