题目传送门

题意:题意坑爹。问符合条件的的山顶个数

分析:降序排序后从每个点出发,假设为山顶,如果四周的点的高度>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的更多相关文章

  1. UVALive 3977 BFS染色

    这个题意搞了半天才搞明白 就是如果定义一个d-summit,即从该点到另一个更高的点,经过的路径必定是比当前点低至少d高度的,如果该点是最高点,没有比他更高的,就直接视为顶点 其实就是个BFS染色,先 ...

  2. HDU 2444 二分图判断 (BFS染色)+【匈牙利】

    <题目链接> 题目大意: 有N个人,M组互相认识关系互相认识的两人分别为a,b,将所有人划分为两组,使同一组内任何两人互不认识,之后将两个组中互相认识的人安排在一个房间,如果出现单人的情况 ...

  3. 【Luogu】P1330封锁阳光大学(bfs染色)

    题目链接 这题恶心死我了. bfs染色,统计每个联通块两色的个数,ans加它们的最小值. #include<cstdio> #include<cctype> #include& ...

  4. XMU 1617 刘备闯三国之汉中之战 【BFS+染色】

    1617: 刘备闯三国之汉中之战 Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 6  Solved: 5[Submit][Status][Web B ...

  5. UVALive - 3977 Summits (BFS染色)

    题目大意:坑爹的题目.题意那么难理解. 讲的就是,假设该点是山顶的话(高度为h).那么以该点为中心,往外辐射.走高度大于h-d的点,到达不了还有一个比它高的点 这就提示了,高度要从大到小排序,依次以高 ...

  6. hdu4751Divide Groups(dfs枚举完全图集合或者bfs染色)

    /************************************************************************* > File Name: j.cpp > ...

  7. Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)

    题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...

  8. CF796D Police Stations BFS+染色

    题意:给定一棵树,树上有一些点是警察局,要求所有点到最近的警察局的距离不大于 $d$,求最多能删几条边 ? 题解: 考虑什么时候一条边可以被断开:这条边的两个端点被两个不同的警察局覆盖掉. 我们要设计 ...

  9. CodeForces-598D(BFS,染色)

    链接: https://vjudge.net/problem/CodeForces-598D 题意: Igor is in the museum and he wants to see as many ...

随机推荐

  1. objective-c数组笔记

    数组与可变数组 2015年6月14日 1.数组 数组的初始化方式 1.初始化一个空数组 NSArray *array = [[NSArray alloc] init];//不可变数组,数组内不可以添加 ...

  2. java中的[Ljava.lang.Object;@2a139a55问题

    数据显示为Ljava.lang.Object;@2a139a55问题,是因为你从数据库读出数据后,存入到list集合上时,如果你没有指定要存入的数据的类型,系统会自动给你赋一个object类型,他是所 ...

  3. October 2nd 2016 Week 41st Sunday

    The road to success is lined with many tempting parking spaces. 通往成功的路边充斥着许多诱人的休息区. Exhausted, I thi ...

  4. August 20th 2016 Week 34th Saturday

    Everything you see exists together in a delicate balance. 你所看到的一切都处于微妙的平衡中. Seeking for balance in l ...

  5. Sightseeing(poj 3463)

    题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...

  6. CocoaPods 安装

    虽然网上关于CocoaPods安装教程多不胜数,但是我在安装的过程中还是出现了很多错误,所以大家可以照下来步骤装一下,我相信会很好用. 前言 在iOS项目中使用第三方类库可以说是非常常见的事,但是要正 ...

  7. -A 解决数据库表太多,预读表时间很长

    Reading table information for completion of table and column names You can turn off this feature to ...

  8. 微软MSMQ消息队列的使用

    首先在windows系统中安装MSMQ 一.MSMQ交互 开发基于消息的应用程序从队列开始.MSMQ包含四种队列类型: 外发队列:消息发送到目的地之前,用它来临时存储消息. 公共队列:在主动目录中公布 ...

  9. 【转载】 JQuery.Gantt(甘特图) 开发指南

    转载来自: http://www.cnblogs.com/liusuqi/archive/2013/06/09/3129293.html JQuery.Gantt是一个开源的基于JQuery库的用于实 ...

  10. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...