UVALive - 3977 Summits (BFS染色)

题目大意:坑爹的题目。题意那么难理解。


讲的就是,假设该点是山顶的话(高度为h)。那么以该点为中心,往外辐射。走高度大于h-d的点,到达不了还有一个比它高的点
这就提示了,高度要从大到小排序,依次以高的点(假设高度为h)为核心辐射,假设碰上高度小于等于h-d的。表示此路不通了。就在该处停止
反之。假设碰上高度大于h-d的,且没有被染色过的。那么就将其染色
假设碰上高度大于h-d的,且被染色的话,就表明能够走到还有一个更高点了,那么此点就不是山顶了
假设中途碰到了和该点一样高的,那么山顶的数量就加1

这里出现了失误,写代码的时候把M写成N了(定义数组的时候,本来是hight[M][M]的,结果写成了hight[N][N],这里N = 250010。M = 510),结果一直CE,非常郁闷,可是电脑上的编译却过了
后来我把结构体的定义拉到了后面,在电脑上编译就出错了。空间不足
这就有点郁闷了。结构体的定义顺序还影响到了编译?
假设有大神知道的。还望告知一下

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 250010
#define M 510

struct Node {
    int x, y, h;
}node[N];

int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int Ans, H, W, D;
int color[M][M];
int hight[M][M];

bool cmp(const Node &a, const Node &b) {
    return a.h > b.h;
}

struct tmp_node{
    int x, y;
    tmp_node() {}
    tmp_node(int x, int y): x(x), y(y) {}
};


void bfs(int u) {
    bool flag = true;
    int tot = 1, lim = node[u].h - D, high = node[u].h;

    queue<tmp_node> Q;
    Q.push(tmp_node(node[u].x, node[u].y));
    color[node[u].x][node[u].y] = high;

    while (!Q.empty()) {
        tmp_node t = Q.front();
        Q.pop();
        for (int i = 0; i < 4; i++) {
            int x = t.x + dir[i][0];
            int y = t.y + dir[i][1];

            if (x >= H || x < 0 || y >= W || y < 0)
                continue;

            if (hight[x][y] <= lim)
                continue;
            if (color[x][y] != -1) {
                if (color[x][y] != high)
                    flag = false;
                continue;
            }

            if (color[x][y] == -1) {
                color[x][y] = high;
                Q.push(tmp_node(x,y));
            }
            if (hight[x][y] == high)
                tot++;
        }
    }
    if (flag)
        Ans += tot;
}

void solve() {
    sort(node, node + H * W, cmp);
    memset(color, -1, sizeof(color));
    Ans = 0;

    for (int i = 0; i < H * W; i++)
        if (color[node[i].x][node[i].y] == -1)
            bfs(i);
    printf("%d\n", Ans);
}

int main() {
    int test;
    scanf("%d", &test);
    while (test--) {

        scanf("%d%d%d", &H, &W, &D);
        for (int i = 0; i < H; i++)
            for (int j = 0; j < W; j++) {
                scanf("%d", &node[i * W + j].h);
                node[i * W + j].x = i;
                node[i * W + j].y = j;
                hight[i][j] = node[i * W + j].h;
            }
        solve();
    }
    return 0;
}
posted @ 2018-01-14 12:01  zhchoutai  阅读(145)  评论(0编辑  收藏  举报