BFS(染色) LA 3977 Summits
题意:题意坑爹。问符合条件的的山顶个数
分析:降序排序后从每个点出发,假设为山顶,如果四周的点的高度>h - d那么可以走,如果走到已经走过的点且染色信息(山顶高度)不匹配那么就不是山顶。重点在于就算知道不是山顶也要染色完。
#include <bits/stdc++.h>
using namespace std; const int N = 5e2 + 5;
const int INF = 0x3f3f3f3f; int h, w, d;
struct Point {
int x, y, z;
Point() {}
Point(int x, int y, int z) : x (x), y (y), z (z) {}
bool operator < (const Point &r) const {
return z > r.z;
}
};
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
vector<Point> ps;
int mat[N][N];
int peak[N][N];
bool vis[N][N]; bool judge(int x, int y, int az) {
if (x < 1 || x > h || y < 1 || y > w || mat[x][y] <= az - d) return false;
else return true;
} int BFS(Point &a) {
if (vis[a.x][a.y]) return 0;
vis[a.x][a.y] = true; peak[a.x][a.y] = a.z;
int cnt = 1;
bool flag = true;
queue<Point> que; que.push (a);
while (!que.empty ()) {
Point r = que.front (); que.pop ();
for (int i=0; i<4; ++i) {
int tx = r.x + dx[i];
int ty = r.y + dy[i];
if (!judge (tx, ty, a.z)) continue;
if (vis[tx][ty]) {
if (peak[tx][ty] != a.z) {
flag = false;
}
continue;
}
vis[tx][ty] = true; peak[tx][ty] = a.z;
que.push (Point (tx, ty, mat[tx][ty]));
if (mat[tx][ty] == a.z) cnt++;
}
}
if (!flag) cnt = 0;
return cnt;
} int run(void) {
memset (vis, false, sizeof (vis));
memset (peak, 0, sizeof (peak));
int ret = 0;
for (int i=0; i<ps.size (); ++i) {
ret += BFS (ps[i]);
}
return ret;
} int main(void) {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d%d", &h, &w, &d);
ps.clear ();
for (int i=1; i<=h; ++i) {
for (int j=1; j<=w; ++j) {
scanf ("%d", &mat[i][j]);
ps.push_back (Point (i, j, mat[i][j]));
}
}
sort (ps.begin (), ps.end ());
printf ("%d\n", run ());
} return 0;
}
BFS(染色) LA 3977 Summits的更多相关文章
- UVALive 3977 BFS染色
这个题意搞了半天才搞明白 就是如果定义一个d-summit,即从该点到另一个更高的点,经过的路径必定是比当前点低至少d高度的,如果该点是最高点,没有比他更高的,就直接视为顶点 其实就是个BFS染色,先 ...
- HDU 2444 二分图判断 (BFS染色)+【匈牙利】
<题目链接> 题目大意: 有N个人,M组互相认识关系互相认识的两人分别为a,b,将所有人划分为两组,使同一组内任何两人互不认识,之后将两个组中互相认识的人安排在一个房间,如果出现单人的情况 ...
- 【Luogu】P1330封锁阳光大学(bfs染色)
题目链接 这题恶心死我了. bfs染色,统计每个联通块两色的个数,ans加它们的最小值. #include<cstdio> #include<cctype> #include& ...
- XMU 1617 刘备闯三国之汉中之战 【BFS+染色】
1617: 刘备闯三国之汉中之战 Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 6 Solved: 5[Submit][Status][Web B ...
- UVALive - 3977 Summits (BFS染色)
题目大意:坑爹的题目.题意那么难理解. 讲的就是,假设该点是山顶的话(高度为h).那么以该点为中心,往外辐射.走高度大于h-d的点,到达不了还有一个比它高的点 这就提示了,高度要从大到小排序,依次以高 ...
- hdu4751Divide Groups(dfs枚举完全图集合或者bfs染色)
/************************************************************************* > File Name: j.cpp > ...
- Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
- CF796D Police Stations BFS+染色
题意:给定一棵树,树上有一些点是警察局,要求所有点到最近的警察局的距离不大于 $d$,求最多能删几条边 ? 题解: 考虑什么时候一条边可以被断开:这条边的两个端点被两个不同的警察局覆盖掉. 我们要设计 ...
- CodeForces-598D(BFS,染色)
链接: https://vjudge.net/problem/CodeForces-598D 题意: Igor is in the museum and he wants to see as many ...
随机推荐
- python基础——访问限制
python基础——访问限制 在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑. 但是,从前面Student类的定义来看,外部代码还 ...
- NYOJ题目1048破门锁
- java 接口与继承
一.继承条件下的构造方法调用 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构 ...
- Loadrunner之HTTP接口测试脚本实例
接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文结果与预期结果进行比对的过程,接口测试可以通过Jav ...
- DOM – 4.doucument属性
4.document属性 2.1 write 2.2 getElementById 方法 getElementsByName getElementsByTagName 案例:全选反选 案例:点击一个按 ...
- windows 10 上office2016 word崩溃的解决方案
方案1:--个人试验可行,把如下路径改成自己系统路径 C:Documents and Settings/Administrator/application/data/microsoft/templat ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- JavaScript - 事件流
事件流 事件冒泡就是事件沿DOM树向上传播,在没一级节点上都会发生,直至传播到document对象. 事件捕获正好相反,但是老版本的浏览器不支持,因此很少有人使用事件捕获. 事件处理程序 HTMl 事 ...
- jquery 生成 html 绑定
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 利用IdentityServer3在ASP.NET 5和Angular中实现OAuth2 Implicit Flow
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:之前介绍过的IdentityServer3虽然是基于Katana开发的,不过同样可以托 ...