FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。
为了能够对旅程有一个安排,他想知道山峰和山谷的数量。
给定一个地图,为FGD想要旅行的区域,地图被分为 n×nn×n 的网格,每个格子 (i,j)(i,j) 的高度 w(i,j)w(i,j) 是给定的。
若两个格子有公共顶点,那么它们就是相邻的格子,如与 (i,j)(i,j) 相邻的格子有(i−1,j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1)(i−1,j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1)。
我们定义一个格子的集合 SS 为山峰(山谷)当且仅当:
- SS 的所有格子都有相同的高度。
- SS 的所有格子都连通。
- 对于 ss 属于 SS,与 ss 相邻的 s′s′ 不属于 SS,都有 ws>ws′ws>ws′(山峰),或者 ws<ws′ws<ws′(山谷)。
- 如果周围不存在相邻区域,则同时将其视为山峰和山谷。
你的任务是,对于给定的地图,求出山峰和山谷的数量,如果所有格子都有相同的高度,那么整个地图即是山峰,又是山谷。
输入格式
第一行包含一个正整数 nn,表示地图的大小。
接下来一个 n×nn×n 的矩阵,表示地图上每个格子的高度 ww。
输出格式
数据范围
1≤n≤10001≤n≤1000,
0≤w≤1090≤w≤109
输入样例1:
5 8 8 8 7 7 7 7 8 8 7 7 7 7 7 7 7 8 8 7 8 7 8 8 8 8
输出样例1:
输入样例2:
5 5 7 8 3 1 5 5 7 6 6 6 6 6 2 8 5 7 2 5 8 7 1 0 1 7
输出样例2:
样例解释
样例1:
样例2:
#include <iostream>#include <cstring>#include <algorithm>#define x first#define y secondusing namespace std;typedef pair<int,int> PII;const int N = 1010;int h[N][N];bool st[N][N];PII q[N*N];int n;void bfs(int sx, int sy, bool &has_higher, bool &has_lower) {int hh = 0, tt = 0;q[0] = {sx,sy};st[sx][sy] = true;while (hh <= tt) {auto t = q[hh++];for (int i = t.x-1; i <= t.x+1; ++i)for (int j = t.y-1; j <= t.y+1; ++j) {if (i == t.x && j == t.y) continue;if (i < 0 || i >= n || j < 0 || j >= n) continue;//山的边界if (h[t.x][t.y] != h[i][j]) {if (h[t.x][t.y] < h[i][j]) has_higher = true;else has_lower = true;} else if (!st[i][j]) {//注意这里只标记一个连通块里面的st[i][j] = true;q[++tt] = {i,j};}}}}int main () {cin >> n;for (int i = 0; i < n; ++i)for (int j = 0; j < n; ++j)cin >> h[i][j];int peaks = 0, valley = 0;for (int i = 0; i < n; ++i)for (int j = 0; j < n; ++j) {if (!st[i][j]) {bool has_higher = false, has_lower = false;bfs(i,j,has_higher,has_lower);if (!has_higher) peaks++;if (!has_lower) valley++;}}cout << peaks << " " << valley << endl;return 0;}
