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. Java 基础(4)——常量 & 注释

    hello 呀,今天的内容超简单( ̄︶ ̄)↗并且,还有暗藏福利哟~~ 常量 常量 就是常常不变的量,第一次定义之后,就不会发生改变了.可能这就是 “常量” 的来源吧哈哈哈(玩笑). 一般来说,常量的定 ...

  2. 由ArrayList来深入理解Java中的fail-fast机制

    1. fail-fast简介“快速失败”也就是fail-fast,它是Java集合的一种错误检测机制.某个线程在对collection进行迭代时,不允许其他线程对该collection进行结构上的修改 ...

  3. 2、springboot返回json

    新建maven项目 添加依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId ...

  4. 四 Scatter/Gather

    scatter/gather用于描述从Channel中读取或者写入到Channel的操作. 分散(scatter):从Channel中读取在读操作中将读取的数据写入多个Buffer中.因此,Chann ...

  5. thinkphp下判断状态值语法

    在thinkphp框架下我们经常会用到状态值的判断:但是这样写会引起语法错误. <div> <if condition="{$res.status} eq '0'" ...

  6. java idea+ssm框架遇到的问题

    0.学习教程 http://www.cnblogs.com/jiekzou/p/9205117.html https://github.com/crossoverJie/SSM 1.gradle没刷新 ...

  7. [转]优化IIS7.5支持10万个同时请求的配置方法

    通过对IIS7的配置进行优化,调整IIS7应用池的队列长度,请求数限制,TCPIP连接数等方面,从而使WEB服务器的性能得以提升,保证WEB访问的访问流畅 通过对IIS7的配置进行优化,调整IIS7应 ...

  8. Spring-aop实现切面的四种方式 (2)

    AOP实现方式一 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  9. yii2.0中url重写实现方法

    在yii框架里有前台和后台页面,举例前台url重写. 控制器与路由 控制器以Controller作为后缀,继承自yii\web\Controller; 动作以action作为前缀,public访问修饰 ...

  10. sparsity and density

    转:http://searchdatamanagement.techtarget.com/definition/sparsity-and-density 查了一下定义,以免会写错 Sparsity a ...