求调或hark了关注!!
查看原帖
求调或hark了关注!!
520544
Phrvth楼主2023/2/11 23:38

T3一开始写了 7070,然后发现 kk 的范围特别大,然后就发现只用飞一次就行,然后就 9090 TLE,然后就发现可以不用优先队列,改了下顺序,又 9090 WA。

谁能拯救我……

WA

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e3 + 7, MAXM = 9e6 + 7;

int n, m, k, H[MAXN][MAXN];

struct Point{int x, y; }K[MAXM];
map<pair<int, int>, int> M;

struct Node{
    int x, y, step; 
    bool operator < (const Node other)const {
        return step > other.step;
    }
};

int vis[MAXN][MAXN];

int dx[4] = {1, 0, -1, 0};
int dy[5] = {0, 1, 0, -1};

inline bool check(int x, int y) {return x >= 1 && x <= n && y >= 1 && y <= m; }

int K_flag = 0;

void Bfs() {
    queue<Node> Q;
    Q.push(Node{1, 1, 0});
    vis[1][1] = 1;
    while (!Q.empty()) { 
        Node now = Q.front(); Q.pop();
        if (now.x == n && now.y == m) {cout << now.step << '\n'; return ;}
        for (int i = 0; i < 4; i ++) {
            int tx = now.x + dx[i], ty = now.y + dy[i];
            if (check(tx, ty) && !vis[tx][ty] && H[tx][ty]) 
                Q.push(Node{tx, ty, now.step + 1}), vis[tx][ty] = 1;
        }
        if (!K_flag && M.count(make_pair(now.x, now.y))) {
            for (int i = 1; i <= k; i ++) {
                if (vis[K[i].x][K[i].y]) continue;
                if (K[i].x == now.x && K[i].y == now.y) continue;
                if (H[K[i].x][K[i].y] == H[now.x][now.y]) Q.push(Node{K[i].x, K[i].y, now.step + 1});
                else Q.push(Node{K[i].x, K[i].y, now.step + 2});
                vis[K[i].x][K[i].y] = 1;
            }
            K_flag = 1;
        }
    }
    cout << -1 << '\n';
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m >> k;
    for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++) cin >> H[i][j];
    for (int i = 1; i <= k; i ++) {
        int x, y; cin >> x >> y;
        K[i] = Point{x, y};
        M[make_pair(x, y)] = 1;
    }
    Bfs();
    return 0;
}
2023/2/11 23:38
加载中...