Ultraviolet A 12130-summits (BFS)

Source: Internet
Author: User

Link to the question: Ultraviolet A 12130-Summits

Given an n? M graph, each position has a value. Given D, if a node value is G as the starting point, it cannot be moved to a position where the value is less than or equal to G? D node. Now we need to find all the peak points in the entire graph. The definition of the peak point is that it cannot be moved to a location that is larger than its own node value.

Solution: Sort each location by weight and move them one by one as the starting point. The value of the V [x] [Y] array can reach the maximum value of the X and Y nodes, if the starting point can be moved to a vertex greater than V [x] [Y], it indicates that the starting point is not the peak point.

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int maxn = 505;const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};struct point {    int x, y, val;    point (int x = 0, int y = 0, int val = 0) {        this->x = x;        this->y = y;        this->val = val;    }    bool operator < (const point& u) const {        return val > u.val;    }};int N, M, D, g[maxn][maxn], v[maxn][maxn];vector<point> vec;void init () {    vec.clear();    memset(v, 0, sizeof(v));    scanf("%d%d%d", &N, &M, &D);    for (int i = 1; i <= N; i++) {        for (int j = 1; j <= M; j++) {            scanf("%d", &g[i][j]);            vec.push_back(point(i, j, g[i][j]));        }    }    sort(vec.begin(), vec.end());}int bfs (int x, int y, int h) {    int ret = 1;    bool flag = true;    queue<point> que;    que.push(point(x, y));    while (!que.empty()) {        point u = que.front();        que.pop();        for (int i = 0; i < 4; i++) {            int p = u.x + dir[i][0];            int q = u.y + dir[i][1];            if (p <= 0 || p > N || q <= 0 || q > M)                continue;            if (h - g[p][q] >= D)                continue;            if (v[p][q] > h) {                flag = false;                continue;            }            if (v[p][q] == h)                continue;            if (g[p][q] == h)                ret++;            v[p][q] = h;            que.push(point(p, q));        }    }    //printf("%d %d %d %d\n", x, y, g[x][y], ret);    if (flag == false)        return 0;    return ret;}int solve () {    int ret = 0;    for (int i = 0; i < vec.size(); i++) {        int x = vec[i].x, y = vec[i].y;        if (v[x][y])            continue;        v[x][y] = g[x][y];        ret += bfs(x, y, g[x][y]);    }    return ret;}int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        init();        printf("%d\n", solve());    }    return 0;}

Ultraviolet A 12130-summits (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.