题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<queue>
using namespace std;
const int N = 1010;
int height[N][N];
bool st[N][N];
int n;
void bfs(int x, int y, bool &has_heigher, bool &has_lower)
{
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
queue<pair<int, int>> q;
q.push({x, y});
st[x][y] = true;
while(q.size())
{
auto t = q.front();
q.pop();
int x = t.first, y = t.second;
for (int i = 0; i < 8; i ++)
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= n) continue;
if (height[a][b] != height[x][y])
{
if (height[a][b] > height[x][y]) has_heigher = true;
else has_lower = true;
}
else if (!st[a][b])
{
q.push({a, b});
st[a][b] = true;
}
}
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
cin >> height[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_heigher = false, has_lower = false;
bfs(i, j, has_heigher, has_lower);
if (!has_heigher) peak ++;
if (!has_lower) valley ++;
}
cout << peak << " " << valley;
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla