#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
queue<PII> q;
const int N = 1100;
int g[N][N],st[N][N];
int n,peak,valley;
int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={-1,0,1,-1,1,-1,0,1};
void flood_fill(int x,int y,bool &is_higher,bool &is_lower)
{
st[x][y]=1;
q.push({x,y});
while(!q.empty())
{
auto t=q.front();
//st[t.x][t.y]=1 ==> TLE
q.pop();
for(int i=0;i<8;i++)
{
int vx=t.first+dx[i],vy=t.second+dy[i];
if(vx<1||vx>n||vy<1||vy>n)continue;
if(g[vx][vy]>g[t.first][t.second])is_higher=true;
if(g[vx][vy]<g[t.first][t.second])is_lower=true;
if(!st[vx][vy]&&g[vx][vy]==g[t.first][t.second])
{
q.push({vx,vy});
//不先将st置1会重复遍历一个点=>TLE
st[vx][vy]=1;
}
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
scanf("%d",&g[i][j]);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(!st[i][j])
{
bool is_higher=false,is_lower=false;
flood_fill(i,j,is_higher,is_lower);
if(!is_higher)peak++;
if(!is_lower)valley++;
}
}
}
cout<<peak<<" "<<valley<<endl;
return 0;
}
AcWing 1106. 山峰和山谷
最新推荐文章于 2024-08-08 15:12:59 发布