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

Mid-Central USA 1997 
 #include<stdio.h>
#include<iostream>
using namespace std;
int n,m;
char map[][];
int rx[]={,,,-,,,-,-};
int ry[]={,-,,,,-,,-};
void del(int i,int j)
{
map[i][j]='*';
int x,y,t;
for(t=;t<;t++)
{
x=i+rx[t];
y=j+ry[t];
if(x>=&&x<n&&y>=&&y<m&&map[x][y]=='@')
del(x,y);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
{
int i,j,num=;
for(i=;i<n;i++)
for(j=;j<m;j++)
cin>>map[i][j];
for(i=;i<n;i++)
for(j=;j<m;j++)
if(map[i][j]=='@')
{
num++;
del(i,j);
}
printf("%d\n",num);
}
return ;
}

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

  1. hdu 1241Oil Deposits(dfs模板)

    题目链接—— http://acm.hdu.edu.cn/showproblem.php?pid=1241 首先给出一个n*m的字符矩阵,‘*’表示空地,‘@’表示油井.问在这个矩阵中有多少组油井区? ...

  2. hdu 1241Oil Deposits(BFS)

    题目链接: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. HDOJ(HDU).1015 Safecracker (DFS)

    HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...

  5. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  6. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...

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

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

  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 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

    Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...

随机推荐

  1. Leetcode-39-Submission Details (Medium)

    1.首先对数组进行排序 2.递归搜索符合的元素 3.注意回溯 超越67%的用户 #!/usr/local/bin/python3 # -*- coding: utf-8 -*- __author__ ...

  2. JS 获取网页的宽高

    网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...

  3. jvm的垃圾回收几种理解

    1.引用计数器回收 给每个对象设置一个计数器,当该对象被引用时,计数器加1,当有其他变量不再引用该对象时,计数器减1.直到计数器数值为0,回收器视为他是‘垃圾’,可以被回收,当该对象被回收时,其他引用 ...

  4. zabbix 布署实践【3 proxy安装】

    使用openstack在生产环境创建的一台虚拟机   环境 CentOS7 4核4G内存40G硬盘 IP:10.120.150.150 镜像默认关闭防火墙,selinux ,NetworkManage ...

  5. 新Mac 开机启动MySQL/MongoDB/Redis 等服务

    在Mac上我们使用[homebrew]包管理工具(http://brew.sh/index_zh-cn.html)来安装和管理开发工具包,例如:mysql.php.redis.只需要一个命令 brew ...

  6. SQL如何实现远程数据库链接

    利用sp_addlinkeserver 进行远程数据库连接可以把多个数据库的数据,放置到一个数据库中, 或者有设置操作权限的情况下,我们可以通过这种方式进行查询,备份数据等操作. 首先,我们要创建连接 ...

  7. dplyr 数据操作 常用函数(5)

    继续来了解dplyr中的其他有用函数 1.sample() 目的是可以从一个数据框中,随机抽取一些行,然后组成新的数据框. sample_n(tbl, size, replace = FALSE, w ...

  8. attr绑定

    目的 attr绑定可以设置DOM元素的属性值.例如,当我们需要设置title的值,或者一个img的src值,或者一个超链接的href,使用属性绑定会在model的值发生改变时动态的更新绑定的属性值. ...

  9. anglarjs概述

    anglarjs 是一个MVC框架,是开发单页面应用的上上之选,它不是一个功能库,是一个开发动态页面的html框架.专注于扩展html功能,提供动态数据绑定,并能够与jquery合作融洽. 要定义一个 ...

  10. java运行时数据区域

    数据区域有:程序计步器,虚拟机栈,本地方法栈,java堆,方法区 程序计步器: 它是一块较小的内存空间,它的作用可以看做是当先线程所执行的字节码的信号指示器. 每一条JVM线程都有自己的PC寄存器,各 ...