UVA - 12130 Summits
Description
Problem G - Summits
Time limit: 8 seconds
You recently started working for the largest map drawing company in theNetherlands. Part of your job is to determine what the summits in aparticular landscape are. Unfortunately, it is not so easy to determinewhich points are summits and which are not, because
we do not want tocall a small hump a summit. For example look at the landscape given bythe sample input.
We call the points of height 3 summits, since there are no higherpoints. But although the points of height 2, which are to theleft of the summit of height 3, are all higher than or equal totheir immediate neighbours, we do notwant to call them summits, because
we can reach a higher point fromthem without going to low (the summits of height 3). In contrast,we do want to call the area of height 2 on the right a summit, sinceif we would want to walk to the summit of height 3, we first have todescend to a point with
height 0.
After the above example, we introduce the concept of a d-summit. Apoint, with height
h, is a d-summit if and only if it isimpossible to reach a higher point without going through an area withheight smaller than or equal to
h-d.
The problem is, given a rectangular grid of integer heights and aninteger
d, to find the number of d-summits.
Input
On the first line one positive number: the number of testcases, atmost 100. After that per testcase:
- One line with three integers 1 ≤ h ≤ 500, 1 &le w ≤ 500 and 1 ≤
d ≤ 1000000000. h and w are the dimensions of the map.
d is as defined in the text. - h lines with w integers, where the xth integer on the
yth line denotes the height 0 ≤ h ≤ 1000000000 of the point (x,
y).
Output
Per testcase:
- One line with the number of summits.
Sample Input
1
6 10 2
0 0 0 0 0 0 0 0 0 0
0 1 2 1 1 1 1 0 1 0
0 2 1 2 1 3 1 0 0 0
0 1 2 1 3 3 1 1 0 0
0 2 1 2 1 1 1 0 2 0
0 0 0 0 0 0 0 0 0 0
Sample Output
4
题意:多么费解的题目啊,找顶点,假设一个点是最高的话那么就是顶点。假设不是的话,可是它到比它到的点的路径中假设有<=h-d(h为该点的高度)那么就不能去,那么它就是顶点
思路:首先找个性质:假设A->B,C->B,假设HA>HC,由于HA-d>HC-d,那么C->A,所以我们先按高度排序,然后逐个BFS,假设它的周围能找到跟它一样高的点。那么这些点都是顶点。假设遇到已经被较高找到的点。那么它就也能够到那个较高的点。那么它就不是顶点
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 510; struct node {
int x, y, h;
node(int _x = 0, int _y = 0, int _h = 0) {
x = _x;
y = _y;
h = _h;
}
} arr[MAXN*MAXN];
int map[MAXN][MAXN];
int vis[MAXN][MAXN];
int n, m, d;
int cnt;
int dx[4]={1, -1, 0, 0};
int dy[4]={0, 0, 1, -1};
queue<node> q; int cmp(node a, node b) {
return a.h > b.h;
} void cal() {
memset(vis, -1, sizeof(vis));
int ans = 0;
while (!q.empty())
q.pop();
for (int i = 0; i < cnt; i++) {
node cur = arr[i];
if (vis[cur.x][cur.y] != -1)
continue;
int flag = 1;
int bound = cur.h - d;
int top = cur.h;
q.push(cur);
int tot = 1;
while (!q.empty()) {
cur = q.front();
q.pop();
vis[cur.x][cur.y] = top;
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (nx < 1 || ny < 1 || nx > n || ny > m)
continue;
if (map[nx][ny] <= bound)
continue;
if (vis[nx][ny] != -1) {
if (vis[nx][ny] != top)
flag = 0;
continue;
}
node tmp;
tmp.x = nx, tmp.y = ny, tmp.h = map[nx][ny];
vis[nx][ny] = top;
if (tmp.h == top)
tot++;
q.push(tmp);
}
}
if (flag)
ans += tot;
}
printf("%d\n", ans);
} int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &n, &m, &d);
cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &map[i][j]);
arr[cnt++] = node(i, j, map[i][j]);
}
sort(arr, arr+cnt, cmp);
cal();
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
UVA - 12130 Summits的更多相关文章
- UVA 12130 - Summits(BFS+贪心)
UVA 12130 - Summits 题目链接 题意:给定一个h * w的图,每一个位置有一个值.如今要求出这个图上的峰顶有多少个.峰顶是这样定义的.有一个d值,假设一个位置是峰顶.那么它不能走到不 ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- UVA数学入门训练Round1[6]
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
随机推荐
- java中怎么终止一个线程的执行----个人学习心得
参考了一下两个网站的介绍: ①:http://blog.csdn.net/liuhanhan512/article/details/7077601 ②:http://www.blogjava.net/ ...
- iOS发展 ---- 至iPhone 6自适应布局设计 Auto Layout
Apple从iOS 6增加了Auto Layout后開始就比較委婉的開始鼓舞.建议开发人员使用自适应布局,可是到眼下为止,我感觉大多数开发人员一直在回避这个问题,无论是不是因为历史原因造成的,至少他们 ...
- ssh 即使主机,同nohup背景脚本
下面的脚本工具:先从本地副本的脚本到远程主机,然后ssh即使在远程主机,脚本的运行副本前(因为脚本需要运行很长,它运行在后台),该脚本仅用于备忘录,如果请指点不足! #!/bin/bash cd /t ...
- DOM的event对象的属性和方法
属性/方法 类型 是否可读写 描写叙述 altKey Boolean 读写 指示是否按下alt键 bubbles Boolean 读 指示事件是否冒泡 button Intrger 读写 鼠标事件发生 ...
- TreeSet排序
TreeSet的排序能够通过两种方法来实现: 1.通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比較器进行排序. 2. ...
- HDU 4454 - Stealing a Cake(三分)
我比较快速的想到了三分,但是我是从0到2*pi区间进行三分,并且漏了一种点到边距离的情况,一直WA了好几次 后来画了下图才发现,0到2*pi区间内是有两个极值的,每个半圆存在一个极值 以下是代码 #i ...
- TP 控制器扩展_initialize方法实现原理
参考网址:http://gongwen.sinaapp.com/article-59.html 控制器扩展接口 系统Action类提供了一个初始化方法_initialize接口,可以用于扩展需要,_i ...
- 【C/C++】BinarySearch
/* * BinarySearch.c * * Created on: 2014年6月25日 * Author: wenbo */ #include <stdio.h> int binar ...
- Coder的利器
Coder的利器记载 工作近三年,使用PC快六年,拥抱Mac整一年,投具器石榴裙三年.14年第一次被同事推荐Everything,开启了JeffJade对工具的折腾之旅,并乐此不疲.时去两年,这必然是 ...
- windows phone (26) ApplicationBar应用程序栏
原文:windows phone (26) ApplicationBar应用程序栏 在应用程序中,如果需要几个按钮或者菜单来执行一些普通的命令,就应该考虑使用ApplicationBar,因为silv ...