题目
FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。
为了能够对旅程有一个安排,他想知道山峰和山谷的数量。
给定一个地图,为FGD想要旅行的区域,地图被分为 n×n 的网格,每个格子 (i,j) 的高度 w(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)。
我们定义一个格子的集合 S 为山峰(山谷)当且仅当:
S 的所有格子都有相同的高度。
S 的所有格子都连通。
对于 s 属于 S,与 s 相邻的 s′ 不属于 S,都有 ws>ws′(山峰),或者 ws<ws′(山谷)。
如果周围不存在相邻区域,则同时将其视为山峰和山谷。
你的任务是,对于给定的地图,求出山峰和山谷的数量,如果所有格子都有相同的高度,那么整个地图即是山峰,又是山谷。
题解
#include<iostream>
#define x first
#define y second
using namespace std;
const int N =1010;
typedef pair<int,int> PII;
int n,h[N][N];
PII q[N*N];
bool st[N][N];
void bfs(int i,int j,bool &has_higher,bool &has_lower){
int hh=0,tt=0;
q[0]={i,j};
st[i][j]=true;
while(hh<=tt){
PII t=q[hh++];
for(int a=t.x-1;a<=t.x+1;a++){
for(int b=t.y-1;b<=t.y+1;b++){
if(a==t.x&&b==t.y)continue;
if(a<0||a>=n||b<0||b>=n)continue;
if(h[a][b]!=h[t.x][t.y]){
if(h[a][b]>h[t.x][t.y])has_higher=true;
else has_lower=true;
}else if(!st[a][b]){
q[++tt]={a,b};
st[a][b]=true;
}
}
}
}
}
int main(){
cin>>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_lower)valley++;
if(!has_higher)peak++;
}
cout<<peak<<" "<<valley;
return 0;
}