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

 

题目大意:给你一个正长方体,长宽高分别为n、m、k,这个长方体由n*m*k个1*1*1的小立方体组成,把这些小立方体编号为0-(n*m*k-1),再给l个编号,表示这些小立方体是存在的,否则就是不存在的。求最总整个图形的外表面积。

解题思路:首先想到,以其中一个存在的小立方体开始,往上下左右前后六个方向搜索,如果这个方向上有小方块,就转移到这个小方块上继续搜索,如果没有,则表面积+1,但是这个方法求出来的包含内表面积!!!于是采用反向思维,在这个立方体周围包一层不存在立方体(这里的不存在就是标记该位置单位立方体tab[i][j][k]=0,存在为1),然后再按上面的方法改成搜索“不存在的小立方体”,只搜索最外圈,那么我们得到的结果就是最外圈那些“不存在的小立方体”所构成的部分的内外表面积之和,它的内表面积就是我们要求的“存在的小立方体组成的物体”的外表面积了。而它的外表面积就是(n*m+m*k+n*k)*2,其实可以不用算,在搜索的时候如果是边界不用+1就行了。注意要用BFS,刚开始用DFS栈溢出了。

知识扩展:这题是标准的洪泛算法的题目,记得勘测油田的那道简单搜索题吗?还有某些画图软件上的把图像上相连的某种颜色换成其他的,你点一块其他全部变化啦。这些都是洪泛算法的应用。本题意思是有一个由单位立方体组成的长方体,有些单位立方体内有宇航员,而外太空的毒气可以渗入没有贴防护膜的房间,所以要在某些必要的位置贴防护膜来防止毒气进入太空站空间。

相关链接:维基百科——洪泛算法:http://zh.wikipedia.org/zh-cn/Flood_fill

图像处理之泛洪填充算法(Flood Fill Algorithm) :http://blog.csdn.net/jia20003/article/details/8908464

洪泛算法讲解:http://www.nocow.cn/index.php/Flood_Fill

 #include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int n,m,k,l;
bool tab[][][],visit[][][];
int face_num;
class W3{
public:
int x,y,z;
W3& set(int xx,int yy,int zz){
x=xx;y=yy;z=zz;
return *this;
}
};
void bfs(){
face_num=;
queue<W3> Q;
W3 temp;
Q.push(temp.set(,,));
while(!Q.empty()){
W3 Top=Q.front();Q.pop();
if(visit[Top.x][Top.y][Top.z])continue;
visit[Top.x][Top.y][Top.z]=;
if(Top.x->=){
if(tab[Top.x-][Top.y][Top.z]==){
if(!visit[Top.x-][Top.y][Top.z])Q.push(temp.set(Top.x-,Top.y,Top.z));
}
else face_num++;
}//左走一格
if(Top.x<=n){
if(tab[Top.x+][Top.y][Top.z]==){
if(!visit[Top.x+][Top.y][Top.z])Q.push(temp.set(Top.x+,Top.y,Top.z));
}
else face_num++;
}//右走一格
if(Top.y->=){
if(tab[Top.x][Top.y-][Top.z]==){
if(!visit[Top.x][Top.y-][Top.z])Q.push(temp.set(Top.x,Top.y-,Top.z));
}
else face_num++;
}//后走一格
if(Top.y<=m){
if(tab[Top.x][Top.y+][Top.z]==){
if(!visit[Top.x][Top.y+][Top.z])Q.push(temp.set(Top.x,Top.y+,Top.z));
}
else face_num++;
}//前走一格
if(Top.z->=){
if(tab[Top.x][Top.y][Top.z-]==){
if(!visit[Top.x][Top.y][Top.z-])Q.push(temp.set(Top.x,Top.y,Top.z-));
}
else face_num++;
}//下走一格
if(Top.z<=k){
if(tab[Top.x][Top.y][Top.z+]==){
if(!visit[Top.x][Top.y][Top.z+])Q.push(temp.set(Top.x,Top.y,Top.z+));
}
else face_num++;
}//上走一格
}
}
int main(){
while(cin>>n>>m>>k>>l){
if(!n && !m && !k && !l)break;
memset(tab,,sizeof(tab));
memset(visit,,sizeof(visit));
int temp1;
for(int i=;i<l;i++){
cin>>temp1;
tab[temp1%(m*n)%n+][temp1%(m*n)/n+][temp1/(n*m)+]=;
}//输入数据并转换为tab[][][]的3维矩阵(这里从1,1,1开始,最外层相当于无人方块)
bfs();
cout<<"The number of faces needing shielding is "<<face_num<<".\n";
}return ;
}

