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 自定义实现switch开关按钮

    前几天在看蘑菇街上有个开关按钮: 就在想是怎样实现的,于是反编译了它的源码,但是这时得到了下面的几张图片: 图片对应的名称: 无色长条:switch_frame; 白色圆点:switch_btn_pr ...

  2. 重写ViewPager方法,防止滑动广告尾页的时候,Fragment也改变! (如果广告设置为轮播的话,不需要重写ViewPager)

    public class MyViewPager extends ViewPager{ public MyViewPager(Context context) { this(context, null ...

  3. Liferay 6.2 改造系列之十七:当Portlet无权限时,不显示错误信息

    在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Set this to true if users are shown ...

  4. caffe安装(linux)

    从官网github下载caffe-master.zip 解压:unzip caffe-master 将Makefile.config.example复制,命名为Makefile.config(如果是C ...

  5. vs2010 安装MVC 3.0

    下载链接如下: MVC 3安装包:http://www.microsoft.com/downloads/zh-cn/details.aspx?familyid=d2928bc1-f48c-4e95-a ...

  6. D7控件\dw_cd_VirtualTreeview_v4.5.2\Demos\Advanced---TVirtualStringTree用法

    VST1: TVirtualStringTree; //按钮公用函数,根据不同 标签tag区分, Screen.Cursor := crHourGlass; //设置屏幕鼠标的形状为crhourGla ...

  7. 使用python实现栈和队列

    1.使用python实现栈: class stack(): def __init__(self): self.stack = [] def empty(self): return self.stack ...

  8. 关于listView 中的聚焦问题

    我在使用listView+adapter 中,遇到一个问题,就是item项添加了若干个可以被监听的控件后 在listView中的setOnItemClickListener失效了 原因是焦点已经在it ...

  9. uva748 - Exponentiation

    import java.io.*; import java.text.*; import java.util.*; import java.math.*; public class Exponenti ...

  10. Storm-166:Nimbus HA solution based on Zookeeper

    Nimbus HA feature is quite important for our application running on the storm cluster. So, we've bee ...