UVA 12130 - Summits(BFS+贪心)
UVA 12130 - Summits
题意:给定一个h * w的图,每一个位置有一个值。如今要求出这个图上的峰顶有多少个。峰顶是这样定义的。有一个d值,假设一个位置是峰顶。那么它不能走到不大于该峰顶高度 - d的位置。假设满足这个条件下。而且无法走到更高的山峰,那么它就是峰顶
思路:利用贪心的策略。把全部点丢到优先队列,每次取出最高的峰值開始找,进行广搜。搜的过程中记录下最大值的点的个数。假设这个是峰顶。就加上这个数。
推断是不是峰顶的方法为,假设广搜过程中。不会找到一个点的能到的最高峰值大于它,那么它就是峰顶,能够在广搜过程边广搜边记录下每一个点能到的最大高度,然后这样一来,事实上每一个点都仅仅会搜到一次,复杂度为O(h
w log(h * w))
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; const int N = 505;
const int D[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int t, h, w, d, g[N][N], vis[N][N]; struct Point {
int x, y, val;
Point(int x, int y, int val = 0) {
this->x = x;
this->y = y;
this->val = val;
}
bool operator < (const Point& a) const {
return val < a.val;
}
}; priority_queue<Point> Q; int bfs(int x, int y, int H) {
queue<Point> Q;
Q.push(Point(x, y));
vis[x][y] = H;
int ans = 1;
int flag = 1;
while (!Q.empty()) {
Point u = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int xx = u.x + D[i][0];
int yy = u.y + D[i][1];
if (xx < 0 || xx >= h || yy < 0 || yy >= w) continue;
if (H - g[xx][yy] >= d) continue;
if (vis[xx][yy] > H) {
flag = 0;
continue;
}
if (vis[xx][yy] == H) continue;
if (g[xx][yy] == H) ans++;
vis[xx][yy] = H;
Q.push(Point(xx, yy));
}
}
if (flag) return ans;
return 0;
} int solve() {
memset(vis, -1, sizeof(vis));
int ans = 0;
while (!Q.empty()) {
Point u = Q.top();
Q.pop();
if (vis[u.x][u.y] != -1) continue;
ans += bfs(u.x, u.y, g[u.x][u.y]);
}
return ans;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &h, &w, &d);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%d", &g[i][j]);
Q.push(Point(i, j, g[i][j]));
}
}
printf("%d\n", solve());
}
return 0;
}
UVA 12130 - Summits(BFS+贪心)的更多相关文章
- UVA12130 Summits(BFS + 贪心)
UVA12130 Summits(BFS + 贪心) 题目链接 题目大意: 给你一个h ∗ w 的矩阵,矩阵的每一个元素都有一个值,代表这个位置的高度. 题目要求你找出这个图中有多少个位置是峰值点.从 ...
- UVA - 12130 Summits
Description Problem G - Summits Time limit: 8 seconds You recently started working for the largest m ...
- BFS+贪心 HDOJ 5335 Walk Out
题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...
- HDU-1072 Nightmare (bfs+贪心)
Nightmare Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- uva 1615 高速公路(贪心,区间问题)
uva 1615 高速公路(贪心,区间问题) 给定平面上n个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D.(n<=1e5) 对于每个 ...
- UVA 10714 Ants 蚂蚁 贪心+模拟 水题
题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...
- UVa 11729 - Commando War(贪心)
"Waiting for orders we held in the wood, word from the front never came By evening the sound of ...
- UVA 10718 Bit Mask 贪心+位运算
题意:给出一个数N,下限L上限U,在[L,U]里面找一个整数,使得N|M最大,且让M最小. 很明显用贪心,用位运算搞了半天,样例过了后还是WA,没考虑清楚... 然后网上翻到了一个人家位运算一句话解决 ...
- UVA 11039-Building designing【贪心+绝对值排序】
UVA11039-Building designing Time limit: 3.000 seconds An architect wants to design a very high build ...
随机推荐
- PHP中的验证码类(完善验证码)
运行结果: <!--vcode.class.php--> <?php class Vcode { private $width; //宽 private $height; //高 p ...
- Python ping 模块
使用socket模块也可以获得域名对应的ip,参考:https://blog.csdn.net/c465869935/article/details/50850598 print socket.get ...
- 谈谈自己对REST、SOA、SOAP、RPC、ICE、ESB、BPM知识汇总及理解(转载)
相关参考文章: 谈谈自己对REST.SOA.SOAP.RPC.ICE.ESB.BPM知识汇总及理解 微服务SOA架构与RPC远程过程调用 SOA和微服务架构的区别 SOA: 维基百科解释:SOA:面向 ...
- jquery中append、prepend, before和after方法的区别(一)
原文:http://blog.csdn.net/woosido123/article/details/64439490 在 jquery中append() 与 prepend()是在元素内插入内容(该 ...
- vue 配合 element-ui使用搭建环境时候遇到的坑
在需要使用element-ui的时候,直接引入文件,发现会报错,解析不了css文件和字体,需要在webpack里面配置上css-loader和style-loader,最好的做法是把element-u ...
- 致命错误:ext/standard/php_smart_str.h:没有那个文件或目录
致命错误:ext/standard/php_smart_str.h:没有那个文件或目录 参考文章:https://blog.csdn.net/jartins/article/details/80371 ...
- LeetCode OJ--Set Matrix Zeroes **
http://oj.leetcode.com/problems/set-matrix-zeroes/ 因为空间要求原地,所以一些信息就得原地存储.使用第一行第一列来存本行本列中是否有0.另外对于第一个 ...
- Javascript实现页面滚动时导航智能定位
遇到的问题: 在做官网的时候,需要滚动定位的区块的图片不确定,无法确定用户浏览区域对应的模块导航 之前的解决方案是: 通过定位滚动条的位置来判断用户浏览区域对应的模块导航,这种方法的弊端是,区块的高度 ...
- react native 添加mobx
"babel-plugin-transform-decorators-legacy": "^1.3.5", "babel-preset-react-n ...
- git上传(本地和远程有冲突时)
一. 冲突的产生:在上次git同步(上传)之后,本地和远程均有更改 二. 处理 1. 丢弃本地,采用远程: git checkout 冲突文件及其路径 如: git checkout bzrobot_ ...