1 条题解

  • 0
    @ 2024-7-27 9:55:17
    #include<iostream>
    #include<queue>
    #include<cstring>
    using namespace std;
    struct point{
        int x,y;
    };
    queue<point> q;
    int d[30][30],x,y,t,sx,sy,tx,ty;
    char g[30][30];
    bool inq[30][30];
    const int xx[4]={-1,0,1,0};
    const int yy[4]={0,1,0,-1};
    bool check(int nx,int ny){
        if(nx<1||nx>x||ny<1||ny>y)return false;
        if(g[nx][ny]=='o')return false;
        return true;
    }
    void spfa(int sx,int sy){
        memset(d,0x7f,sizeof(d));
        d[sx][sy]=0;
        inq[sx][sy]=true;
        q.push((point){sx,sy});
        while(!q.empty()){
            point u=q.front();
            q.pop();
            inq[u.x][u.y]=false;
            for(int i=0;i<4;i++){
                int nx=u.x+xx[i];
                int ny=u.y+yy[i];
                if(check(nx,ny)){
                    int tmp=1;
                    if(g[u.x][u.y]=='.')tmp=1;
                    if(g[u.x][u.y]=='#')tmp=2;
                    if(d[u.x][u.y]+tmp<d[nx][ny]){
                        d[nx][ny]=d[u.x][u.y]+tmp;
                        if(!inq[nx][ny]){
                            q.push((point){nx,ny});
                            inq[nx][ny]=true;
                        }
                    }
                }
            }
        }
    }
     
    int main(){
    
        cin>>t>>y>>x;
        for(int i=1;i<=x;i++){
            for(int j=1;j<=y;j++){
                cin>>g[i][j];
                if(g[i][j]=='s'){
                    sx=i;
                    sy=j;
                }
                if(g[i][j]=='m'){
                    tx=i;
                    ty=j;
                }
            }
        }
        spfa(sx,sy);
        if(d[tx][ty]<t)cout<<d[tx][ty];
        else cout<<"55555";
    }
    
    • 1

    信息

    ID
    3053
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    48
    已通过
    5
    上传者