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
 
 
套模板,上代码:
 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
char map[][];
int vis[][];///标记数组
int dir[][]= {{,},{-,},{,},{,-},{,},{-,-},{,-},{-,}};///八面搜素
int n,m;
void DFS(int x,int y)
{
int a,b,i;
vis[x][y]=;
for(i=; i<; i++)
{
a=x+dir[i][];
b=y+dir[i][];
if(a>=&&a<n&&b>=&&b<m&&vis[a][b]==&&map[a][b]=='@')
{
DFS(a,b);
}
}
return ;
}
int main()
{
int count,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
if(n==&&n==)
break;
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
count=;
for(i=; i<n; i++)
{
scanf("%s",map[i]);
}
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
if(vis[i][j]==&&map[i][j]=='@')
{
count++;
DFS(i,j);
}
}
}
printf("%d\n",count);
}
return ;
}

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. MySQL常用参数说明(持续更新)

      ##innodb correlate   innodb_flush_log_at_trx_commit value: 0,[1],2 effect: control the flush opera ...

  2. 搭建Apache服务器并使用自签证书实现https访问

    实验环境:两台Centos7.2的虚拟机,一台作CA服务器,一台作Apache服务器,此处安装httpd-2.4.6的版本. 1)CA服务器 # 私钥一般存放位置:/etc/pki/CA/privat ...

  3. 解决jquey中当事件嵌套时,内层事件会执行多次的问题

    出现情景:当内层事件需要外层事件触发后产生的一些值得时候 情景复现: <!DOCTYPE html> <html lang="en"> <head&g ...

  4. 关于php中数字0与其他变量的相等判断

    在实践过程中,经常需要做`==`判断,有时候会把0当做false一样用,但是0和false在用来做比较的时候还是不一样的, false false==0 等于true false=='0' 等于tru ...

  5. laravel4.2 Redis 使用

    laravel4.2 Redis 使用 配置文件,app/config/database.php 'redis' => array( 'cluster' => false, 'defaul ...

  6. Hive操作之向分区表中导入数据的语义错误

    1.建完分区表之后,向表中导入数据 命令为: load data local inpath '/home/admin/Desktop/2015082818' into table db_web_dat ...

  7. 网站用户行为分析——HBase的安装与配置

    Hbase介绍 HBase是一个分布式的.面向列的开源数据库,源于Google的一篇论文<BigTable:一个结构化数据的分布式存储系统>.HBase以表的形式存储数据,表有行和列组成, ...

  8. Redis在Linux中的运用

    Redis在Linux中的运用 一.Redis安装部署 下载: wget http://download.redis.io/releases/redis-3.2.12.tar.gz 解压: 上传至 / ...

  9. epoll 服务端 ET模式

    windows下IOCP, linux下 epoll. epoll模型其实也是一个同步模型,ET是epoll里面的一种模式,叫 边缘触发. 个人理解,类似于 windows下的事件选择模型.代码如下: ...

  10. 北京Uber优步司机奖励政策(1月26日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...