求助大佬,我觉得这段代码思路没问题,但是0分,样例也过不了(悬赏关注)

P2895 [USACO08FEB] Meteor Shower S

gghack_Nythix @ 2023-02-07 19:52:27

rt,没人回就再发了一个,之前的删了

#include <bits/stdc++.h>
#define int long long
#define maxx 350
using namespace std;
struct meteor{
    int x,y;
};
queue<meteor>q;
int ans[maxx][maxx],dh[maxx][maxx],dir[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};
signed main(){
    int m,ansss = 1145141919;
    memset(ans,-1,sizeof(ans));
    memset(dh,0x7f,sizeof(dh));
    cin >> m;
    for(int i = 1;i <= m;++i){
        int x,y,t;
        cin >> x >> y >> t;
#define MINN(x,y,t) if(x >= 0 && y>= 0)dh[x][y] = min(dh[x][y],t);
        MINN(x,y,t);
        for(int y = 0;y < 4;++y){
            MINN(x+dir[y][0],y+dir[y][1],t);
        }
    }
    q.push(meteor{0,0});
    ans[0][0] = 0;
    while(!q.empty()){
        meteor reload = q.front();
        int rx = reload.x,ry = reload.y;
        q.pop();
        for(int i = 0;i < 4;++i){
            int fx = rx + dir[i][0],fy = ry + dir[i][1];
            if(fx < 0 || fy < 0 || fx > 305 || fy > 305|| ans[fx][fy] != -1 || ans[rx][ry] + 1 >= dh[fx][fy]){
                continue;
            }
            ans[fx][fy] = ans[rx][ry] + 1;
            q.push(meteor{fx,fy});
        }

    }
    for(int i = 0;i <= 305;++i){
        for(int j = 0;j <= 305;++j){
            if(dh[i][j] > 1000 && ans[i][j] != -1){
                ansss = min(ansss,ans[i][j]);
            }
        }
    }
    if(ansss == 1145141919){
        cout << -1;
    }
    else{
        cout << ansss;
    }
    return 0;
}

by Yujinhe469 @ 2023-02-07 22:06:47

for(int i = 1;i <= m;++i){
        int x,y,t;
        cin >> x >> y >> t;
#define MINN(x,y,t) if(x >= 0 && y>= 0)dh[x][y] = min(dh[x][y],t);
        MINN(x,y,t);
        for(int y = 0;y < 4;++y){
            MINN(x+dir[y][0],y+dir[y][1],t);
        }
    }

其中int y和cin>>y重复变量名了,导致循环内部的y意义其实是int y而不是cin>>y


by gghack_Nythix @ 2023-02-09 12:41:21

@Yujinhe469

#include <bits/stdc++.h>
#define int long long
#define maxx 350
using namespace std;
struct meteor{
    int x,y;
};
queue<meteor>q;
int ans[maxx][maxx],dh[maxx][maxx],dir[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};
signed main(){
    int m,ansss = 1145141919;
    memset(ans,-1,sizeof(ans));
    memset(dh,0x7f,sizeof(dh));
    cin >> m;
    for(int i = 1;i <= m;++i){
        int x,y,t;
        cin >> x >> y >> t;
#define MINN(x,z//这里重复了吗?,t) if(x >= 0 && z>= 0)dh[x][z] = min(dh[x][z],t);
        MINN(x,y,t);
        for(int y = 0;y < 4;++y){
            MINN(x+dir[y][0],y+dir[y][1],t);
        }
    }
    q.push(meteor{0,0});
    ans[0][0] = 0;
    while(!q.empty()){
        meteor reload = q.front();
        int rx = reload.x,ry = reload.y;
        q.pop();
        for(int i = 0;i < 4;++i){
            int fx = rx + dir[i][0],fy = ry + dir[i][1];
            if(fx < 0 || fy < 0 || fx > 305 || fy > 305|| ans[fx][fy] != -1 || ans[rx][ry] + 1 >= dh[fx][fy]){
                continue;
            }
            ans[fx][fy] = ans[rx][ry] + 1;
            q.push(meteor{fx,fy});
        }

    }
    for(int i = 0;i <= 305;++i){
        for(int j = 0;j <= 305;++j){
            if(dh[i][j] > 1000 && ans[i][j] != -1){
                ansss = min(ansss,ans[i][j]);
            }
        }
    }
    if(ansss == 1145141919){
        cout << -1;
    }
    else{
        cout << ansss;
    }
    return 0;
}

by Yujinhe469 @ 2023-02-09 19:36:56

@gghack_m3d 不是,是循环中的循环:

for(int y = 0;y < 4;++y){
            MINN(x+dir[y][0],y+dir[y][1],t);
        }

应把int y进行修改,变量名和输入的y重复了


by gghack_Nythix @ 2023-02-09 19:38:52

@Yujinhe469 ok,谢谢大佬


by gghack_Nythix @ 2023-02-09 19:39:57

@Yujinhe469 过了,此贴结


|