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.

Input

The input file contains one or more grids. Each grid begins with a line containing mand 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  and . 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 思路:
直接用dfs爆搜
实现代码:
#include<iostream>
#include<cstring>
using namespace std;
const int M = 1e2+;
int vis[M][M];
char mp[M][M];
int n,m;
void dfs(int u,int v,int d){
if(u < ||u >= m||v < ||v >= n) return;
if(vis[u][v]||mp[u][v]!='@') return;
vis[u][v] = ;
for(int i = -;i <= ;i ++){
for(int j = -;j <= ;j ++){
if(i==&&j==) continue;
dfs(u+i,v+j,d);
}
}
return;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
while(cin>>m>>n){
if(n==||m==) break;
for(int i = ;i < m;i ++)
for(int j = ;j < n;j ++)
cin>>mp[i][j];
memset(vis,,sizeof(vis));
int ans = ;
for(int i = ;i < m;i ++){
for(int j = ;j < n;j ++){
if(!vis[i][j]&&mp[i][j]=='@'){
dfs(i,j,++ans);
}
}
}
cout<<ans<<endl;
}
return ;
}

UVa 572 油田 (dfs)的更多相关文章

  1. UVa 572 油田(DFS求连通块)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  3. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  4. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  5. 2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

    这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小 ...

  6. UVA 572 (dfs)

    题意:找出一块地有多少油田.'@'表示油田.找到一块就全部标记. #include<cstdio> #define maxn 110 char s[maxn][maxn]; int n,m ...

  7. UVA 572 dfs求连通块

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  8. UVA - 572 Oil Deposits(dfs)

    题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...

  9. 暴力求解——UVA 572(简单的dfs)

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

随机推荐

  1. 50Hz工频干扰消除

    50Hz工频干扰消除 今天整理工频干扰消除算法. 我们知道,设计数字滤波器,和模拟滤波器的实质,其实就是求一组系数,逼近要求的频率响应. 模拟滤波器已经很成熟,因此,数字滤波器的设计,将S平面映射到Z ...

  2. 第39章 ETH—Lwip以太网通信

    第39章     ETH—Lwip以太网通信 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/ ...

  3. Junit测试中找不到junit.framework.testcase

    在使用Junit进行测试时,出现如下问题: 找不到junit.framework.testcase 解决方法: 选中项目->属性->Java构建路径->库->添加外部jar 在 ...

  4. 基于TLS证书手动部署kubernetes集群(下)

    一.master节点组件部署 承接上篇文章--基于TLS证书手动部署kubernetes集群(上),我们已经部署好了etcd集群.flannel网络以及每个节点的docker,接下来部署master节 ...

  5. 2017-2018 Exp6 信息搜集与漏洞扫描 20155214

    目录 Exp6 信息搜集与漏洞扫描 实验内容 信息收集 漏洞扫描 知识点 Exp6 信息搜集与漏洞扫描 收集渗透目标的情报是最重要的阶段.如果收集到有用的情报资料的话,可以大大提高对渗透测试的成功性. ...

  6. [Oracle]坏块处理:确认坏块的对象

    如果已经知道 FILE#,BLOCK#,则 可以通过如下查询来看: SQL> SELECT SEGMENT_TYPE,OWNER||'.'||SEGMENT_NAME FROM DBA_EXTE ...

  7. 探索sklearn | K均值聚类

    1 K均值聚类 K均值聚类是一种非监督机器学习算法,只需要输入样本的特征 ,而无需标记. K均值聚类首先需要随机初始化K个聚类中心,然后遍历每一个样本,将样本归类到最近的一个聚类中,一个聚类中样本特征 ...

  8. 汇编 fsub ,fmul,fdiv,fild,CVTTPS2PI 指令

    知识点:  浮点指令 fsub 一.浮点指令fsub 格式 fsub memvar // st0=st0-memvar 知识点:  浮点指令 fmul 一.浮点指令fmul 格式 fmul mem ...

  9. dxp altium pcb里面如果想让重叠的两个元件不报错怎么设置?

    dxp的设置是Design Rules里面有个Placement选项,把第一个的钩去掉即可.

  10. bootstrap-validator基本使用(自定义验证身份证号和手机号)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...