【ACWing】1106. Peaks and valleys

Subject address:

https://www.acwing.com/problem/content/1108/

FGD kids especially like to climb mountains, he is studying the peaks and valleys while climbing. In order to have a plan for the journey, he wanted to know the number of peaks and valleys. Given a map, which is the area FGD wants to travel, the map is divided into n × nn × nn×n grid, each grid(i, j) (i, j)(i,j ) Altitudew (i, j) w (i, j)w(i,j ) is given. If two lattices have common vertices, then they are adjacent lattices, such as(i, j) (i,j)(i,j ) With a reciprocal lattice(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) (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)(i1,j1),(i1,j),(i1,j+1),(i,j1),(i,j+1),(i+1,j1),(i+1,j),(i+1,j+1 ) . We define a set of gridsSSS is a mountain (valley) if and only if:SSAll grids of S have the same height. SSAll the lattices of S are connected. Forsss belongs toSSS , andsss adjacent tos's's does not belong toSSS,都有 w s > w s ′ w_s>w_{s'} ws>ws(Mountain peak), or ws <ws ′ w_s<w_{s'}ws<ws(valley). If there is no adjacent area around, it will be treated as a mountain and a valley at the same time. Your task is to find the number of peaks and valleys for a given map. If all the grids have the same height, then the entire map is a mountain and a valley.

Input format: the
first line contains a positive integer nnn indicates the size of the map. Nextn × nn×nn×A matrix of n , representing the heightwwof each grid on the mapw

Output format:
one line, containing two integers, indicating the number of peaks and valleys.

Data range:
1 ≤ n ≤ 1000 1≤n≤10001n1000
0 ≤ w ≤ 1 0 9 0≤w≤10^9 0w109

The idea is BFS. Start BFS from each unvisited point, and record whether the boundary of the connected block with the same number of points is larger or smaller than it. If there is no larger or smaller number than it, then it is a mountain; It is small, then it is a valley. code show as below:

#include <iostream>
#include <queue>
using namespace std;

const int N = 1010;
int h[N][N];
int n;
queue<pair<int, int> > q;
bool st[N][N];

// 两个bool要传引用,修改其值后以供check当前连通块是否是山峰,是否是山谷
void bfs(int x, int y, bool& has_higher, bool& has_lower) {
    
    
    st[x][y] = true;
    q.push({
    
    x, y});
    while (!q.empty()) {
    
    
        auto t = q.front();
        q.pop();
        x = t.first, y = t.second;
        for (int dx = -1; dx <= 1; dx++)
            for (int dy = -1; dy <= 1; dy++) {
    
    
                if (dx == 0 && dy == 0) continue;

                int nx = x + dx, ny = y + dy;
                if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
                if (h[nx][ny] != h[x][y]) {
    
    
                    if (h[nx][ny] > h[x][y]) has_higher = true;
                    else has_lower = true;
                } else if (!st[nx][ny]) {
    
    
                    st[nx][ny] = true;
                    q.push({
    
    nx, ny});
                }
            }
    }
}

int main() {
    
    
    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> 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_higher) peak++;
                if (!has_lower) valley++;
            }

    cout << peak << ' ' << valley << endl;

    return 0;
}

Time and space complexity O (n 2) O(n^2)O ( n2)

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/114559582
Recommended