You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
// 通过了 20/22个数据
int n;
bool st[N][N];
int h[N][N];
/*
# 不同操作系统默认栈的大小
Linux8MBulimit -s
Windows2MB
# 原因分析
n=1000 n*n=1e3*1e3=1e6
sx,sy int 4byte,8 byte
bool has_higher,has_lower 1byte 2byte
1e6*10=1e7 byte = 1e7/1024 kb=9,765.625 kb = 9.53mb
has_higher,has_lower
1e6*8= 8e6/1024 kb=7,812.5kb = 7.6mb
AcWingGCCLinux8MBY~has_higher,
has_lowerbfsFlood Fill
# 写给AcWing
AcWing8MBCCFy
https://studyingfather.blog.luogu.org/noi-technical-faq
# 解决办法:
* dfsMLE
* dfs,bfs使3GB
*/
void dfs(int sx, int sy, bool &has_higher, bool &has_lower) {
st[sx][sy] = true;
for (int x = sx - 1; x <= sx + 1; x++) {
for (int y = sy - 1; y <= sy + 1; y++) {
if (x <= 0 || x > n || y <= 0 || y > n) continue;
if (h[sx][sy] != h[x][y]) { // 高度不相等
if (h[sx][sy] < h[x][y]) has_higher = true;
if (h[sx][sy] > h[x][y]) has_lower = true;
} else { // 高度相等
if (st[x][y]) continue;
st[x][y] = true;
dfs(x, y, has_higher, has_lower);
}
}
}
}
int vally, peak;
int main() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> h[i][j];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (!st[i][j]) {
bool has_higher = false, has_lower = false;
dfs(i, j, has_higher, has_lower);
if (has_higher && has_lower) continue;
if (has_higher) vally++;
if (has_lower) peak++;
}
}
}
// 对于不存在山峰+山谷的一马平地山峰山谷都输出1
if (peak == 0 && vally == 0) peak = 1, vally = 1;
printf("%d %d\n", peak, vally);
return 0;
}