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 ...
随机推荐
- node起始——安装并且新建一个node项目
1.安装nodejs和npm http://nodejs.cn/ 2.设置环境变量,安装到那里就在那里配置. 3.express开发框架安装 //命令行输入命令 npm install -g expr ...
- WebRTC 介绍 (转)
google开源了WebRTC项目,网址是:http://code.google.com/p/webrtc/. WebRTC实现了基于网页的视频会议,标准是WHATWG 协议,目的是通过浏览器提供简单 ...
- 标准C程序设计七---52
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- hdu 1565&hdu 1569(网络流--最小点权值覆盖)
方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Codeforces 321D Ciel and Flipboard(结论题+枚举)
题目链接 Ciel and Flipboard 题意 给出一个$n*n$的正方形,每个格子里有一个数,每次可以将一个大小为$x*x$的子正方形翻转 翻转的意义为该区域里的数都变成原来的相反数. ...
- Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)
题目链接 Tree and Queries 题目大意 给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
- Codeforces 599E Sandy and Nuts(状压DP)
题目链接 Sandy and Nuts 题意大概就是给出限制条件求出在该限制条件下树的种数. #include <bits/stdc++.h> using namespace std; # ...
- Codeforces Gym 100338H High Speed Trains 组合数学+dp+高精度
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- 1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- Android在其他线程中更新UI
public class TransferTools { private static final int MSG_START = 1001; private static final int MSG ...