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. 

InputThe 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. 
OutputFor 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 <bits/stdc++.h>
using namespace std;
#define M 102 char map1[M][M];
int vis[M][M];
int m,n,sum;
int dir[8][2] = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}}; void dfs(int x, int y){
for(int i = 0; i < 8; i++)
{
int dx = x + dir[i][0];
int dy = y + dir[i][1];
if(dx >= 0 && dy >= 0 && dx < m && dy < n && map1[dx][dy] == '@' && !vis[dx][dy]){
vis[dx][dy] = 1;
dfs(dx,dy);
}
}
return;
} int main(){
while(cin>>m>>n && m && n){
memset(vis,0,sizeof(vis));
sum = 0;
getchar();
for(int i = 0; i < m; i++)
scanf("%s",map1[i]);
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
if(map1[i][j] == '@' && !vis[i][j]){
sum++;dfs(i,j);
}
cout<<sum<<endl;
}
return 0;
}

Oil Deposits HDU 1241的更多相关文章

  1. Oil Deposits HDU - 1241 (dfs)

    Oil Deposits HDU - 1241 The GeoSurvComp geologic survey company is responsible for detecting undergr ...

  2. (深搜)Oil Deposits -- hdu -- 1241

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. kuangbin专题 专题一 简单搜索 Oil Deposits HDU - 1241

    题目链接:https://vjudge.net/problem/HDU-1241 题意:问有几个油田,一个油田由相邻的‘@’,组成. 思路:bfs,dfs都可以,只需要遍历地图,遇到‘@’,跑一遍搜索 ...

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

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

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

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

  6. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

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

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

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

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

  9. hdu 1241:Oil Deposits(DFS)

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

随机推荐

  1. Page7:能控性、能观性及其判据和对偶原理(2)[Linear System Theory]

    内容包含连续时间时变系统的能控性和能观测性判据,离散时间线性系统的能控性和能观测性判据,以及对偶原理

  2. [性能优化] perf 高级用法:完整记录程序性能指标,并按照时间段对程序进行有针对性的性能分析

    如题: 假设你已经熟悉了基本用法,知道perf是干嘛的,以及会用 perf top [性能优化] perf 背景:目标程序在运行的某时间段内会出现性能下降,需要了解这个时间内,程序发生了什么. 方法: ...

  3. oracle优化:避免全表扫描

    http://blog.csdn.net/onetree2010/article/details/6098259

  4. 用2个DATETIMEPICKER分别输入时间和日期,再合并成一个DATETIME类型

    DtpDate为日期的,DtpTime为时间的 StrToDateTime(FormatDateTime('yyyy-MM-dd', DtpDate.Date) + ' ' + TimeToStr(D ...

  5. ORACLE监听配置及测试实验(2)

    实验四 在tnsname.ora里添加默认监听代号 [oracle@oracle01 admin]$ vi tnsnames.ora 添加一行 PORT1528=(ADDRESS = (PROTOCO ...

  6. Python+Flash+NodeJS 接口自动化平台

    一.前端安装步骤# manager-web(1)下载项目 git clone https://github.com/t880216t/manager-web.git (2) 安装依赖 cnpm ins ...

  7. 【Python基础】安装python第三方库

    pip命令行安装(推荐) 打开cmd命令行 安装需要的第三方库如:pip install numpy 在安装python的相关模块和库时,我们一般使用“pip install  模块名”或者“pyth ...

  8. ETL : kettle Spoon 转换 + 作业

    Kettle能做什么? 前言 : 需将db2中数据导入到mysql中,利用etl工具进行多表转换.以此为切入点,系统整理.学习kettle工具. 提醒: kettle是纯java编写,机器需要有jre ...

  9. GENIL_BOL_BROWSER, GENIL_MODEL_BROWSER,BSP_WD_CMPWB 使用方法

    一:GENIL_BOL_BROWSER 使用方法 1: 进入x3c系统.输入T-CODE  GENIL_BOL_BROWSER 2: 输入一个component set  名称 3: 选择一个对象,双 ...

  10. 多线程——实现Runnable接口实现一个多线程

    实现Runnable接口实现一个多线程 Runnable接口源码: package java.lang; //Runnable接口源码只有一个run方法 public interface Runnab ...