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
*@*@*
**@**
*@*@*
18
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
 

Sample Output

0
1
2
2
 
分析:
本题为一个dfs题  寻找图中有几块油田  首先依次寻找 找到的标记下  再寻找下一个直到再也找不到为止题较为简单直接贴AC代码:
#include <iostream>
#include <cstring>
using namespace std;
char a[][];
int x,i,j,y;
int t[][]; void bfs(int o,int p, int num) //定义bfs函数
{
if(o<||o>=x||p>=y||p<) return; //过界就退出
if(a[o][p]!='@'||t[o][p]!=) return; // 没有找到或者已经找到过的也退出
t[o][p] = num;
for(int i = -;i <=;i++)
{
for(int j = -;j <= ;j++)
{
if(i != ||j != )
{
bfs(o+i,p+j,num); //继续寻找下个
}
}
}
}
int main()
{
int s ;
while(cin>>x>>y)
{
if(x == && y == ) break;
memset(t,,sizeof(t));
for( i = ; i < x;i++)
{
for( j = ; j < y; j++)
{
cin>>a[i][j];
}
}
s = ;
for(i = ; i < x; i++)
for(j = ; j < y; j++)
{
if(t[i][j]==&&a[i][j]=='@') bfs(i,j,++s); //没有找到过的且是油田 就进入dfs寻找
}
cout<<s<<endl;
}
return ;
}
 

2016HUAS暑假集训训练题 G - Oil Deposits的更多相关文章

  1. 2016huas暑假集训训练题 G-Who's in the Middle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数  刚开始以为是按数学中的 ...

  2. 2016HUAS暑假集训训练题 F - 简单计算器

    Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值.    Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运 ...

  3. 2016HUAS暑假集训训练题 E - Rails

    There is a famous railway station in PopPush City. Country there is incredibly hilly. The station wa ...

  4. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

  5. 2016HUAS暑假集训训练题 D - Find a way

    F                                                                                                   ...

  6. 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...

  7. 2016huasacm暑假集训训练三 G - 还是畅通工程

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/G 这题和上一道题差不多,还更简单点,直接用prim算法就行,直接贴AC代码: im ...

  8. 2016HUAS暑假集训训练2 O - Can you find it?

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/O 这道题是一道典型二分搜素题,题意是给定3个数组 每个数组的数有m个 再给定l个s ...

  9. 2016HUAS暑假集训训练2 L - Points on Cycle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/L 这是一道很有意思的题,就是给定一个以原点为圆心的圆,然后给定 一个点  求最大三 ...

随机推荐

  1. 系统剖析Android中的内存泄漏

    [转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...

  2. 临时变量不能作为非const类型引用形参的实参

    摘要:     非const 引用形参只能与完全同类型的非const对象关联.      具体含义为:(1)不能用const类型的对象传递给非const引用形参:                  ( ...

  3. java读取properties配置文件信息

    一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...

  4. 在windows环境中用eclipse搭建hadoop开发环境

    1. 整体环境和设置 1.1 hadoo1.0.4集群部署在4台VMWare虚拟机中,这四台虚拟机都是通过NAT模式连接主机 集群中/etc/hosts文件配置 #本机127.0.0.1 localh ...

  5. MFC CPen CBrush CFont

    在OnDraw函数内定义后使用 使用时,要pDC->SelectObject(),如pDC->SelectObject(font); CPen://draw line Pen.Create ...

  6. Python学习笔记07

      时间: tickets 时间元组 格式化的时间 日历 import time tickets = time.time() print tickets print time.localtime() ...

  7. HTML DOM Document

    Document 对象 每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 提示:Document 对 ...

  8. HDU5772 String problem(最大权闭合子图)

    题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...

  9. SqlServer事务回滚(2)

    SQL Server 2008中SQL应用系列--目录索引 SQL事务 一.事务概念    事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是 ...

  10. SOAPUI测试步骤之流量控制(Conditional Goto)

    1. TestSteps流量控制 1.1.有条件转到一步步测试 Conditional Goto拥有任意数量的的XPath表达式伴随相应的目标测试步骤.这些被应用到在先前的采样测试的最近的响应; 配置 ...