UVA – 12130 Summits

UVA – 12130 Summits

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Description

Download as PDF

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 hd.

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

The 2007 ACM Northwestern European Programming Contest

题意:多么费解的题目啊,找顶点,假设一个点是最高的话那么就是顶点。假设不是的话,可是它到比它到的点的路径中假设有<=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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117669.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月2日 上午9:00
下一篇 2022年1月2日 上午10:00


相关推荐

  • 彻底解决mysql报错:1030, ‘Got error 28 from storage engine‘

    彻底解决mysql报错:1030, ‘Got error 28 from storage engine‘恕我直言,网上文章千篇一律,没一个能解决的,全是说清一下内存就好了,但是并没有教不会的小白清理…==这个问题确实是服务器系统盘满了,mysql指定的临时文件目录满掉,大概就是这个意思.下面解决/dev/vda1系统盘满了,其实我压根不知道/dev/vda1这在哪,是什么,后来了解这是virtio-block类型的设备。科普一下:以’c’开头的一行表示该设备是一个……

    2022年10月21日
    4
  • 实测|企业级场景硬刚OpenClaw,国产智能体bit-Agent小青龙实现全方位碾压!

    实测|企业级场景硬刚OpenClaw,国产智能体bit-Agent小青龙实现全方位碾压!

    2026年3月13日
    3
  • Razor视图引擎

    Razor视图引擎1 ASP NET nbsp MVC3 nbsp 带来了一种新的名为 Razor nbsp 的视图引擎 提供了下列优点 Razor nbsp 的语法简单且清晰 只需要最小化的输入 Razor nbsp 容易学习 语法类似于 nbsp C nbsp 和 nbsp VBVisual nbsp Studio nbsp 对于 nbsp Razor nbsp 提供了智能提示和语法着色 Razor nbsp 视图不需要允许程序或者启动 nbsp Web nbsp 服务器就可以进行测试 2 Razor nbsp 现在提供了一些新的特征 model nbsp

    2026年3月18日
    2
  • 超级P2P搜索引擎

    超级P2P搜索引擎
    搜索Google大家都用过吧?我们正是利用它强劲的搜索功能来突破封锁下载,Google搜索和限制下载有什么关系,没可能实现吧?不要不相信哦,往下看哦!

    http://www.google.com/intl/zh-CN/
    http://www.3721.com/
    http://www.baidu.com/

      首先打开Google,在关键词输入框中输入“indexof/“inurl:lib(双引号为英文状态下),选择“搜索简体中文

    2022年7月18日
    43
  • html中div滚动条设置,DIV滚动条属性及样式设置方式「建议收藏」

    html中div滚动条设置,DIV滚动条属性及样式设置方式「建议收藏」这里向大家描述一下DIV滚动条属性及样式设置,所谓DIV滚动条,就是利用DIV标签,在里面嵌入CSS样式表,加入overflow的属性值,这样,当div所规范的区域内的内容达到一定程序时,滚动条就派上用场。DIV滚动条属性及样式设置所谓DIV滚动条,就是利用DIV标签,在里面嵌入CSS样式表,加入overflow的属性值,这样,当div所规范的区域内的内容达到一定程序时,滚动条就派上用场。其功能大…

    2022年7月12日
    51
  • mysql数据库中sql修改字段类型

    mysql数据库中sql修改字段类型首先说明一下:在mysql数据库中可以对表的字段类型进行修改的,这样的好处是正常情况下原来的数据不会丢失的。  它的语法规则是:altertablenewexamplemodifyidvaechar(20);

    2022年6月10日
    38

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号