题目链接:https://ac.nowcoder.com/acm/contest/1870/J

题目大意:求最大的连通块是多大

主要是为了防止自己忘记bfs怎么写。。。。。

 #include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = ; int l, h, vis[MAXN][MAXN];
char map[MAXN][MAXN];
int dx[] = {, , , , , -, -, -}; //方向数组
int dy[] = {, -, , , -, , , -}; struct Node
{
int x, y;
}; int check(int x, int y)
{
if(x < || x > h || y < || y > l) //不越界
return ;
if(map[x][y] != '.') //不可走
return ;
if(vis[x][y] == ) //已经走过
return ;
return ;
} int bfs(int x, int y)
{
int sum = ;
queue<Node> Q;
while(!Q.empty()) Q.pop();
Node no;
no.x = x, no.y = y;
vis[x][y] = ;
sum ++;
Q.push(no);
while(!Q.empty())
{
Node a = Q.front();
Q.pop();
for(int k = ; k < ; k ++)
{
Node next = a;
next.x += dx[k];
next.y += dy[k];
if(check(next.x, next.y))
{
sum ++;
vis[next.x][next.y] = ;
Q.push(next);
}
}
}
return sum;
} int main()
{
int ans = -;
scanf("%d%d", &l, &h);
getchar();
for(int i = ; i <= h; i ++)
scanf("%s", map[i] + );
for(int i = ; i <= h; i ++)
for(int j = ; j <= l; j ++)
if(!vis[i][j] && map[i][j] == '.') //进入bfs的条件
ans = max(ans, bfs(i, j));
printf("%d\n", ans);
return ;
}

Feeding Time 【bfs求最大连通块】的更多相关文章

  1. 链表加bfs求补图联通块

    https://oj.neu.edu.cn/problem/1387 给一个点数N <= 100000, 边 <= 1000000的无向图,求补图的联通块数,以及每个块包含的点数 由于点数 ...

  2. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  3. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  4. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

  5. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  6. 中矿新生赛 H 璐神看岛屿【BFS/DFS求联通块/连通块区域在边界则此连通块无效】

    时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 璐神现在有张n*m大小的地图,地图上标明了陆地(用 ...

  7. Codeforces 987 K预处理BFS 3n,7n+1随机结论题/不动点逆序对 X&Y=0连边DFS求连通块数目

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  8. UVA 572 dfs求连通块

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  9. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

随机推荐

  1. Ubuntu18.04开机动画(bootsplash)安装

    一.搜索喜欢的主题 1.通过软件源搜索,这个比较简单但是没有太喜欢的.-----------------------------------------------------------pipci@ ...

  2. 【源码拾遗】从vue-router看前端路由的两种实现

    本文由浅入深观摩vue-router源码是如何通过hash与History interface两种方式实现前端路由,介绍了相关原理,并对比了两种方式的优缺点与注意事项.最后分析了如何实现可以直接从文件 ...

  3. jsp页面中的EL表达式不被解析org.apache.jasper.JasperException: Unable to convert string [${item.createtime}]

    https://m.imooc.com/qadetail/277572 web.xml的版本是不是2.3, 如果是2.3,在jsp页面开头添加<%@ page isELIgnored=" ...

  4. OpenFOAM动网格技术介绍【转载】

    转载自:http://blog.sina.com.cn/s/blog_e256415d0101nfhp.html Chalmers大学的Andreu Oliver González对OpenFOAM中 ...

  5. lucene正向索引——正向信息,Index –> Segments (segments.gen, segments_N) –> Field(fnm, fdx, fdt) –> Term (tvx, tvd, tvf)

    转自:http://www.cnblogs.com/forfuture1978/archive/2009/12/14/1623599.html 上面曾经交代过,Lucene保存了从Index到Segm ...

  6. Linux信号使用及自定义信号

    linux自定义信号:https://www.cnblogs.com/bigben0123/p/3186661.html linux信号.值及解释:https://blog.csdn.net/luot ...

  7. TortoiseSVN 使用教程

    TortoiseSVN 使用教程 TortoiseSVN 是 Subversion 版本控制系统的一个免费开源客户端,可以超越时间的管理文件和目录. TortoiseSVN 安装 下载地址:https ...

  8. vuecli3集成easyui

    思路是这样的,首先要将jquery设置成全局,然后就可以正常使用easyUI了. jquery安装命令: npm install --save jquery jquery-easyui安装命令: np ...

  9. Web前端笔记整理

    不使用Ajax无刷新提交: header('HTTP/1.1 204 No Content'); var a=document.createElement('img'); a.setAttribute ...

  10. 010-多线程-JUC集合-Queue-ConcurrentLinkedQueue

    一.概述 ConcurrentLinkedQueue是线程安全的队列,它适用于“高并发”的场景. 它是一个基于链接节点的无界线程安全队列,按照 FIFO(先进先出)原则对元素进行排序.队列元素中不可以 ...