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<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3fffffff
using namespace std;
const int maxn = ;
int num;
int n,m;//注意!定义全局变量不能在函数里面重定义了!!
char a[maxn][maxn];
int judge(int x,int y)
{
if(x<||y<||x>=n||y>=m||a[x][y]=='*')
return ;
return ;
}
struct Node
{
int x,y;
}; int d[][]={{,-},{,},{-,},{,},{-,-},{,-},{-,},{,}}; queue<Node> q; void bfs(int i,int j)
{ Node fir,tmp;
fir.x=i;
fir.y=j;
q.push(fir); while(!q.empty())
{
fir=q.front();
q.pop(); for(int i=;i<;i++)//遍历8个方向
{ tmp.x=fir.x+d[i][];
tmp.y=fir.y+d[i][]; if(judge(tmp.x,tmp.y))
{
q.push(tmp);
a[tmp.x][tmp.y]='*';
}
}
}
return ;
} int main()
{ while(~scanf("%d%d",&n,&m))
{
if(n==&&m==) break;
num=;
for(int i=;i<n;i++)
scanf("%s",&a[i]);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j]=='@')
{
bfs(i,j);
num++;
}
}
}
printf("%d\n",num);
}
return ;
}

F - Oil Deposits 【地图型BFS+联通性】的更多相关文章

  1. CSU-ACM2016暑期集训训练4-BFS(F - Oil Deposits)

    F - Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  2. HDU 1241 Oil Deposits (DFS or BFS)

    链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...

  3. G - Rescue 【地图型BFS+优先队列(有障碍物)】

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...

  4. C - 你经历过绝望吗?两次! 【地图型BFS+优先队列(障碍物)】

    4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的“猪坚强”.不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川“猪坚 ...

  5. B - ACM小组的古怪象棋 【地图型BFS+特殊方向】

    ACM小组的Samsara和Staginner对中国象棋特别感兴趣,尤其对马(可能是因为这个棋子的走法比较多吧)的使用进行深入研究.今天他们又在 构思一个古怪的棋局:假如Samsara只有一个马了,而 ...

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

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

  7. HDU 1241 Oil Deposits (DFS/BFS)

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

  8. (简单) POJ 1562 Oil Deposits,BFS。

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  9. Oil Deposits 搜索 bfs 强联通

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

随机推荐

  1. 【bzoj3379】[Usaco2004 Open]Turning in Homework 交作业 区间dp

    题目描述 数轴上有C个点,每个点有一个坐标和一个访问时间,必须在这个时间后到达这个点才算访问完成.可以在某个位置停留.每在数轴上走一个单位长度消耗一个单位的时间,问:访问所有点并最终到B花费的最小时间 ...

  2. java 使用ByteArrayOutputStream和ByteArrayInputStream实现深拷贝

    首先介绍Java中的浅拷贝(浅克隆)和深拷贝(深克隆)的基本概念: 浅拷贝: 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.浅复制仅仅复制所考虑的对象,而 ...

  3. JavaScript实现键盘操作页面跳转

    对于使用笔记本的同学来说,鼠标操作比较费劲,键盘操作比较方便,下面是一段JavaScript写的,用键盘来实现页面跳转.把location后面的改成你要跳转的地址即可,示例是用方向键实现日志页面的前一 ...

  4. [学习笔记]Tarjan&&欧拉回路

    本篇并不适合初学者阅读. SCC: 1.Tarjan缩点:x回溯前,dfn[x]==low[x]则缩点. 注意: ①sta,in[]标记. ②缩点之后连边可能有重边. 2.应用: SCC应用范围还是很 ...

  5. win32 application怎么把结果输出到调试窗口

    方法1: TCHAR str[]; wsprintf(str, TEXT(); OutputDebugString(TEXT("-------lala------\n")); Ou ...

  6. Educational Codeforces Round 54 (Rated for Div. 2) ABCD

    A. Minimizing the String time limit per test 1 second memory limit per test 256 megabytes Descriptio ...

  7. java,jenkins

    以前玩的是hudson ,现在玩的是jenkins.以前用的是Tomcat,现在不知道他们怎么不用... 1,装个Jenkins镜像. 2.配置项目: 先取个名字:exchange 配个svn: 构建 ...

  8. 固定width但是有间隔

    <!DOCTYPE > <html> <head> <title></title> <meta name="name&quo ...

  9. codefoeces problem 671D——贪心+启发式合并+平衡树

    D. Roads in Yusland Mayor of Yusland just won the lottery and decided to spent money on something go ...

  10. bzoj 1026 DP,数位统计

    2013-11-20 08:11 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1026 首先我们用w[i,j]表示最高位是第i位,且是j的 ...