49分求助(玄关)

P2895 [USACO08FEB] Meteor Shower S

zhangzirui66 @ 2024-08-17 22:57:23

rt,看了坑点,调不出来了。

#include<bits/stdc++.h>
using namespace std;
queue<int> qx, qy;
int dis[505][505], c[505][505], stx, sty, ans = 2e9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
char mp[505][505];
void bfs(){
    qx.push(stx);
    qy.push(sty);
    dis[stx][sty] = 0;
    while(qx.size()){
        int x = qx.front(), y = qy.front();
        qx.pop(); qy.pop();
        for(int i = 0; i < 4; i ++){
            int tx = x + dx[i], ty = y + dy[i];
            if(tx >= 0 && tx <= 500 && ty >= 0 && ty <= 500 && dis[tx][ty] == 2e9 && mp[tx][ty] != '#'){
                qx.push(tx);
                qy.push(ty);
                dis[tx][ty] = min(dis[x][y] + 1, dis[tx][ty]);
            }
        }
    }
}
int main(){
    int n;
    cin >> n;
    for(int i = 0; i <= 500; i ++)
        for(int j = 0; j <= 500; j ++)
        mp[i][j] = '.', c[i][j] = 2e9, dis[i][j] = 2e9;
    for(int i = 1; i <= n; i ++){
        int x, y, t;
        cin >> x >> y >> t;
        c[x][y] = min(c[x][y], t);
        c[x][y + 1] = min(c[x][y + 1], t);
        c[x][y - 1] = min(c[x][y - 1], t);
        c[x + 1][y] = min(c[x + 1][y], t);
        c[x - 1][y] = min(c[x - 1][y], t);
    }
    stx = sty = 0;
    bfs();
    dis[stx][sty] = 0;
    for(int i = 0; i <= 500; i ++)
        for(int j = 0; j <= 500; j ++){
            if(c[i][j] != 2e9){
                if(dis[i][j] >= c[i][j]) mp[i][j] = '#';
            }
            dis[i][j] = 2e9;
        }
    bfs();
    dis[stx][sty] = 0;
    for(int i = 0; i <= 500; i ++){
        for(int j = 0; j <= 500; j ++){
            if(c[i][j] == 2e9 && dis[i][j] != 2e9 && mp[i][j] != '#') ans = min(ans, dis[i][j]);
        }
    }
    if(ans != 2e9) cout << ans;
    else cout << -1;
    return 0;
}

求救,说错误点悬1关,改好悬2关。


|