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)的更多相关文章

  1. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  2. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  3. uva 572 oil deposits——yhx

    Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...

  4. UVA - 572 Oil Deposits(dfs)

    题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...

  5. UVa 572 - Oil Deposits (简单dfs)

    Description GeoSurvComp地质调查公司负责探測地下石油储藏. GeoSurvComp如今在一块矩形区域探測石油.并把这个大区域分成了非常多小块.他们通过专业设备.来分析每一个小块中 ...

  6. UVa 572 Oil Deposits (Floodfill && DFS)

    题意 :输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横竖以及对角方向),就是说它们属于同一个八连块. 分析 :可以考虑种子填充深搜的方法.两重for循 ...

  7. Uva 572 Oil Deposits

    思路:可以用DFS求解.遍历这个二维数组,没发现一次未被发现的‘@’,便将其作为起点进行搜索.最后的答案,是这个遍历过程中发现了几次为被发现的‘@’ import java.util.*; publi ...

  8. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  9. Oil Deposits(dfs)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. Zigbee开发(1)

    只是研究zigbee的技术,也许后续的博客不会有很及时的更新,有时间 写一点东西能让大家有所收获吧. 环境搭建 Windows 64位的操作系统 IAR7.6 for 8051 ZStack CC25 ...

  2. Android开发人员必知的开发资源

    developer.android.com 官方开发人员网站推荐资源 在动手编写第一个 Android 应用之前,用心读一读 Android Design 章节.尤其是以下的这些文章: Devices ...

  3. POJ 1724 ROADS(bfs最短路)

    n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路. 直接优先队列bfs暴力搞就行了,100*10000个状态而已.节点扩充的时候,dp[i][j]表示到达第i点花 ...

  4. httpd的简单配置(转)

    一般网站都采用httpd作web服务器提供web页面,本文主要介绍下几个httpd中常用的配置属性和配置方式,当然具体应用更具具体需求来定. 代理模块配置: 由于网页动态化,网页的生成基本代理到后端服 ...

  5. cocos2d-x CCNode类

    文章引用自http://blog.csdn.net/qiurisuixiang/article/details/8763260 1 CCNode是cocos2d-x中一个非常重要的类.CCNode是场 ...

  6. 通用型CRM还是行业型CRM?-定制为王

    大数据时代,怎样利用工具摆脱繁杂的数据管理之苦,洞察有价值的销售信息,是每一个管理者的迫切须要.Zoho  CRM问世10年来,见证了一个个行业客户怎样在CRM帮助下实现了效率和业绩提升.相同,广泛的 ...

  7. poj1679(最小生成树)

    传送门:The Unique MST 题意:判断最小生成树是否唯一. 分析:先求出原图的最小生成树,然后枚举删掉最小生成树的边,重做kruskal,看新的值和原值是否一样,一样的话最小生成树不唯一. ...

  8. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

  9. 在内网架设一个可供外网登录的ftpserver

    ftpserver是使用比較寻常的server,可是IP资源是有限的.那么怎么让内网的server給外网的用户提供服务了? 首先须要找一个FTPserver程序,我在这边使用pure-ftpd-mys ...

  10. Android自己定义控件实战——仿淘宝商品浏览界面

    转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38656929 用手机淘宝浏览商品详情时,商品图片是放在后面的,在第一个Scr ...