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. 曾经的UCOSii

    首先记住一句话:实时操作系统,并非真的实时.操作系统必须有延时,一个系统执行好几个任务,实质是任务之间不停的切换,有延时才有切换任务的余地,如果没有.....应该都见识过卡机,任务切换不过来了 如果你 ...

  2. POJ 1860&&3259&&1062&&2253&&1125&&2240

    六道烦人的最短路(然而都是水题) 我也不准备翻译题目了(笑) 一些是最短路的变形(比如最长路,最短路中的最长边,判环),还有一些就是裸题了. 1860:找环,只需要把SPFA的松弛条件改一下即可,这里 ...

  3. VS新建一个模板工程

    新建一个模板工程的好处:    1.就不用每次都走一边新建向导了,新建工程一步到位. 2.可以往项目中每次都的输入的代码,如一些声明注释-- 效果图: 具体步骤: 1.自己先新建一个属于自己的工程. ...

  4. cocos2d-x学习记录3——CCTouch触摸响应

    游戏不同于影音,强交互性是其一大特色,在游戏中主要体现为接受用户的输入并响应.智能手机触摸是其重要的输入方式. 在cocos2d-x中,触摸分为单点触摸和多点触摸. 单点触摸:主要继承CCTarget ...

  5. Centos7下python3安装ipython

    一.通过压缩包安装ipython 1.下载ipython安装包 [root@localhost ~]# wget https://pypi.python.org/packages/79/63/b671 ...

  6. [CF1083D]The Fair Nut’s getting crazy[单调栈+线段树]

    题意 给定一个长度为 \(n\) 的序列 \(\{a_i\}\).你需要从该序列中选出两个非空的子段,这两个子段满足 两个子段非包含关系. 两个子段存在交. 位于两个子段交中的元素在每个子段中只能出现 ...

  7. 手撸orm

    ORM简介 ORM即Object Relational Mapping,全称对象关系映射.当我们需要对数据库进行操作时,势必需要通过连接数据.调用sql语句.执行sql语句等操作,ORM将数据库中的表 ...

  8. memcached 和redis比较

    同属于NOSQL存储,网上流传很多memcached能做的是redis都可以做,为什么基本现在两种都火,原因他们有各自擅长的地方. memcahed内部采用多核模式,单列运行很快.memcached采 ...

  9. Win7远程桌面的多用户连接破解

    系统是 64位WIN7 旗舰版 每当我用其它机器连WIN7的3389远程桌面时,WIN7那台机子就会退出到注销用户后的状态了,后来我新建了个用户,用不同用户登陆还是退出,也就是说不能同时2个人操作电脑 ...

  10. Hadoop 部署文档

    Hadoop 部署文档 1 先决条件 2 下载二进制文件 3 修改配置文件 3.1 core-site.xml 3.2 hdfs-site.xml 3.3 mapred-site.xml 3.4 ya ...