1 条题解

  • 0
    @ 2024-3-16 20:44:09
    #include <bits/stdc++.h>
    using namespace std;
    int n,m;
    char a[30][30];
    int bx,by,ex,ey;
    int fx[5]={0,0,1,0,-1};
    int fy[5]={0,1,0,-1,0};
    struct Node{
    	int x,y,step;
    };
    queue<Node>q;
    int main(){
    	ios::sync_with_stdio(false);
    	cin.tie(0);cout.tie(0);
    	cin>>n>>m;
    	for (int i=1;i<=n;i++){
    		for (int j=1;j<=m;j++){
    			cin>>a[i][j];
    			if (a[i][j]=='@'){
    				bx=i;
    				by=j;
    			}else if (a[i][j]=='*'){
    				ex=i;
    				ey=j;
    			}
    		}
    	}
    	q.push({bx,by,0});
    	while (q.size()){
    		for (int i=1;i<=4;i++){
    			int tx=fx[i]+q.front().x;
    			int ty=fy[i]+q.front().y;
    			if (tx==ex&&ty==ey){
    				cout<<q.back().step;
    				return 0;
    			}
    			if (a[tx][ty]=='.'){
    				a[tx][ty]='#';
    				q.push({tx,ty,q.front().step+1});			
    			}
    		}
    		q.pop();
    	}
    	cout<<-1;
    	return 0;
    }
    
    • 1

    信息

    ID
    896
    时间
    1000ms
    内存
    128MiB
    难度
    4
    标签
    递交数
    32
    已通过
    17
    上传者