求助大佬4个wa

P2895 [USACO08FEB] Meteor Shower S

cwh1769 @ 2021-02-14 20:45:37

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<queue>
using namespace std;
int M, flag = 0;
const int INF = 1e9;
int dist[311][311];
int map[311][311];
int xx[4] = { 0,0,1,-1 };
int yy[4] = { 1,-1,0,0 };
void chang(int x, int y, int t) {//对地图进行时间覆盖(取最小的时间)
    map[x][y] = t;
    if (x - 1 >= 0)
        map[x - 1][y] = min(map[x - 1][y], t);
    map[x + 1][y] = min(map[x + 1][y], t);
    if (y - 1 >= 0)
        map[x][y - 1] = min(map[x][y - 1], t);
    map[x][y + 1] = min(map[x][y + 1], t);
}
struct node {
    int x;
    int y;
    int step;
};
queue<node>v;
void BFS() {
    node start;
    start.x = 0; start.y = 0; start.step = 0;
    dist[0][0] = 1;
    v.push(start);
    while (!v.empty()) {
        node now;
        now = v.front();
        v.pop();
        if (map[now.x][now.y] == INF) {
            cout << now.step;
            flag = 1;
            break;
        }
        for (int i = 0; i < 4; i++) {
            node next;
            next.x = now.x + xx[i];
            next.y = now.y + yy[i];
            next.step = now.step + 1;
            if (next.x < 0 || next.y < 0 || next.step >= map[next.x][next.y] || dist[next.x][next.y] == 1)continue;
            dist[next.x][next.y] = 1;
            v.push(next);
        }
    }
}
int main() {
    cin >> M;
    for (int i = 0; i <= 310; i++) {
        for (int j = 0; j <= 310; j++) {
            map[i][j] = INF;
        }
    }
    for (int i = 0; i < M; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        //map[x][y] = z;
        chang(x, y, z);
    }
    BFS();
    if (flag == 0) {
        cout << "-1";
    }
    return 0;
}

by zhaojiangxu @ 2021-02-18 13:30:18

chang函数中map[x][y]也要判断


by MercuryFish @ 2021-07-30 20:44:56

@zhaojiangxu

感谢大佬,我和楼主错的一样,要不是你的提示我可能会一直看不出哪里错,你的解释帮我节省了很多时间。


|