Problem Description

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 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.

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

题意:

*代表荒地,@代表油田。@的上下左右,还有对角的4个如果还有@,就表示它们是一个油田,问-给出的图中,一共有多少油田。

分析:

本题用DFS(深搜)可以很好的解决问题!

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char map[210][210];
int df[210][210];
int con,n,m;
void dfs(int x,int y,int t){ if(df[x][y]==1||x<0||y<0||x>=n||y>=m){
return;
}
if(map[x][y]=='*'){
return;
}
if(t==1&&map[x][y]=='@'){
con++;
//printf("con=%d\n",con);
} //printf("%d %d\n",x,y);
if(map[x][y]=='@'){
map[x][y]='*';
df[x][y]=1;
dfs(x+1,y+1,0);
dfs(x-1,y-1,0);
dfs(x+1,y,0);
dfs(x,y+1,0);
dfs(x-1,y,0);
dfs(x,y-1,0);
dfs(x+1,y-1,0);
dfs(x-1,y+1,0);
df[x][y]=0;
} } int main()
{
while(~scanf("%d%d",&n,&m),(n||m)){
memset(df,0,sizeof(df));
con=0;
getchar();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%c",&map[i][j]);
}
getchar();
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
dfs(i,j,1);
}
}
printf("%d\n",con);
}
return 0;
}

HDOJ/HDU 1241 Oil Deposits(经典DFS)的更多相关文章

  1. HDU - 1241 Oil Deposits 经典dfs 格子

    最水的一道石油竟然改了一个小时,好菜好菜. x<=r  y<=c  x<=r  y<=c  x<=r  y<=c  x<=r y<=c #include ...

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

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

  3. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  4. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  5. hdu 1241:Oil Deposits(DFS)

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

  6. HDU 1241 Oil Deposits (DFS)

    题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...

  7. HDU 1241 Oil Deposits【DFS】

    解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...

  8. HDU - 1241 Oil Deposits 【DFS】

    题目链接 https://cn.vjudge.net/contest/65959#problem/L 题意 @表示油田 如果 @@是连在一起的 可以八个方向相连 那么它们就是 一块油田 要找出 一共有 ...

  9. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

随机推荐

  1. EIGamal密码体制

    EIGamal密码体制:由EIGamal提出,是一种基于离散对数问题的双钥密码体制,既可用于加密,又可以用于签名. 密钥对生成步骤: 1.取大素数p和g<p(g最好是p的素根) 2.选一整数x& ...

  2. [翻译]ASP.NET Web API的路由

    原文:Routing in ASP.NET Web API 在我们新建一个Web API项目时,会在App_Start文件夹下的WebApiConfig.cs中定义一个默认路由: config.Rou ...

  3. MVC的发展

    ASP.NET下的MVC从原始的1.0走到2.0,再到3.0,现在走到4.0,也许明年5.0就问世了,先不管那些,那说说这些MVC在ASP.NET是如何变化发展的.对于.net编程人员来说可能会很熟悉 ...

  4. DIV+CSS 网页布局之:两列布局

    1.宽度自适应两列布局 两列布局可以使用浮动来完成,左列设置左浮动,右列设置右浮动,这样就省的再设置外边距了. 当元素使用了浮动之后,会对周围的元素造成影响,那么就需要清除浮动,通常使用两种方法.可以 ...

  5. linux curl命令验证服务器断点续传支持

    有个同事说,发现现在对外下载安装包的服务器不支持断点续传,我听了一阵纳闷,lighttpd server对于静态文件应该默认支持断点续传的,登机器查看lighttpd配置文件发现 对断点续传的支持被禁 ...

  6. delphi发送html带附件邮件

    var html: TIdText; begin if not SMTP.Connected then SMTP.Disconnect(); try SMTP.Connect; try if SMTP ...

  7. hadoop1——map到reduce中间的shuffle过程

    ---恢复内容开始--- shuffle和排序 过程图如下: MapReduce确保每个reduce的输入都按键排序,系统执行排序的过程——将map输出作为输入传给reduce——成为shuffle, ...

  8. html5音频和视频相关属性和方法

    方法 方法 描述 addTextTrack() 为音视频加入一个新的文本轨迹 canPlayType() 检查指定的音视频格式是否得到支持 load() 重新加载音视频标签 play() 播放音视频 ...

  9. C#——System.Diagnostics.Process.Start的妙用

    我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 那么如何做呢? 答案 ...

  10. Prism vs MvvmCross

    Prism vs MvvmCross 在上一篇Xamarin开发环境及开发框架初探中,曾简单提到MvvmCross这个Xamarin下的开发框架.最近又评估了一些别的,发现老牌Mvvm框架Prism现 ...