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 ...
随机推荐
- 正式进军Matlab图像处理
Matlab取整函数有:fix, floor, ceil, round,具体应用方法如下: 1. fix朝零方向取整,如fix(-1.3) = -1; fix(1.3) = 1; 2. floor顾名 ...
- [BZOJ1339] [Baltic2008] Mafia / 黑手党
Description 匪徒准备从一个车站转移毒品到另一个车站,警方准备进行布控. 对于每个车站进行布控都需要一定的代价, 现在警方希望使用最小的代价控制一些车站,使得去掉这些车站后,匪徒无法从原定的 ...
- AOJ.800 热身之开关灯
热身之开关灯 Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 276 Submis ...
- codeforces902C. Hashing Trees
https://codeforces.com/contest/902/problem/C 题意: 给你树的深度和树的每个节点的深度,问你是否有重构,如果有重构输出两个不同的结构 题解: 如果相邻节点的 ...
- 桥接物理网卡,pipwork指定ip,外网连接,研究salt+docker
1.桥接物理网卡: 首先下载工具: yum -y install --enablerepo=epel bridge-utils 停止服务: 983 systemctl stop docker 删除do ...
- Windows Time Client
Timezone: UTC Coordinated Universal Time ====Perform by Local / administrator must,configure Time se ...
- [洛谷P1382] 楼房
题目描述 地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i],0),右上角为(r[i],h[i]).地平线高度为0.在轮廓 ...
- 关于UML
http://www.cnblogs.com/zfc2201/archive/2011/08/16/2141433.html
- postman接口间关联
现有A.B两个接口,A接口返回的response的body中的部分数据,是B接口发起请求部分数据的来源. A接口response的body结构如下: { "address": &q ...
- decimal数据类型
DECIMAL(N,M)中M是小数部分的位数,若插入的值未指定小数部分或者小数部分不足M位则会自动补到M位小数,若插入的值小数部分超过了M为则会发生截断,截取前M位小数. N是整数部分加小数部分的总长 ...