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. 【bzoj2438】[中山市选2011]杀人游戏 Tarjan

    题目描述 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面,查出谁是杀手.警察能够对每一个人进行查证,假如查证的对象是平民,他会告诉警察,他认识的人, 谁是杀手, 谁是平民 ...

  2. Str 函数

    Str 函数 Visual Studio 2005 返回数字的 String 表示形式.     Public Shared Function Str(ByVal Number As Object) ...

  3. TCP/IP Note2

    TCP/IP寻址 TCP/IP使用32个比特或者4个0到255之间的数字来为计算机编址. 1. IP地址 每个计算机必须有一个IP地址才能够连入Internet中. 每个IP包必须有一个地址才能够发送 ...

  4. yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY

    yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY 博客分类: linux   三种解决方案 我采取第三种方案解决的 第一种: linu ...

  5. Ubuntu14.04 换源 阿里云

    sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup sudo vim /etc/apt/sources.list sudo apt-g ...

  6. org.apache.http.conn.HttpHostConnectException: Connection to xxx refused.

    if you are using emulator to run your app for local server. mention the local ip as 10.0.2.2 and hav ...

  7. c语言几个字符串处理函数的简单实现

    直接贴代码: char* strcpy(char *a,char*b){//把字符串b全部拷贝到a中 assert(a != nullptr&&b != nullptr); char ...

  8. Intellij IDEA创建spring MVC项目

    相信各位未来的Java工程师已经接触到了spring MVC这个框架的强大之处,看了很多的教程,都是eclipse的,在intellij IDEA这个强大的工具面前居然不能很顺畅的,今天我就带领大家用 ...

  9. C++11 自动释放锁(转)

    原文转自 https://blog.csdn.net/lmb1612977696/article/details/77712170 c++11加入了很多新的特性,值得我们去探索. 先看一个例子:普通的 ...

  10. 列出top中的pid

    #!/usr/bin/env python import os import string #方法1:通过字符串的isdigits来判断 #filelist = os.listdir('/proc') ...