最后一个点wa,输出-1求助

P2895 [USACO08FEB] Meteor Shower S

luanmenglei @ 2019-09-14 13:01:02

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int m;
int pic[510][510];
bool f[510][510];
struct P {
    P(int a, int b, int c) {
        x = a;
        y = b;
        t = c;
    }
    int x, y, t;
};
const int dx[4] = { 0, 0, 1, -1 };
const int dy[4] = { 1, -1, 0, 0 };
void bfs() {
    f[0][0] = true;
    queue<P> q;
    q.push(P(0, 0, 0));
    while (!q.empty()) {
        P p = q.front();
        q.pop();

        for (int i = 0; i < 4; i++) {
            int xx = p.x + dx[i];
            int yy = p.y + dy[i];
            if (xx >= 0 && yy >= 0 && !f[xx][yy] && (pic[xx][yy] > p.t + 1 || pic[xx][yy] == -1)) {
                f[xx][yy] = true;
                //              printf("%d %d %d\n",p.x,p.y,p.t);
                //              system("pause");
                q.push(P(xx, yy, p.t + 1));
                if (pic[xx][yy] == -1) {
                    printf("%d", p.t + 1);
                    return;
                }
            }
        }
    }
    printf("-1");
    return;
}
int main() {
    for (int i = 0; i <= 300; i++)
        for (int j = 0; j <= 300; j++) pic[i][j] = -1;
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
        int x, y, t;
        scanf("%d%d%d", &x, &y, &t);
        for (int i = 0; i < 4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (xx >= 0 && yy >= 0 && xx <= 305 && yy <= 305) {
                if (pic[xx][yy] == -1 || pic[xx][yy] > t)
                    pic[xx][yy] = t;
            }
        }
        if (pic[x][y] == -1 || pic[x][y] > t)
            pic[x][y] = t;
    }
    //  printf("%d",pic[0][0]);
    bfs();
    return 0;
}

rt,求助


by luanmenglei @ 2019-09-19 20:57:09

解决了


|