题目:

 
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. 想玩 Android 开发板?这些常用命令你不知不行!

    2019-04-19 关键字:Android机顶盒常用命令.Linux命令 笔者早年间从事 Android 机顶盒开发工作,那会刚毕业,技术也比较菜,工作过程中遇到过不少困难,不过所幸当时就有做笔记的 ...

  2. elasticsearch-head的安装

    elasticsearch-head是es的一个可视化的客户端插件,可以直接对ES进行增删改查操作,安装前需要先安装NODEJS: 安装: 1.到git上下载源代码: # git clone git: ...

  3. PLSQL Developer中文乱码问题

    前言 使用PLSQL工具进行连接远程oracle时,中文乱码 解决过程 1 查看服务器端编码 select userenv('language') from dual; 2 查看客户端编码 执行语句 ...

  4. Java IO系列之四:NIO通信模型

    分布式rpc框架有很多,比如dubbo,netty,还有很多其他的产品.但他们大部分都是基于nio的, nio是非阻塞的io,那么它的内部机制是怎么实现的呢. 1.由一个专门的线程处理所有IO事件,并 ...

  5. hive字段名、注释中文显示问号

    问题如下图: 解决方法: header1的/etc/my.conf文件,在[mysqld]分组下面添加配置:character-set-server=utf8init_connect='SET NAM ...

  6. zipline-- 开发指南

    Development Guidelines开发指南This page is intended for developers of Zipline, people who want to contri ...

  7. DUMP 3.8 企业级电商项目 支付宝之类

    ① 沙箱登录:https://openhome.alipay.com/platform/appDaily.htm 获得一个 使用环境描述 APPID.授权回调地址.沙箱钱包哪里下载之类的 ② 沙箱环境 ...

  8. Python核心编程笔记 第三章

    3.1     语句和语法    3.1.1   注释( # )   3.1.2   继续( \ )         一般使用换行分隔,也就是说一行一个语句.一行过长的语句可以使用反斜杠( \ ) 分 ...

  9. Ubuntu18.04环境下melodic安装gmapping

    Ubuntu18.04 环境下melodic中很多包没有提供sudo apt install的安装方式,需要通过源代码安装,安装方法如下: 1.先安装依赖库: sudo apt--dev sudo a ...

  10. 从XML文件和properties文件提取数据

    XML文档格式内容如下 <?xml version="1.0" encoding="UTF-8"?> <root>     <fi ...