Topic links: Click here
Note peaks and valleys of the judgment, it may as well hillside
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 1010, M = N * N;
int n;
int h[N][N];
bool st[N][N];
PII q[M];
void bfs(int sx, int sy, bool& has_higher, bool& has_lower)
{
int hh = 0, tt = -1;
q[++tt] = {sx, sy};
st[sx][sy] = true;
while(hh <= tt)
{
PII 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 < 0 || i >= n || j < 0 || j >= n) continue;
if(h[i][j] != h[t.x][t.y])
{
if(h[i][j] < h[t.x][t.y]) has_lower = true;
else has_higher = true;
continue;
}
if(st[i][j]) continue;
q[++tt] = {i, j};
st[i][j] = true;
}
}
}
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
{
scanf("%d", &h[i][j]);
}
}
int peak = 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) peak++;
if(!has_lower) valley++;
}
}
}
printf("%d %d\n", peak, valley);
return 0;
}