58分蒟蒻 求救

P2895 [USACO08FEB] Meteor Shower S

喝脉动割动脉 @ 2022-10-10 17:28:11

58分蒟蒻 求救

#include<bits/stdc++.h>
#define re register

using namespace std;
struct vec{
    int x,y,num;
};
int xx,yy,zz,nx,ny,nnum,a[310][310],jx[4]={-1,0,0,1},jy[4]={0,-1,1,0};
bool vis[310][310];

inline int bfs(int x,int y,int step){
    queue<vec> q;
    q.push(vec{x,y,step});
    vis[x][y]=1;
    while(!q.empty()){
        xx=q.front().x,yy=q.front().y,zz=q.front().num;
        q.pop();
        for(int i=0;i<4;++i){
            nx=xx+jx[i],ny=yy+jy[i],nnum=zz+1;
            if(nx>=0&&ny>=0){
                if(a[nx][ny]==0x3f3f3f3f){
                    return nnum;
                }
                if(!vis[nx][ny]&&nnum<a[nx][ny]){
                    q.push(vec{nx,ny,nnum});
                    vis[nx][ny]=1;
                }
            }
        }
    }
    return -1;
}

int main(){
    int n,x,y,ti;
    scanf("%d",&n);
    memset(a,0x3f,sizeof(a));
    for(re int i=1;i<=n;++i){
        scanf("%d%d%d",&x,&y,&ti);
        a[x][y]=min(a[x][y],ti);
        for(int j=0;j<4;++j){
            if(x+jx[j]>=0&&y+jx[j]>=0)
                a[x+jx[j]][y+jy[j]]=min(a[x+jx[j]][y+jy[j]],ti);
        }
    }
    printf("%d",bfs(0,0,0));
    return 0;
}

by Vergil_Sparda @ 2022-10-10 18:24:41

@喝脉动割动脉 42行的jy写成jx了

#include<bits/stdc++.h>
#define re register

using namespace std;
struct vec{
    int x,y,num;
};
int xx,yy,zz,nx,ny,nnum,a[310][310],jx[4]={-1,0,0,1},jy[4]={0,-1,1,0};
bool vis[310][310];

inline int bfs(int x,int y,int step){
    queue<vec> q;
    q.push(vec{x,y,step});
    vis[x][y]=1;
    while(!q.empty()){
        xx=q.front().x,yy=q.front().y,zz=q.front().num;
        q.pop();
        for(int i=0;i<4;++i){
            nx=xx+jx[i],ny=yy+jy[i],nnum=zz+1;
            if(nx>=0&&ny>=0){
                if(a[nx][ny]==0x3f3f3f3f){
                    return nnum;
                }
                if(!vis[nx][ny]&&nnum<a[nx][ny]){
                    q.push(vec{nx,ny,nnum});
                    vis[nx][ny]=1;
                }
            }
        }
    }
    return -1;
}

int main(){
    int n,x,y,ti;
    scanf("%d",&n);
    memset(a,0x3f,sizeof(a));
    for(re int i=1;i<=n;++i){
        scanf("%d%d%d",&x,&y,&ti);
        a[x][y]=min(a[x][y],ti);
        for(int j=0;j<4;++j){
            if(x+jx[j]>=0&&y+jy[j]>=0)
                a[x+jx[j]][y+jy[j]]=min(a[x+jx[j]][y+jy[j]],ti);
        }
    }
    printf("%d",bfs(0,0,0));
    return 0;
}

by Vergil_Sparda @ 2022-10-10 18:28:47

变量不要都定义在全局里啊


by 喝脉动割动脉 @ 2022-10-10 18:31:12

@qknb_QWQ A了,感谢祁大佬!


by 喝脉动割动脉 @ 2022-10-10 18:32:16

@qknb_QWQ 这道题没关系吧,没有重名


|