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
 
Source
 #include <bits/stdc++.h>
using namespace std;
char a[][];
int n,m,res;
int dx[]={,-,,};
int dy[]={,,,-};
void dfs(int x,int y)
{
a[x][y]='*';
for(int i=-;i<=;i++){
for(int j=-;j<=;j++){
int nx=x+i,ny=y+j;
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='@'){
dfs(nx,ny);
}
}
}
return ;
}
void solve()
{
res=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='@'){
dfs(i,j);
res++;
}
}
}
}
int main() {
while(cin>>n>>m&&(n&&m)){
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
res=;
solve();
cout<<res<<endl;
}
return ;
}

Hdu1241 Oil Deposits (DFS)的更多相关文章

  1. HDU-1241 Oil Deposits (DFS)

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

  2. HDU1241 Oil Deposits —— DFS求连通块

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

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

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

  4. Oil Deposits(dfs)

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

  5. hdu1241 Oil Deposits

    Oil Deposits                                                 Time Limit: 2000/1000 MS (Java/Others)  ...

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

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

  7. UVa572 Oil Deposits DFS求连通块

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

  8. HDU 1241 Oil Deposits (DFS/BFS)

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

  9. HDU_1241 Oil Deposits(DFS深搜)

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

随机推荐

  1. linux 安装svn客户端

    安装命令:yum install -y subversion 客户端使用命令: svn help  帮助命令 svn checkout --help  子帮助命令

  2. 基于 SOA 概念 RPC 框架 的 消息中心 云部署 设计 漫谈

    一.背景 假设有一个系统的最大并发量有2000TPS左右.同时该系统有闲时和忙时,希望可以随时进行拓展和削减服务能力,以节省服务器费用开销. 该系统能提供站内消息.短信.app消息.邮箱的一个消息系统 ...

  3. python 检测字符串编码类型是什么

    for index,value in enumerate(listvalue): adchar=chardet.detect(value) print adchar if adchar['encodi ...

  4. Ai challenger 2017 image caption小结

    参加了今年的ai challenger 的image caption比赛,最终很幸运的获得了第二名.这里小结一下. Pytorch 越来越火了.. 前五名有三个pytorch, 两个tensorflo ...

  5. 字符串匹配的 KMP算法

    一般字符串匹配过程 KMP算法是字符串匹配算法的一种改进版,一般的字符串匹配算法是:从主串(目标字符串)和模式串(待匹配字符串)的第一个字符开始比较,如果相等则继续匹配下一个字符, 如果不相等则从主串 ...

  6. CF 1131C Birthday

    C. Birthday time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  7. javascript面试题(2)

    可以参考一  https://www.cnblogs.com/DCL1314/p/7903114.html 1.什么是JavaScript?(这是基本题,对很多程序员来说也是送分题!) JavaScr ...

  8. namecheap 添加二级域名

    namecheap Docs Nginx 添加一个server server { listen 80; server_name video.ajanuw.fun; location / { root ...

  9. 有关自动化构建gulp的搭建

    1,cnpm instal bower -g 2,  cnpm install bower 3,  bower init 生成bower.json文件 4, type null >.bowerr ...

  10. IniHelper

    /// <summary> /// ini文件操作类 /// </summary> public class IniHelper { #region 动态链接库调用 /// & ...