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. 【bzoj3262】陌上花开 CDQ分治+树状数组

    题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...

  2. $.ajax()方法参数总结

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和d ...

  3. LeetCode -- Merge Two Sorted Linked List

    Question: Merge two sorted linked lists and return it as a new list. The new list should be made by ...

  4. HDU 6153 A Secret(扩展KMP模板题)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others) Total ...

  5. 【算法】最小乘积生成树 & 最小乘积匹配 (HNOI2014画框)

    今天考试的时候果然题目太难于是我就放弃了……转而学习了一下最小乘积生成树. 最小乘积生成树定义: (摘自网上一篇博文). 我们主要解决的问题就是当k = 2时,如何获得最小的权值乘积.我们注意到一张图 ...

  6. [NOIP2012] 文化之旅 dfs

    这道题就体现了聪明的搜索策略的重要性,如果我们正着搜,判断效率会明显下滑,所以我们就采用倒着搜索.(其实很玄学.....) #include <cstdio> #include <b ...

  7. 【COGS 2051】王者之剑 最小割

    这个其实就是在说明相邻的点不能取,我们发现只要其满足这个条件他总能走出来,那么我们就最小割就是了,我们先黑白染色,S 一排黑点 一排白点 T 对于相邻的点我们就直接中间连INF,于是就满足只要一个点选 ...

  8. D. Relatively Prime Graph

    Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E( ...

  9. io缓冲为何可以提高效率

    问题 据我了解,运用FileInputStream读写一段数据是一个字节一个字节的读取,如果有10个字节大小的文件,就要调用10次系统调用,每次将读取的数据赋值给变量,然后程序使用变量. 缓冲区可以看 ...

  10. IDEA无法编译java8的lambda表达式提示Error:(16, 48) java: -source 1.5 中不支持 lambda 表达式

    在idea中新建了一个java8的项目,但是写lambda表达式提示语法错误,提示如下错误信息: Error:(16, 48) java: -source 1.5 中不支持 lambda 表达式 (请 ...