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. 第9章 CSS3中的变形与动画(下)

    Keyframes介绍 Keyframes被称为关键帧,其类似于Flash中的关键帧.在CSS3中其主要以"@keyframes"开头,后面紧跟着是动画名称加上一对花括号" ...

  2. MobileWeb 适配总结

    开门见山,本篇将总结一下 MobileWeb 的适配方法,即我们常说的H5页面.手机页面.WAP页.webview页面等等. 本篇讨论的页面指专门针对手机设备设计的页面,并非兼容全设备的响应式布局. ...

  3. javastscript获取光标位置

    需求是获取某元素的内容,然后将该内容插入到文本框当前的光标位置 (function($) { $.fn.extend({ insertAtCaret: function(myValue) { var ...

  4. 参数化查询为什么能够防止SQL注入 (转)

    很多人都知道SQL注入,也知道SQL参数化查询可以防止SQL注入,可为什么能防止注入却并不是很多人都知道的. 本文主要讲述的是这个问题,也许你在部分文章中看到过这块内容,当然了看看也无妨. 首先:我们 ...

  5. C++基础--struct的大小

    在修改别人的代码的过程中,发现很多人会把struct和struct的定义混淆,在这里主要是为了提醒自己Struct定义的规范性. #include <stdio.h> struct x{ ...

  6. C++程序员必需的修养

    原文:http://www.cnblogs.com/ctoroad/archive/2006/03/24/357423.html 我总结了在用C/C++语言(主要是C语言)进行程序写作上的三十二个“修 ...

  7. Python爬虫教程-22-lxml-etree和xpath配合使用

    Python爬虫教程-22-lxml-etree和xpath配合使用 lxml:python 的HTML/XML的解析器 官网文档:https://lxml.de/ 使用前,需要安装安 lxml 包 ...

  8. arm汇编学习(五)

    新增个手写GNU语法arm的方法,以后可以狂逆狂写 hello.S文件 .data msg: .ascii "Hello, ARM!\n" len = . - msg .text ...

  9. 20.远程分支&跟踪分支

    远程分支 远程引用是对远程仓库的引用(指针),包括分支.标签等等. 你可以通过 git ls-remote (remote) 来显式地获得远程引用的完整列表,或者通过 git remote show ...

  10. django模板报错:Requested setting TEMPLATE_DEBUG, but settings are not configured. You must either define

    转自:http://blog.csdn.net/xiaowanggedege/article/details/8651236 django模板报错: Requested setting TEMPLAT ...