1 条题解

  • 0
    @ 2025-4-26 15:12:18

    广度优先搜索

    #include<bits/stdc++.h>
    using namespace std;
    int n,k;
    struct node{
        int x,y;
        int time;
        int size;
    };
    char a[305][305];
    bool vis[305][305];
    int dir[6][2]={1,0,-1,0,0,1,0,-1};
    int s=2;
    bool judge(int x,int y,int size){
        if(vis[x][y]){
            return false;
        }
        for(int i=-size;i<=size;i++){
            for(int j=-size;j<=size;j++){
                int xx=x+i;
                int yy=y+j;
                if(xx<1||xx>n||yy<1||yy>n||a[xx][yy]=='*'){
                    return false;
                }
            }
        }
        return true;
    }
    int nowsize(int x){
        if(x<k){
            return 2;
        }
        else if(x<2*k){
            return 1;
        }
        else{
            return 0;
        }
    }
    bool flag1=0,flag2=0;
    void bfs(){
        queue<node>q;
        q.push({3,3,0,2});
        vis[3][3]=1;
        while(!q.empty()){
            node u=q.front();
            q.pop();
            if(u.x==n-2&&u.y==n-2){
                cout<<u.time<<endl;
                return;
            }
            if(u.size!=0){
                q.push({u.x,u.y,u.time+1,nowsize(u.time+1)});
            }
            for(int i=0;i<4;i++){
                int bx=u.x+dir[i][0];
                int by=u.y+dir[i][1];
                if(judge(bx,by,u.size)){
                    q.push({bx,by,u.time+1,nowsize(u.time+1)});
                    vis[bx][by]=1;
                }
            }
        }
    }
    int main(){
        cin>>n>>k;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cin>>a[i][j];
            }
        }
        bfs();
        return 0;   
    }
    /*++++++ 
      ++++++ 
      ++++++
      ++++++
      ++++++
      ++++++ 
    */
    

    信息

    ID
    12678
    时间
    1000ms
    内存
    128MiB
    难度
    4
    标签
    递交数
    5
    已通过
    1
    上传者