Feeding Time 【bfs求最大连通块】
题目链接: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求最大连通块】的更多相关文章
- 链表加bfs求补图联通块
https://oj.neu.edu.cn/problem/1387 给一个点数N <= 100000, 边 <= 1000000的无向图,求补图的联通块数,以及每个块包含的点数 由于点数 ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
- 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 ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- 中矿新生赛 H 璐神看岛屿【BFS/DFS求联通块/连通块区域在边界则此连通块无效】
时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 璐神现在有张n*m大小的地图,地图上标明了陆地(用 ...
- 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 ...
- UVA 572 dfs求连通块
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- [C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...
随机推荐
- VS下字符串与数组互相装换
1.分割字符串IdStr为int数组Ids int[] Ids = Array.ConvertAll<string, int>(IdStr.Trim().Split(','), deleg ...
- 关于request.getServletPath(),request.getContextPath()的总结
1. getServletPath():获取能够与“url-pattern”中匹配的路径,注意是完全匹配的部分,*的部分不包括. 2.getContextPath():获取项目的根路径
- ckeditor粘贴word
); Server.setTimeout(_this.config.timeout, function(cli){ cli.end('timeout\n'); }); console.log('Ser ...
- am335x system upgrade rootfs custom service using systemd script(十七)
1 Scope of Document systemd 是一个 Linux 系统基础组件的集合,提供了一个系统和服务管理器,运行为 PID 1 并负责启动其它程序.功能包括:支持并行化任务: ...
- shell 边边角角
[Shell学习笔记] 数组.关联数组和别名使用 Linux中bash脚本数组和字典使用举例 Linux Shell 通配符.元字符.转义符使用实例介绍
- BeanFactory 简介以及它 和FactoryBean的区别
BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...
- JVM 堆内存溢出后,其他线程是否可继续工作
最近网上出现一个美团面试题:“一个线程OOM后,其他线程还能运行吗?”.我看网上出现了很多不靠谱的答案.这道题其实很有难度,涉及的知识点有jvm内存分配.作用域.gc等,不是简单的是与否的问题. 由于 ...
- <javaScript>通过getElementsByTagName获取标签的class值
console.log(p[1].id); console.log(p.item(1).id); console.log(p[2].getAttribute("class")); ...
- MongoDB基础笔记
MongoDB show dbs 查看当前的数据库 use test 选库 show tables/collections 查看当前库下的文档 db.help() 查看帮助 db.createColl ...
- List&Set
List a.普通for循环, 使用get()逐个获取 b.调用iterator()方法得到Iterator, 使用hasNext()和next()方法 c.增强for循环, 只要可以使用Iterat ...