1106 山峰与山谷
作者:
jy9
,
2024-12-03 23:13:16
,
所有人可见
,
阅读 1
#include <iostream>
using namespace std;
const int N = 1e3+10;
int mp[N][N];
bool st[N][N];
int n;
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
bool has_tall, has_low;
int mountain, valley;
void dfs(int x, int y){
st[x][y] = 1;
for (int i = 0; i < 8; i ++ ){
int xx = x + dx[i];
int yy = y + dy[i];
if(xx < 1 || xx > n || yy < 1 || yy > n) continue;
if(mp[xx][yy] > mp[x][y]){
has_tall = 1;
continue;
}if(mp[xx][yy] < mp[x][y]){
has_low = 1;
continue;
}
if(st[xx][yy]) continue;
dfs(xx, yy);
}
}
int main(){
cin >> n;
for (int i = 1; i <= n; i ++ ){
for (int j = 1; j <= n; j ++ ) cin >> mp[i][j];
}
for (int i = 1; i <= n; i ++ ){
for (int j = 1; j <= n; j ++ ) {
if(st[i][j]) continue;
dfs(i, j);
if(has_tall && !has_low) valley ++;
else if(!has_tall && has_low) mountain ++;
else if(!has_tall & !has_low){
mountain ++;
valley ++;
}
has_tall = has_low = 0;
}
}
cout << mountain << ' ' << valley;
return 0;
}