题目:

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdivides 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 containsone 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

题意:

和迷宫的输入差不多,‘*’是墙,‘@’是油田,以一个油田为中心,如果它的东南西北一个各个角落,一共八个方向也有油田的话,这些油田就算作一个油田。

分析:

查找到‘@’的位置,找到一个,油田加1,对找到的‘@’的四周进行DFS查找,刚开始我以为和迷宫的做法差不多,我把查找过的地方用一个数组标记一下,后来测试结果不对,

才发现在某一次存在题意中的相邻油田时,在主函数中会加1,而实际上这块油田我已经计算进去了,这样就重复了。正确的做法应该是把计算进去的那块油田变为‘*’,这样

就不存在重复计算了!!!

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
char a[][];
int row,col;
int dir[][]=
{
{,},
{,},
{,-},
{,-},
{,},
{-,},
{-,},
{-,-}
};
using namespace std;
void dfs(int i,int j)
{
a[i][j]='*';
for (int k=;k<;k++)
{
int x=i+dir[k][];
int y=j+dir[k][];
if (x>=&&x<=row&&y>=&&y<=col&&a[x][y]=='@')
dfs(x,y);
}
return ;
}
int main()
{
while ((cin>>row>>col)&&(row!=||col!=))
{
int c=;
getchar();
for (int i=;i<=row;i++)
for (int j=;j<=col;j++)
cin>>a[i][j];
for (int i=;i<=row;i++)
for (int j=;j<=col;j++)
if (a[i][j]=='@')
{
dfs(i,j);
c++;
}
cout << c << endl;
}
return ;
}

Oil Deposits(油田)(DFS)的更多相关文章

  1. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  2. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

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

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

  4. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

  5. HDOJ/HDU 1241 Oil Deposits(经典DFS)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

  6. ZOJ 1709 Oil Deposits(dfs,连通块个数)

    Oil Deposits Time Limit: 2 Seconds      Memory Limit: 65536 KB The GeoSurvComp geologic survey compa ...

  7. 【HDU - 1241】Oil Deposits(dfs+染色)

    Oil Deposits Descriptions: The GeoSurvComp geologic survey company is responsible for detecting unde ...

  8. HDU 1241 Oil Deposits【DFS】

    解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...

  9. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  10. Oil Deposits(DFS连通图)

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

随机推荐

  1. macbook 安装laravel5.4

    1.安装composer php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');&q ...

  2. C语言笔记 16_标准库&stdio&stdlib&string&time

    <stdio.h> 简介 stdio .h 头文件定义了三个变量类型.一些宏和各种函数来执行输入和输出. 库变量 下面是头文件 stdio.h 中定义的变量类型: 序号 变量 & ...

  3. IMX6开发板qt creator直接编译ARM架构程序

    除了通过 11.2.2 小节通过命令行的操作来编译在 iTOP-imx6 开发板上运行的程序,还可以直接在 qtcreator 上设置,然后每次编译的程序都可以在开发板上运行.如下图所示,打开 qtc ...

  4. 九、Shell脚本高级编程实战第九部

    一.监控mysql主从同步是否异常,如果异常,发送短信给管理员 1)开发一个守护进程脚本每30秒实现检测一次. 2)如果错误号是:1158.1159.1008.1007.1062,请跳过 3)请使用数 ...

  5. 吴裕雄--天生自然python Google深度学习框架:图像识别与卷积神经网络

  6. Qt OpenCV 在界面显示图片 通过Lable方式 和GraphicsView 方式

    1. 通过lable方式打开图片. 代码如下: void MainWindow::on_pushButton_clicked() { Mat srcImage,gray_image,srcImage1 ...

  7. 学习ECC及Openssl下ECC生成密钥的部分源代码心得

    一.ECC的简介 椭圆曲线算法可以看作是定义在特殊集合下数的运算,满足一定的规则.椭圆曲线在如下两个域中定义:Fp域和F2m域. Fp域,素数域,p为素数: F2m域:特征为2的有限域,称之为二元域或 ...

  8. idea maven Running C:\Users\Administrator\AppData\Local\Temp\archetype1tmp

    Running C:\Users\Administrator\AppData\Local\Temp\archetype1tmp 在IDEA中通过maven项目管理工具创建javaweb项目的时候一直卡 ...

  9. 图像的手绘效果(Python)

    PIL库,Python Image Library PIL库是一个具有强大图像处理能力的第三方库 在命令行下的安装方法:pip install pillow from PIL import Image ...

  10. Java--类以及对象

    什么是类 就是将一类事物的相同的本质特性抽象出来,类具有属性和方法,属性就是特征(具有什么),方法就是行为(能做什么). 类是一种引用的数据类型,类创建的对象的过程叫做实例化 什么是对象 对象就是类中 ...