题目:

 
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.

InputThe 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.
OutputFor 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

题解:

  逐个遍历,遇到‘@’就深搜它的周围,逐层深搜,直到把相连的所有@都变成*。

代码:

#include <iostream>

using namespace std;
char a[][];
int n,m,sum=;
void dfs(int i,int j){
if(a[i][j]!='@'||i<||j<||i>=m||j>=n) return;
else{
a[i][j]='*';
dfs(i-, j-);
dfs(i-, j);
dfs(i-, j+);
dfs(i, j-);
dfs(i, j+);
dfs(i+, j-);
dfs(i+, j);
dfs(i+, j+);
}
} int main()
{
int i,j;
while(cin>>m>>n){
sum=;
if(n==||m==) break;
for(i=;i<m;i++)
for(j=;j<n;j++)
cin>>a[i][j];
for(i=;i<m;i++)
for(j=;j<n;j++){
if(a[i][j]=='@'){
dfs(i,j);
sum++;
}
}
cout<<sum<<endl;
}
return ;
}

hdu 1241 Oil Deposits (简单搜索)的更多相关文章

  1. HDU 1241 Oil Deposits DFS搜索题

    题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...

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

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

  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

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

  5. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

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

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

  7. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

  8. hdu 1241:Oil Deposits(DFS)

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

  9. HDU 1241 Oil Deposits (DFS)

    题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...

随机推荐

  1. 财务CLOUD成本核算

    1.关账  仓库账关账 2.应收应付是否已审核 生成财务应收应付 3.存货账关账 4.1采购存货核算 4.2零成本维护 4.3成本中心设置 4.4成本项目设置 4.5费用项目设置 4.6成本项目匹配方 ...

  2. novaclient的api调用流程与开发

    novaclient的api调用流程与开发 2015年07月05日 19:27:17 qiushanjushi 阅读数:3915   http://blog.csdn.net/tpiperatgod/ ...

  3. cmd 常用命令

    注:绿色的为比较常用的命令 命令 名称 ASSOC  显示或修改文件扩展名关联. ATTRIB 显示或更改文件属性. BREAK  设置或清除扩展式 CTRL+C 检查. CACLS 显示或修改文件的 ...

  4. codeforces960G. Bandit Blues

    题目链接:codeforces960G 来看看三倍经验:hdu4372 luogu4609 某蒟蒻的关于第一类斯特林数的一点理解QAQ:https://www.cnblogs.com/zhou2003 ...

  5. TODO springboot学习笔记

    学习中,是在是搞不懂是什么狗屎....

  6. 公共DNS推荐及dns测速

    DNS在平时上网中扮演重要角色,如果不注意DNS的话,可能会导致网速慢.弹窗广告.网址打不开.打开不是自己想要的网站.劫持等一系列问题.针对DNS的问题,今天我们就来总结一下,看看哪个DNS服务器最好 ...

  7. jquery 中 this 的范围

    jquery 中的 this button 通常会存在  onclick;// input[type='file'] 会存在 onchange 事件 eg:  onclick = " del ...

  8. Koa与Node.js开发实战(3)——Nunjucks模板在Koa中的应用(视频演示)

    技术架构: ​ 在Koa中应用Nunjucks,需要先把Nunjucks集成为符合Koa规格的中间件(Middleware),从本质上来讲,集成后的中间件的作用是给上下文对象绑定一个render(vi ...

  9. Arduino-函数库和程序架构介绍

    (1)声明变量及接口的名称 (2)setup().在程序运行时首先要调用setup()函数[初始化函数],用于初始化变量.设置针脚的输出/输入类型.配置串口.引入类库文件等等.每次Arduino上电或 ...

  10. 毕业设计——Django邮件发送功能实现及问题记录

    django发送邮件:send_mail()&send_mass_mail() 自强学堂 刘江的博客 HTTPS,TLS和SSL django发送邮件及其相关问题 步骤 : 0. 登录作为发送 ...