Oil Deposits


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 1 <= m <= 100 and
1 <= n <= 100. 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


Source: Mid-Central USA 1997

分析:

dfs水题,问你@连通块的个数

@可以向8个方向扩展

和油田问题属于同一类问题

传送门(点我)

code:

#include<bits/stdc++.h>
using namespace std;
#define max_v 105
int n,m;
char a[max_v][max_v];
int used[max_v][max_v];
int dir[][]={{,},{,-},{,},{-,},{,},{,-},{-,},{-,-}};
void dfs(int x,int y,int z)
{
if(x<||x>=n||y<||y>=m)
return ;
if(used[x][y]>||a[x][y]!='@')
return ;
used[x][y]=z;
for(int i=;i<;i++)
dfs(x+dir[i][],y+dir[i][],z);
}
int main()
{
while(cin>>n>>m)
{
if(m==&&n==)
break;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>a[i][j];
}
}
memset(used,,sizeof(used));
int c=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(used[i][j]==&&a[i][j]=='@')
dfs(i,j,++c);
}
}
printf("%d\n",c);
}
}

ZOJ 1709 Oil Deposits(dfs,连通块个数)的更多相关文章

  1. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

  2. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

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

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

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

  4. HDU 1241 Oil Deposits (DFS or BFS)

    链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...

  5. 【BZOJ 1098】办公楼(补图连通块个数,Bfs)

    补图连通块个数这大概是一个套路吧,我之前没有见到过,想了好久都没有想出来QaQ 事实上这个做法本身就是一个朴素算法,但进行巧妙的实现,就可以分析出它的上界不会超过 $O(n + m)$. 接下来介绍一 ...

  6. P1197 [JSOI2008]星球大战 [删边求连通块个数]

    展开 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的 ...

  7. UVa572 Oil Deposits DFS求连通块

      技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...

  8. HDU1241 Oil Deposits —— DFS求连通块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

  9. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

随机推荐

  1. Browser对象之Window对象

    对象属性 对象方法 setInterval() 按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInte ...

  2. Gradle sync failed: Cannot set the value of read-only property 'outputFile'

    错误 Gradle sync failed: Cannot set the value of read-only property 'outputFile' 原因 gradle打包,自定义apk名称代 ...

  3. Win7下硬盘安装Ubuntu双系统

    一. 准备工作 1. 下载ubuntu镜像文件:Ubuntu-14.04.5-desktop-amd64.iso(4G及以上内存建议64位) 注意这个amd并不是指amd芯片. 2. 下载硬盘分区工具 ...

  4. c# 获取文件夹大小

    private long GetDirectorySizeMethod1(string directory) { long directorySize = 0; DirectoryInfo di = ...

  5. 第2章、数据与简单计算程序(c语言入门)

    一.写程序描述数据的处理 ①描述数据 ②描述过程 ps:和做鸡蛋饼差不多:数据是材料,作法是过程. 空白符的作用是使用程序更容易了解. 二.标识符 1.基本要求: ①由字母,数字,下划线组成. ②数字 ...

  6. 用css3+js写了一个钟表

    有一天看到css3旋转这个属性,突发奇想的写了一个钟表(没做浏览器兼容),来一起看看是怎么写的吧! 先给个成品图,最终结果是个样子的(动态的). 首先,思考了一下页面的布局,大致需要4层div,最底层 ...

  7. react+javascript前端进阶

    组合1: react技术栈(react(阮一峰react入门,官网教程).redux(阮一峰redux入门,官网教程).saga)+JS(ES6)+antd+you don`t know JS(上中下 ...

  8. iview中事件获取自定义参数

    前提:页面渲染多个cascaer组件,根据不同cascader组件选中的值,加载不同内容 解决:此时需要解决的问题是,在on-change事件中返回index索引使用如下格式: <Cascade ...

  9. Bootstrap + AngularJS+ Ashx + SQL Server/MySQL

    去年年底12月,为适应移动端浏览需求,花了1个月时间学习Bootstrap,并将公司ASP网站重构成ASP.NET. 当时采取的网站架构: Bootstrap + jQuery + Ashx + SQ ...

  10. asp.net怎么让某一页的 requestEncoding设置成utf-8

    web.config里是这样的 <globalization requestEncoding="gb2312" responseEncoding="gb2312&q ...