analysis
Enumerate each point first, and if it has not been visited, search for the entire "block" with the same height.
Then judge whether the block next to it is taller or shorter than him, and
add up the number of peaks and valleys
Upload code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
}q[1000001];
int n,cnt,sf,sg;
int a[1001][1001];
int v[1001][1001];
int dx[9]={
0,0,1,0,-1,1,1,-1,-1};
int dy[9]={
0,1,0,-1,0,1,-1,1,-1};
void bfs(int x,int y)
{
int h=0,t=1;
int f1=0,f2=0;
q[1].x=x;q[1].y=y;
while(h<t)
{
h++;
for(int i=1;i<=8;i++)
{
int xx=q[h].x+dx[i];
int yy=q[h].y+dy[i];
if(xx<1||xx>n||yy<1||yy>n) continue;
if(!v[xx][yy]&&a[xx][yy]==a[q[h].x][q[h].y])
{
v[xx][yy]=1;
t++;
q[t].x=xx;
q[t].y=yy;
}
if(a[xx][yy]>a[q[h].x][q[h].y]) f1=1;
if(a[xx][yy]<a[q[h].x][q[h].y]) f2=1;
}
}
if(f1&&!f2) sg++;
if(f2&&!f1) sf++;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(!v[i][j])
{
v[i][j]=1;
bfs(i,j);
cnt++;
}
}
}
if(cnt==1)
{
cout<<1<<' '<<1;
return 0;
}
else cout<<sf<<' '<<sg;
return 0;
}