poj 1096 & zoj 1063 Space Station Shielding (搜索 floodfill )

Space Station Shielding
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1342 Accepted: 477

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. 

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



题意:

可以理解为把给你的这些方块放入水里,会浸到多少个面。


思路:

沿外围扫描进去,把外面的全部能标记上,然后依次访问每个方块的6个面,如果通过这个面到达的方块访问了则可浸到,ans++。


感想:

被按照x、y、z优先的顺序坑了好久。。。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 65
#define MAXN 200005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 0.000001
typedef long long ll;
using namespace std;

int n,m,ans,cnt,tot,flag;
int h,k;
struct Node
{
    int x,y,z;
}now,cur;
vector<Node>a;
int dx[]={0,0,0,0,1,-1};
int dy[]={0,0,-1,1,0,0};
int dz[]={1,-1,0,0,0,0};
bool vis[maxn][maxn][maxn];
bool out[maxn][maxn][maxn];

bool isok(int x,int y,int z)
{
    if(x>=0&&x<n&&y>=0&&y<m&&z>=0&&z<h) return true ;
    return false ;
}
void dfs(int x,int y,int z)
{
    if(vis[x][y][z]) return ;
    out[x][y][z]=1;
    int i,j,t,tx,ty,tz;
    for(i=0;i<6;i++)
    {
        tx=x+dx[i];
        ty=y+dy[i];
        tz=z+dz[i];
        if(isok(tx,ty,tz)&&!out[tx][ty][tz])
        {
            dfs(tx,ty,tz);
        }
    }
}
void solve()
{
    int i,j,t,u,x,y,z,sz;
    int tx,ty,tz;
    memset(out,0,sizeof(out));
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(!out[i][j][0]) dfs(i,j,0);
            if(!out[i][j][h-1]) dfs(i,j,h-1);
        }
    }
    for(i=0;i<m;i++)
    {
        for(j=0;j<h;j++)
        {
            if(!out[0][i][j]) dfs(0,i,j);
            if(!out[n-1][i][j]) dfs(n-1,i,j);
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<h;j++)
        {
            if(!out[i][0][j]) dfs(i,0,j);
            if(!out[i][m-1][j]) dfs(i,m-1,j);
        }
    }
    sz=a.size();
    ans=0;
    for(i=0;i<sz;i++)
    {
        x=a[i].x,y=a[i].y,z=a[i].z;
        cnt=0;
        for(j=0;j<6;j++)
        {
            tx=x+dx[j];
            ty=y+dy[j];
            tz=z+dz[j];
            if(tx<0||tx>=n||ty<0||ty>=m||tz<0||tz>=h)
            {
                if(tx==-1||tx==n||ty==-1||ty==m||tz==-1||tz==h) ans++;
                continue ;
            }
            if(out[tx][ty][tz]) ans++;
        }
    }
}
int main()
{
    int i,j,t;
    int x,y,z;
    while(~scanf("%d%d%d%d",&n,&m,&h,&k))
    {
        if(!(n||m||h||k)) break ;
        a.clear();
        memset(vis,0,sizeof(vis));
        for(i=1;i<=k;i++)
        {
            scanf("%d",&t);
            z=t/(n*m);
            int u=t-n*m*z;
            y=u/n;
            x=u%n;
            vis[x][y][z]=1;
            cur.x=x,cur.y=y,cur.z=z;
            a.push_back(cur);
        }
        solve();
        printf("The number of faces needing shielding is %d.\n",ans);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值