F - Oil Deposits 【地图型BFS+联通性】
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+联通性】的更多相关文章
- CSU-ACM2016暑期集训训练4-BFS(F - Oil Deposits)
F - Oil Deposits Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- HDU 1241 Oil Deposits (DFS or BFS)
链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...
- G - Rescue 【地图型BFS+优先队列(有障碍物)】
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...
- C - 你经历过绝望吗?两次! 【地图型BFS+优先队列(障碍物)】
4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的“猪坚强”.不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川“猪坚 ...
- B - ACM小组的古怪象棋 【地图型BFS+特殊方向】
ACM小组的Samsara和Staginner对中国象棋特别感兴趣,尤其对马(可能是因为这个棋子的走法比较多吧)的使用进行深入研究.今天他们又在 构思一个古怪的棋局:假如Samsara只有一个马了,而 ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1241 Oil Deposits (DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- (简单) POJ 1562 Oil Deposits,BFS。
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- Oil Deposits 搜索 bfs 强联通
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
随机推荐
- Spark探索经典数据集MovieLens
Spark探索经典数据集MovieLens 阅读目录 前言 环境 初步预览 探索用户数据 探索电影数据 探索评级数据 回到顶部 前言 MovieLens数据集包含多个用户对多部电影的评级数据,也包括电 ...
- eclipse web(Spring+SpringMVC+Hibernate)项目迁移至intellij idea
1.导入Eclipseweb项目 跟着导航一直下一步 出现警告不要担心,先点击确认,到后面再进行设置jdk 成功导入项目后如下图 2.对导入的项目进行配置按Ctrl+shift+alt+s(或下图中的 ...
- npm安装node-sass失败,EACCES: permission denied
增加--unsafe-perm,即 sudo npm install node-sass --unsafe-perm --save-dev 成功安装node-sass
- 如何去掉Json字符串中反斜杠
做项目的时候,遇到了这样的问题,前台传来的Json字符串在实体类中不对应(无法转换为实体类),而且传来的数据项是跟着数据库中的表的变动而变动的(不能重写实体类). 前台Json字符串为: string ...
- [洛谷P1074] 靶形数独
洛谷题目链接:靶形数独 题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博 ...
- [转载]解决clickonce不支持administer权限问题
转自ClickOnce deployment vs. requestedExecutionLevel = requireAdministrator ClickOnce方式部署应用简单方便,估计很多人都 ...
- 【Python实例一】使用minidom读取xml文件
前言:最近刚在廖雪峰老师的网站里学习了Python的基础内容,想着循序渐进地找点实例练练手,网上看到有很多相关资料,决定针对感兴趣的内容实际编码实践一下,昨天刚好看到有关使用Python来读取XML文 ...
- Csharp 非安全代码
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { c ...
- 编辑器KindEditor的使用
1.具体使用方法看点这里 2.下载点这里 3.文件夹说明 ├── asp asp示例,删掉 ├── asp.net asp.net示例,删掉 ├── attached 空文件夹,放置关联文件attac ...
- HDU2389(二分图匹配Hopcroft-Carp算法)
Rain on your Parade Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Ot ...