46pts求调
查看原帖
46pts求调
1263684
Elysialr楼主2024/10/24 20:33
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int moves[2][8]={{0,0,1,1,1,-1,-1,-1},{1,-1,0,1,-1,0,1,-1}};
string s[1000];
int dis[1000][1000];
queue<pair<int,int>> q;
int n,m,a,b;
int main(){
    cin>>n>>m>>a>>b;
    for(int i=m;i>=1;i--){
        cin>>s[i];
        s[i]=' '+s[i];
    }
    s[a][b]='*';
    dis[a][b]=0;
    q.push(make_pair(a,b));
    while(!q.empty()){
        int x=q.front().first;
        int y=q.front().second;
        q.pop();
        for(int i=0;i<8;i++){
            int xx=x+moves[0][i];
            int yy=y+moves[1][i];
            if(xx>=1&&xx<=n)
            if(yy>=1&&yy<=m)
            if(s[xx][yy]=='.'){
                s[xx][yy]='*';
                dis[xx][yy]=dis[x][y]+1;
                q.push(make_pair(xx,yy));
            }
        }

        if(q.empty())
        cout<<dis[x][y];
    }
    return 0;
}
2024/10/24 20:33
加载中...