[ACM_搜索] POJ 1096 Space Station Shielding (搜索 + 洪泛算法Flood_Fill)的更多相关文章

  1. poj 2031 Building a Space Station【最小生成树prime】【模板题】

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepte ...

  2. (最小生成树) Building a Space Station -- POJ -- 2031

    链接: http://poj.org/problem?id=2031 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6011 ...

  3. POJ 3126 Prime Path 广度优先搜索 难度:0

    http://poj.org/problem?id=3126 搜索的时候注意 1:首位不能有0 2:可以暂时有没有出现在目标数中的数字 #include <cstdio> #include ...

  4. POJ 2031 Building a Space Station【经典最小生成树】

    链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  5. Building a Space Station POJ - 2031

    Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...

  6. Building a Space Station POJ 2031 【最小生成树 prim】

    http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...

  7. POJ 2031 Building a Space Station

    3维空间中的最小生成树....好久没碰关于图的东西了.....              Building a Space Station Time Limit: 1000MS   Memory Li ...

  8. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepte ...

  9. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...

随机推荐

  1. 8.12 CSS知识点5

    背景原点 background-origin 设置元素背景图片的原始起始位置,必须保证背景是background-repeat为no-repeat此属性才会生效. 语法: background-ori ...

  2. VC++ WINDOWS自定义消息范围

    WINDOWS自定义消息WM_USER和WM_APP WM_USER常量是Windows帮助应用程序定义私有窗口类里的私有消息,通常使用WM_USER+一个整数值,但总值不能超过0x7FFF(十进制: ...

  3. LINUX内核分析第七周学习总结:可执行程序的装载

    LINUX内核分析第七周学习总结:可执行程序的装载 韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/cours ...

  4. 构建ASP.NET网站十大必备工具(1)

    最近使用ASP.NET为公司构建了一个简单的公共网站(该网站的地址:http://superexpert.com/).在这个过程中,我们使用了数量很多的免费工具,如果把构建ASP.NET网站的必备工具 ...

  5. 绕过/*,web.xml直接访问jsp

    web.xml中如果配置了/* 全匹配,那么不能用servet去响应页面返回了,因为全都被会/*拦截. <servlet> <servlet-name>validateAuth ...

  6. Ubuntu下搭建Android编译环境

    Ubuntu一台新机器的一些环境搭建新增一个3TB的硬盘,挂载方法,大于2TB的得parted来进行分区1: sudo parted /dev/sda2: mklabel gpt3: unit TB4 ...

  7. 运用CSS和JS写的大图轮播-带箭头

    <style type="text/css"> #datu { width:500px; height:400px; position:relative; margin ...

  8. [转载]Altium规则详解及设置

    在Altium中进行PCB的设计时,经常会使用规则(Rule)来进行限定以确定线宽孔径等参数,此文将简要的介绍规则中的一些标量代表了什么. Electrical——电气规则.安全间距,线网连接等 Ro ...

  9. POST 和GET传输的最大容量分别是多少?

    get 是通过URL提交数据,因此GET可提交的数据量就跟URL所能达到的最大长度有直接关系.很多文章都说GET方式提交的数据最多只能是1024字节,而 实际上,URL不存在参数上限的问题,HTTP协 ...

  10. Linux内核同步机制--转发自蜗窝科技

    Linux内核同步机制之(一):原子操作 http://www.wowotech.net/linux_kenrel/atomic.html 一.源由 我们的程序逻辑经常遇到这样的操作序列: 1.读一个 ...