一道板子题求助
查看原帖
一道板子题求助
405146
Ruan_ji楼主2023/11/25 15:33

一直没调出来啊 样例输出的是1000000005 和 0

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>

#define MAXN 505
#define inf 1000000005

using namespace std;

struct str {
    long long a, b;
    bool operator<(const str e)const {
        return b > e.b;
    }
};

int n, m, x;
int nn;
char s[MAXN][MAXN];
vector<str> G[MAXN];
priority_queue<str, vector<str>, less<str> > q;
long long dis[MAXN];
long long num[MAXN][MAXN];

void build() {
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (j - 1 >= 1) {
                if (s[i][j] == s[i][j - 1]) {
                    G[ num[i][j] ].push_back({num[i][j - 1], 1});
                }
                else {
                    G[ num[i][j] ].push_back({num[i][j - 1], 0});
                }
            }
            if (i - 1 >= 1) {
                if (s[i][j] == s[i - 1][j]) {
                    G[ num[i][j] ].push_back({num[i - 1][j], 1});
                }
                else {
                    G[ num[i][j] ].push_back({num[i - 1][j], 0});
                }
            }
            if (i + 1 <= m) {
                if (s[i][j] == s[i + 1][j]) {
                    G[ num[i][j] ].push_back({num[i + 1][j], 1});
                }
                else {
                    G[ num[i][j] ].push_back({num[i + 1][j], 0});
                }
            }
            if (j + 1 <= n) {
                if (s[i][j] == s[i][j + 1]) {
                    G[ num[i][j] ].push_back({num[i][j + 1], 1});
                }
                else {
                    G[ num[i][j] ].push_back({num[i][j + 1], 0});
                }
            }
        }
    }
}

void dij() {
    q.push({x, 0});
    for (int i = 1; i <= nn; ++i) {
        dis[i] = inf;
    }
    dis[x] = 0;
    while (!q.empty()) {
        str cnt = q.top();
        long long u = cnt.a;
        long long v = cnt.b;
        q.pop();

        if (v > dis[u]) continue;
        for (int i = 0; i < G[u].size(); ++i) {
            long long c1 = G[u][i].a;
            long long c2 = G[u][i].b;
            if (c2 + dis[u] < dis[c1]) {
                dis[c1] = c2 + dis[u];
                q.push({c1, dis[c1]});
            }
        }
    }
}

int main() {
    while (cin >> n >> m) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                cin >> s[i][j];
            }
        }
        int cnt = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                cnt += 1;
                num[i][j] = cnt;
            }
        }

        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;

        x = num[x1][y1];
        nn = n * m;

        build();
        dij();

        cout << dis[ num[x2][y2] ] << endl;

        for (int i = 1; i <= nn; ++i) {
            G[i].clear();
        }
        for (int i = 1; i <= nn; ++i) dis[i] = 0;
        n = 0; m = 0; nn = 0;
    }
    return 0;
}

2023/11/25 15:33
加载中...