题解:P13429 [GCJ 2009 Qualification] Watersheds

· · 题解

题解:P13429 [GCJ 2009 Qualification] Watersheds

前言

下文的【注意事项】和【代码】均采用 C++14 (GCC 9) 的标准。

洛谷题目

思路

水题,数学方法秒解,连题解也没什么好说的……

解法

显然的,对于一个水位 (x, y),第 i 行 j 列的格子高度为 a_{i, j},谁的下一个流向坐标应该是:

\min(a_{i + 1, j}, a_{i, j - 1}, a_{i, j + 1}, a_{i - 1, j})

且 1 \le i \le h,1 \le j \le w。

于是,可以使用 DFS 解决。

其实我们可以根据题目里的汇点来解决,每个点坐标最终的标记,其实是要反过来,顺着流向去找它所属于的汇点。

代码

无注释版

#include<bits/stdc++.h>
using namespace std;
typedef pair < int, int > pii;
int mp[110][110];
char lb[110][110];
int vis[110][110];
int T, h, w;
int dx[] {-1, 0, 0, 1};
int dy[] {0, -1, 1, 0};
pii dfs(int x, int y) {
    int nowh = mp[x][y];
    pii result = {x, y};
    for (int i = 0; i < 4; i++) {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (xx >= 1 && xx <= h && yy >= 1 && yy <= w) {
            if (mp[xx][yy] < nowh) {
                nowh = mp[xx][yy];
                result = {xx, yy};
            }
        }
    }
    return result;
}
signed main() {
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> T;
    for (int t = 1; t <= T; ++ t) {
        cin >> h >> w;
        for (int i = 1; i <= h; ++ i) {
            for (int j = 1; j <= w; ++ j) {
                cin >> mp[i][j];
                vis[i][j] = -1;
            }
        }
        char nxtlb = 'a';
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; ++ j) {
                pii now = {i, j};
                pii nxt = dfs(i, j);
                while (now != nxt) {
                    now = nxt;
                    nxt = dfs(now.first, now.second);
                }
                if (vis[now.first][now.second] == -1) {
                    vis[now.first][now.second] = nxtlb - 'a';
                    ++ nxtlb;
                }
                lb[i][j] = 'a' + vis[now.first][now.second];
            }
        }
        cout << "Case #" << t << ':' << endl;
        for (int i = 1; i <= h; ++ i) {
            for (int j = 1; j <= w; ++ j) {
                cout << lb[i][j];
                if (j < w) cout << ' ';
            }
            cout << endl;
        }
    }
    return 0;
}

有注释版

#include<bits/stdc++.h>
using namespace std;
typedef pair < int, int > pii;
int mp[110][110];
char lb[110][110];
int vis[110][110]; // 记录每个汇点的标签编号
int T, h, w;
// 方向:北、西、东、南(题目要求的顺序)
int dx[] {-1, 0, 0, 1};
int dy[] {0, -1, 1, 0};
// 找到当前格子的流向
pii dfs(int x, int y) {
    int nowh = mp[x][y];
    pii result = {x, y}; // 默认是自己(汇点)
    for (int i = 0; i < 4; i++) {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (xx >= 1 && xx <= h && yy >= 1 && yy <= w) {
            if (mp[xx][yy] < nowh) {
                nowh = mp[xx][yy];
                result = {xx, yy};
            }
        }
    }
    return result;
}
signed main() {
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> T;
    for (int t = 1; t <= T; ++ t) {
        // 输入、初始化
        cin >> h >> w;
        for (int i = 1; i <= h; ++ i) {
            for (int j = 1; j <= w; ++ j) {
                cin >> mp[i][j];
                vis[i][j] = -1;
            }
        }
        char nxtlb = 'a';
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                // 找到最终的汇点
                pii now = {i, j};
                pii nxt = dfs(i, j);
                // 沿着流向找到汇点
                while (now != nxt) {
                    now = nxt;
                    nxt = dfs(now.first, now.second);
                }
                if (vis[now.first][now.second] == -1) {
                    vis[now.first][now.second] = nxtlb - 'a';
                    ++ nxtlb;
                }
                // 设置当前格子的标签
                lb[i][j] = 'a' + vis[now.first][now.second];
            }
        }
        // 输出
        cout << "Case #" << t << ':' << endl;
        for (int i = 1; i <= h; ++ i) {
            for (int j = 1; j <= w; ++ j) {
                cout << lb[i][j];
                if (j < w) cout << ' ';
            }
            cout << endl;
        }
    }
    return 0;
}