1 条题解

  • 1
    @ 2024-7-26 16:38:07
    #include<iostream>
    #include<queue>
    using namespace std;
    const int N=155;
    const int M=155;
    //不是,哥们? 
    //        .,
    //    /TIIE)  1
    //   /(-      1
    //1 L(_,   .___1	
    //1       ITTI     
    //1T1     I  I    
    //1 1     I  I
    struct Node{
    	int x,y,step;
    };
    queue<Node> q;
    char a[N][M];int n,m;
    //00 01 0-1 10 11 1-1 -10 -11 -1-1 
    int dx[]={-1,-2,-2,-1,1,2,2,1};
    int dy[]={-2,-1,1,2,-2,-1,1,2};
    Node s;
    int tx,ty;
    bool vis[N][M];
    void bfs(int grass){
    	q.push(s);
    	vis[s.x][s.y]=1; 
    	Node last;
    	while(!q.empty()){
    		Node t=q.front();
    //		cout<<t.x<<" "<<t.y<<" "<<t.step<<'\n';
    		last=t;
    		q.pop();
    		if(t.x==tx&&t.y==ty){
    			cout<<t.step;
    		}
    		for(int i=0;i<8;i++){
    			int nx=t.x+dx[i];
    			int ny=t.y+dy[i];
    			if(vis[nx][ny])continue;
    			if(a[nx][ny]=='*')continue;
    			q.push({nx,ny,t.step+1});
    			vis[nx][ny]=1;
    		}
    	}
    	cout<<-1;
    	return ;
    }
    int main(){
    	cin>>n>>m;
    	s.step=0;
    	int cnt=0;
    	for(int i=0;i<m;i++){
    		for(int j=0;j<n;j++){
    			cin>>a[i][j];
    		}
    	}
    	bfs(cnt);
    	return 0;
    }
    
    • 1

    信息

    ID
    384
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    (无)
    递交数
    332
    已通过
    84
    上传者