题目:

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. python爬虫王者荣耀高清皮肤大图背景故事通用爬虫

    wzry-spider python通用爬虫-通用爬虫爬取静态网页,面向小白 基本上纯python语法切片索引,少用到第三方爬虫网络库 这是一只小巧方便,强大的爬虫,由python编写 主要实现了: ...

  2. Java连载72-String类详解、多个构造方法

    一.String类 1.String类是不可以变类,也就是说String对象声明后 2.java.lang.String:是字符串类型 (1)字符串一旦创建不可再改变,“abc”字符串对象一旦创建,不 ...

  3. windows Apache 配置支持HTTPS的SSL证书

    在设置Apache + SSL之前, 需要做: 安装Apache, 下载安装Apache时请下载带有ssl版本的Apache安装程序. 并且ssl需要的文件在如下的位置: [Apache安装目录]/m ...

  4. 静态、动态cell区别

    静态cell:cell数目固定不变,图片/文字固定不变(如qq设置列表可使用静态cell加载) 动态cell:cell数目较多,且图片/文字可能会发生变化(如应网络请求,淘宝列表中某个物品名称或者图片 ...

  5. linux epoll ET边沿触发

    /***EPOLL ET 触发必须使用非阻塞,LT触发可以阻塞/非阻塞.*read 函数 非阻塞读需 忙轮寻 soket关闭返回0,循环读完数据*如果已经读完再读read返回 -1,errno=11( ...

  6. TPO1-3Timberline Vegetabtion on Mountain|have the advantage over

    The upper timberline, like the snow line, is highest in the tropics and lowest in the Polar Regions. ...

  7. Web 自动化

    自动化:由机器设备代替人为自动完成指定目标的过程 自动化测试:由程序代替人为去验证程序功能的过程 为什么要进行自动化测试? 解决-回归测试 压力测试 兼容性测试 提高测试效率,保证产品质量 什么阶段开 ...

  8. 编译原理_P1002

    . 词法分析 1.1 词法记号及属性 词法记号.模式.词法单元 记号名 词法单元列举    模式的非形式描述 if if 字符i,f for for     字符f,o,r relation < ...

  9. redis day02 下

    位图:是二进制数据(0101101010)2^32 强势点: 01_login :101110(比如:第一天登录,二天没登录) 传统的字符串解决方案中 记录用户登录日期  统计堪忧 01_login_ ...

  10. B-Tree(B树)原理及C++代码实现

    B树是一种平衡搜索树,它可以看做是2-3Tree和2-3-4Tree的一种推广.CLRS上介绍了B树目前主要针对磁盘等直接存取的辅存设备,许多数据库系统也利用B树或B树的变种来存储信息. 本文主要针对 ...