Peaks and valleys Ridges and Valleys

Title Description

Thinking

This question is beginning to see, is not very will, who would like to issue statistics and bfs together, there is no state beginning, not the end of the state, even a title like the shortest word did not appear.
Then Statistical Well, the title says the squares are the same height, put the point read through and around this point at the same height on the team, the same height as the points are marked marked browsing. See the process, both smaller than it is, there are also bigger than it, then this is not the height of the point we want to count the answers, only smaller than it, or only larger than it, then what we want statistics of. Two marks may be provided, see the marking process it is smaller than there is larger than it.
Then there is only one highly of time, since the two marks are not met, then all add 1.
Then it is this topic feeling dfs and bfs can do, as if there is a way flooding method can also be resolved.
Then there was complain about bfs routine, compared to simple dfs, dfs easy to write, but how each layer represents the state more difficult, coupled with the pruning and the like, qwq ······

Code

#include <cstdio>
#include <cstring>
#include <queue>

int n;
int mp[1005][1005], vis[1005][1005], obs[1005][1005];
int dirx[] = {0, -1, -1, -1, 0, 1, 1, 1, 0};
int diry[] = {0, -1,  0,  1, 1, 1, 0,-1,-1};
struct Node {
    int x, y;
} tmp, cur;
std::queue<Node> q;
int lows, highs;
inline int read() {
    int s = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') ch = getchar();
    while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    return s * f;
}

bool valid(int x, int y) {
    if (x < 1 || x > n) return false;
    if (y < 1 || y > n) return false;
    return true;
}

void bfs(int x, int y) {
    while (!q.empty()) q.pop();
    tmp.x = x, tmp.y = y;
    q.push(tmp);
    vis[x][y] = 1;
    int z = mp[x][y];
    bool low = false, high = false;
    while (!q.empty()) {
        cur = q.front();
        q.pop();
        for (int i = 1; i <= 8; ++i) {
            tmp.x = cur.x + dirx[i];
            tmp.y = cur.y + diry[i];
            if (valid(tmp.x, tmp.y)) {
                int tz = mp[tmp.x][tmp.y];
                if (tz < z) low = true;
                if (tz > z) high = true;
                if (tz == z && !vis[tmp.x][tmp.y]) {
                    q.push(tmp);
                    vis[tmp.x][tmp.y] = 1;
                }
            }
        }
    }
    if (!low) lows++;
    if (!high) highs++;
}

int main() {
    n = read();
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            mp[i][j] = read();
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (!vis[i][j]) bfs(i, j);
        }
    }
    printf("%d %d", highs, lows);

    return 0;
}

Guess you like

Origin www.cnblogs.com/liuzz-20180701/p/11610243.html