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. Web Tours自带示例网站无法打开的解决方案

    问题现象: LoadRunner自带的测试样品,旅行社机票预订系统HP Web Tours以下简称为Web Tours. 1.LoadRunner程序的Sample目录下无Web和Web Tours服 ...

  2. 端口偷窃(Port Stealing)技术

    端口偷窃(Port Stealing)技术   该技术主要用于局域网中间人攻击中,尤其目标计算机采用静态ARP后,导致ARP欺骗无效.   背景知识:路由器为了方便转发数据包,会在内部记录每个接口和M ...

  3. http://blog.csdn.net/lipeng32768/article/details/50845547

    http://blog.csdn.net/lipeng32768/article/details/50845547

  4. PHP历程(PHP与MYSQL数据库之间连接、创建和关闭)

    <?php define('WXLEVELS_DB_HOST','127.0.0.1'); //服务器 define('WXLEVELS_DB_USER','root'); //数据库用户名 d ...

  5. spark 部署问题

    spark的web UI 端口设置:spark-env.sh 中设置SPARK_MASTER_WEBUI_PORT 为自己想设置的端口号. 其他worker 的web UI 端口默认:8081 mas ...

  6. React的第一步

    首先了解React中所牵扯到的几个重要的概念 什么是React? 是Facebook的开发团队开发出来的一个用于构建用户界面个js库,最近才开源出来公布于世,它的初衷是用于创建“独立的视图组件”,一个 ...

  7. javascript拾掇

    用javascript如何给span赋值呢?一般有两种方法: 1>输出html <body onload="s()"><span id="hell ...

  8. Linux添加Terminal快捷键和文件系统的总结

    一:Fedora 怎样设置终端快捷键 百度连接:http://jingyan.baidu.com/article/cb5d61053598ed005d2fe05c.html ubuntu里面不用设置就 ...

  9. iOS学习16之OC集合遍历和数组排序

    1.集合遍历 1> 遍历 集合(Collection):OC中提供的容器类:数组,字典,集合. 遍历:对集合中元素依次取出的过称叫做遍历. 三种方式:① for循环遍历: ② NSEnumera ...

  10. 【python游戏编程之旅】第六篇---pygame中的Sprite(精灵)模块和加载动画

    本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 直到现在我们已经学了很多pygame基础知识了,从这篇博客开始我们将会学习pygame中高级部分,更多和精灵 ...