UVa 572 Oil Deposits(DFS)
Oil Deposits |
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides
the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil.
A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to
determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n,
the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input;
otherwise and .
Following this are m lines of n characters
each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@',
representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit
will not contain more than 100 pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
题意 计算@连通块的数量 典型的dfs应用
#include<cstdio>
#include<cstring>
using namespace std;
#define r i+x[k]
#define c j+y[k]
const int N = 105;
char mat[N][N];
int n, x[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
int m, y[8] = { -1, 0, 1, -1, 1, -1, 0, 1}; int dfs (int i, int j)
{
if (mat[i][j] == '*') return 0;
mat[i][j] = '*';
for (int k = 0; k < 8; ++k)
if (r > 0 && r <= n && c > 0 && c <= m && mat[r][c] == '@')
dfs (r, c);
return 1;
} int main()
{
while (scanf ("%d%d", &n, &m), n)
{
int ans = 0;
for (int i = 1; i <= n; ++i)
scanf ("%s", mat[i] + 1);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
ans += dfs (i, j);
printf ("%d\n", ans);
}
return 0;
}
UVa 572 Oil Deposits(DFS)的更多相关文章
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- uva 572 oil deposits——yhx
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...
- UVA - 572 Oil Deposits(dfs)
题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...
- UVa 572 - Oil Deposits (简单dfs)
Description GeoSurvComp地质调查公司负责探測地下石油储藏. GeoSurvComp如今在一块矩形区域探測石油.并把这个大区域分成了非常多小块.他们通过专业设备.来分析每一个小块中 ...
- UVa 572 Oil Deposits (Floodfill && DFS)
题意 :输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横竖以及对角方向),就是说它们属于同一个八连块. 分析 :可以考虑种子填充深搜的方法.两重for循 ...
- Uva 572 Oil Deposits
思路:可以用DFS求解.遍历这个二维数组,没发现一次未被发现的‘@’,便将其作为起点进行搜索.最后的答案,是这个遍历过程中发现了几次为被发现的‘@’ import java.util.*; publi ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- Oil Deposits(dfs)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
随机推荐
- 移动App測试实战:顶级互联网企业软件測试和质量提升最佳实践
这篇是计算机类的优质预售推荐>>>><移动App測试实战:顶级互联网企业软件測试和质量提升最佳实践> 国内顶级互联网公司測试实战经验总结.阿里.腾讯.京东.携程.百 ...
- 佳文分享:CAP定理
1976年6月4号,周5,在远离音乐会大厅的一个楼上的房间内,在位于Manchester的Lesser Free Trade Hall ,Sex Pistols 乐队(注:Sex Pistols的经理 ...
- Andriod中绘(画)图----Canvas的使用具体解释
转载请注明出处:http://blog.csdn.net/qinjuning 因为在网络上找到关于Canvas的使用都比較抽象,或许是我的逻辑思维不太好吧,总是感觉理解起来比較困难, 尤其是对 ...
- ImageMagick的安装及使用
近期在使用ImageMagick处理酒店团购图片,写篇博客小小的总结下它的安装及使用方法.ImageMagick是一套功能强大且免费的图片处理开发包,能够用来读,写和处理多种格式的图片文件,总之非常强 ...
- 只能从脚本中调用在类定义上有[ScriptService]属性的Web服务问题的解决方案
ajax调用webservice中的接口时, 会出现[只能从脚本中调用在类定义上有[ScriptService]属性的...]的异常. 这是因为, 在.net3.5中, 访问web服务, 要对web服 ...
- c#事件委托
转载地址:http://www.cnblogs.com/wudiwushen/archive/2010/04/20/1703368.html 从序言中,大家应该对委托和事件的重要性有点了解了吧, ...
- Tkinter隐藏窗口再让他显示出来的例子
隐藏主要是 : withdraw()函数. 重新显示出来主要是: update()和deiconify()函数. 来源:http://www.blog.pythonlibrary.org/2012/0 ...
- java中由类名和方法名字符串实现其调用【反射机制】
js里通过eval()函数,在知道某个方法名是可以实现调用该方法,那么在java里边又怎么实现的呢? java里边是通过反射机制来实现,代码如下: import java.lang.reflect.M ...
- Hadoop单机版安装,配置,运行
Hadoop是最近非常流行的东东啦,但是乍一看都觉得是集群的东东,其实在单机版上安装Hadoop也是可以的,并且安装好以后可以很方便的进行程序的调试,调试好程序以后再丢到集群中,放心的算吧,呵呵.. ...
- 《JavaScript设计模式与开发实践》读书笔记之命令模式
1.命令模式 1.1 传统的命令模式 命令模式的应用场景:请求的发送者和请求接收者消除彼此耦合关系 以页面点击按钮为例 点击按钮后,借助命令对象,解开按钮和负责具体行为对象之间的耦合 <body ...