[ACM_搜索] POJ 1096 Space Station Shielding (搜索 + 洪泛算法Flood_Fill)

简介:


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

复制代码
 1 #include<iostream>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 int n,m,k,l;
 6 bool tab[70][70][70],visit[70][70][70];
 7 int face_num;
 8 class W3{
 9 public:
10     int x,y,z;
11     W3& set(int xx,int yy,int zz){
12         x=xx;y=yy;z=zz;
13         return *this;
14     }
15 };
16 void bfs(){
17     face_num=0;
18     queue<W3> Q;
19     W3 temp;
20     Q.push(temp.set(0,0,0));
21     while(!Q.empty()){
22         W3 Top=Q.front();Q.pop();
23         if(visit[Top.x][Top.y][Top.z])continue;
24         visit[Top.x][Top.y][Top.z]=1;
25         if(Top.x-1>=0){
26             if(tab[Top.x-1][Top.y][Top.z]==0){
27                 if(!visit[Top.x-1][Top.y][Top.z])Q.push(temp.set(Top.x-1,Top.y,Top.z));
28             }
29             else face_num++;
30         }//左走一格
31         if(Top.x<=n){
32             if(tab[Top.x+1][Top.y][Top.z]==0){
33                 if(!visit[Top.x+1][Top.y][Top.z])Q.push(temp.set(Top.x+1,Top.y,Top.z));
34             }
35             else face_num++;
36         }//右走一格
37         if(Top.y-1>=0){
38             if(tab[Top.x][Top.y-1][Top.z]==0){
39                 if(!visit[Top.x][Top.y-1][Top.z])Q.push(temp.set(Top.x,Top.y-1,Top.z));
40             }
41             else face_num++;
42         }//后走一格
43         if(Top.y<=m){
44             if(tab[Top.x][Top.y+1][Top.z]==0){
45                 if(!visit[Top.x][Top.y+1][Top.z])Q.push(temp.set(Top.x,Top.y+1,Top.z));
46             }
47             else face_num++;
48         }//前走一格
49         if(Top.z-1>=0){
50             if(tab[Top.x][Top.y][Top.z-1]==0){
51                 if(!visit[Top.x][Top.y][Top.z-1])Q.push(temp.set(Top.x,Top.y,Top.z-1));
52             }
53             else face_num++;
54         }//下走一格
55         if(Top.z<=k){
56             if(tab[Top.x][Top.y][Top.z+1]==0){
57                 if(!visit[Top.x][Top.y][Top.z+1])Q.push(temp.set(Top.x,Top.y,Top.z+1));
58             }
59             else face_num++;
60         }//上走一格
61     }
62 }
63 int main(){
64     while(cin>>n>>m>>k>>l){
65         if(!n && !m && !k && !l)break;
66         memset(tab,0,sizeof(tab));
67         memset(visit,0,sizeof(visit));
68         int temp1;
69         for(int i=0;i<l;i++){
70             cin>>temp1;
71             tab[temp1%(m*n)%n+1][temp1%(m*n)/n+1][temp1/(n*m)+1]=1;
72         }//输入数据并转换为tab[][][]的3维矩阵(这里从1,1,1开始,最外层相当于无人方块)
73         bfs();
74         cout<<"The number of faces needing shielding is "<<face_num<<".\n";
75     }return 0;
76 }
复制代码
相关文章
|
13天前
|
机器学习/深度学习 算法
算法系列之搜索算法-深度优先搜索DFS
深度优先搜索和广度优先搜索一样,都是对图进行搜索的算法,目的也都是从起点开始搜索,直到到达顶点。深度优先搜索会沿着一条路径不断的往下搜索,直到不能够在继续为止,然后在折返,开始搜索下一条候补路径。
90 62
算法系列之搜索算法-深度优先搜索DFS
|
14天前
|
存储 算法
算法系列之搜索算法-广度优先搜索BFS
广度优先搜索(BFS)是一种非常强大的算法,特别适用于解决最短路径、层次遍历和连通性问题。在面试中,掌握BFS的基本实现和应用场景,能够帮助你高效解决许多与图或树相关的问题。
30 1
算法系列之搜索算法-广度优先搜索BFS
|
18天前
|
算法
基于SOA海鸥优化算法的三维曲面最高点搜索matlab仿真
本程序基于海鸥优化算法(SOA)进行三维曲面最高点搜索的MATLAB仿真,输出收敛曲线和搜索结果。使用MATLAB2022A版本运行,核心代码实现种群初始化、适应度计算、交叉变异等操作。SOA模拟海鸥觅食行为,通过搜索飞行、跟随飞行和掠食飞行三种策略高效探索解空间,找到全局最优解。
|
4月前
|
算法 搜索推荐 数据库
二分搜索:高效的查找算法
【10月更文挑战第29天】通过对二分搜索的深入研究和应用,我们可以不断挖掘其潜力,为各种复杂问题提供高效的解决方案。相信在未来的科技发展中,二分搜索将继续发挥着重要的作用,为我们的生活和工作带来更多的便利和创新。
91 1
|
5月前
|
算法 决策智能
基于禁忌搜索算法的VRP问题求解matlab仿真,带GUI界面,可设置参数
该程序基于禁忌搜索算法求解车辆路径问题(VRP),使用MATLAB2022a版本实现,并带有GUI界面。用户可通过界面设置参数并查看结果。禁忌搜索算法通过迭代改进当前解,并利用记忆机制避免陷入局部最优。程序包含初始化、定义邻域结构、设置禁忌列表等步骤,最终输出最优路径和相关数据图表。
|
6月前
|
大数据 UED 开发者
实战演练:利用Python的Trie树优化搜索算法,性能飙升不是梦!
在数据密集型应用中,高效搜索算法至关重要。Trie树(前缀树/字典树)通过优化字符串处理和搜索效率成为理想选择。本文通过Python实战演示Trie树构建与应用,显著提升搜索性能。Trie树利用公共前缀减少查询时间,支持快速插入、删除和搜索。以下为简单示例代码,展示如何构建及使用Trie树进行搜索与前缀匹配,适用于自动补全、拼写检查等场景,助力提升应用性能与用户体验。
91 2
|
5月前
|
存储 算法 C++
【搜索算法】 跳马问题(C/C++)
【搜索算法】 跳马问题(C/C++)
|
5月前
|
人工智能 算法 Java
【搜索算法】数字游戏(C/C++)
【搜索算法】数字游戏(C/C++)
|
7月前
|
算法
【算法】递归、搜索与回溯——汉诺塔
【算法】递归、搜索与回溯——汉诺塔
|
7月前
|
算法
【算法】递归、搜索与回溯——简介
【算法】递归、搜索与回溯——简介

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等