题目:

 
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. git总结一、工作中常用基础命令

    首先来了解两个概念: 工作区:比如你的项目目录 暂存区:git和其他版本控制系统的不同之处就是有这个暂存区的概念. .git不是工作区,而是git 版本库,在版本库中存放着很多东西,比如暂存区(sta ...

  2. Python基础:编码规范(4)

    1.命名规范 Python中不同代码元素采用不同命名方式: ◊ 包名:全部小写字母,中间可以由点分隔开.作为命名空间,包名需具有唯一性. ◊ 模块名:全部小写字母,如果是多个单词构成,使用下划线分隔. ...

  3. django模板引擎自定义变量

    定义临时变量: {% with i=1 %} {{i}} {% endwith %} 定义对临时变量操作的tag 在templatetags中创建set_val.py 内容是 from django ...

  4. String,StringBuffer与StringBuilder的区别?? 缓存

    转: String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主 ...

  5. Kafka概述(一)

    一.消息队列 客户端A给客户端B发送数据,若是直接发的话,客户端A给客户端B需要同步. 例如, 1)  A在给B发送数据的时候,B挂掉了,此时的A是没有办法给B发送数据的: 2)  A发送10M/s, ...

  6. layui——Cannot create property 'LAY_TABLE_INDEX' on number '1'

    前言 用layui写的后台,PHP处理好数据传输过来的时候报错了,最后发现是数据处理成layui格式的时候出错了,直接传了json对象过来,layui需要的是一个json的数. 解决 外面套成数组即可 ...

  7. <Android基础> (六) 数据存储 Part 3 SQLite数据库存储

    6.4 SQLite数据库存储 SQLite是一种轻量级的关系型数据库,运算速度快,占用资源少. 6.4.1 创建数据库 Android为了管理数据库,专门提供了SQLiteOpenHelper帮助类 ...

  8. MySQL物理备份 lvm-snapshot

    MySQL备份之 lvm-snapshot lvm-snapshot(工具备份) 优点: 几乎是热备(穿件快照前把表上锁,创建完成后立即释放) 支持所有引擎 备份速度快 无需使用昂贵的商业软件(它是操 ...

  9. Flask框架(1)--基础

    Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后 ...

  10. mkdir(): Permission denied

    记录下,凡是遇到此类问题都是无权限导致. 根据不同场景,需要在不同的文件夹设置不同的权限. 例如,图片相关, 在php中,首先看下 配置php.ini的文件上传是否开启(file_uploads = ...