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
思路 : 这是一道特别经典的DFS题目 就是先找到一个@然后将与@相连的@全部变成“×” dfs结束,说明没有与@相连的点了 计一此数
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
char arr[][];
int mark[][]={};
int d[][]={{,},{,},{,-},{-,},{,},{,-},{-,},{-,-}};
void dfs(int x,int y){
mark[x][y]=;
for(int i=;i<;i++){
int dx=x+d[i][];
int dy=y+d[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&mark[dx][dy]==&&arr[dx][dy]=='@'){
arr[dx][dy]='*';
dfs(dx,dy);
}
}
}
int main()
{
while(cin>>n>>m&&m){
int ans=;
memset(mark,,sizeof(mark));
for(int i=;i<n;i++)
scanf("%s",&arr[i]);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(arr[i][j]=='@')
{
dfs(i,j);
ans++;
}
}
cout<<ans<<endl;
} return ;
}

A - Oil Deposits DFS的更多相关文章

  1. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  2. Oil Deposits(dfs)

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

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

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

  4. UVa572 Oil Deposits DFS求连通块

      技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...

  5. HDU 1241 Oil Deposits (DFS/BFS)

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

  6. HDU-1241 Oil Deposits (DFS)

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

  7. HDU_1241 Oil Deposits(DFS深搜)

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

  8. UVa 572 Oil Deposits(DFS)

     Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil ...

  9. [POJ] 1562 Oil Deposits (DFS)

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16655   Accepted: 8917 Des ...

  10. Oil Deposits(dfs水)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. linux下vmware could not open /dev/vmmon/no/such/file/or/directory问题

    执行 sudo vmware-modconfig --console --install-all 详解这里  

  2. logstash设置从文件读取的重要参数说明及如何强置重新读取

    问题描述: 如果运行logstash时从文件读取数据时,就会遇到一个问题,如果读取的目标文件未经修改,而仅修改了conf文件,则即使重新运行logstash,或是执行时使用-r时输出也无法更新. 解决 ...

  3. 干货 | Python进阶之学习笔记(一)

    认识Python Python应用场景 Python基础语法 一.认识Python Python 是一种计算机程序设计语言.是一种动态的.面向对象的脚本语言,最初被设计用于编写自动化脚本(shell) ...

  4. List<Object>转List<T>

    今天遇到一个麻烦,公司有个项目用了一个封装dao的模板,他妈的不管是查一条数据的方法,还是查一个集合数据的方法,全都返回Object或List<Object> 由于对象是Object根本不 ...

  5. jmeter响应乱码(十四)

    方法一: jmeter响应乱码解决方法:在jmeter的bin目录下找到jmeter.propertis这个文件,修改里面的#sampleresult.default.encoding=ISO-885 ...

  6. 如何搭建本地web服务

    IIS服务是windows自带的web服务,我们可以用来搭建本地网站,供局域网内的用户之前访问,比如办公室的同事之间,一个教室里的同学们. 先说明这是Windows10 x64位 家庭普通版的系统. ...

  7. turtle实例

    1.彩虹 (1) from turtle import * def HSB2RGB(hues): hues = hues * 3.59 #100转成359范围 rgb=[0.0,0.0,0.0] i ...

  8. C++STL(一)——string类

    STL--string类 初始化 string的赋值 string的连接 string的性质描述 遍历 字符指针和string的转化 查找.替换.交换 字符串的拼接 区间删除. 插入 大小写转换 比较 ...

  9. 编写SpringBoot 中的AOP

    编写SpringBoot 中的AOP 在程序开发的过程中会使用到AOP的思想,面向切面进行开发,比如登录的验证,记录日志等等-频繁需要操作的步骤,在遇到这种情况时就要使用Spring 的AOP了 Sp ...

  10. Nginx知多少系列之(二)安装

    目录 1.前言 2.安装 3.配置文件详解 4.Linux下托管.NET Core项目 5.Linux下.NET Core项目负载均衡 6.Linux下.NET Core项目Nginx+Keepali ...