代码
#include<bits/stdc++.h>
using namespace std;
char a[51][51];
int r,c;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int tx,ty;
struct Node{int x,y,step;
};
int bfs(int x,int y){a[x][y]='#';queue<Node> q;q.push({x,y,1});while(!q.empty()){Node Current=q.front();q.pop();if(Current.x==r&&Current.y==c){return Current.step;}for(int i=0;i<4;++i){tx=Current.x+fx[i];ty=Current.y+fy[i];if(a[tx][ty]=='.'){a[tx][ty]='#';q.push({tx,ty,Current.step+1});}} }return -1;
}
int main(){cin>>r>>c;for(int i=1;i<=r;i++){for(int j=1;j<=c;j++){cin>>a[i][j];}}cout<<bfs(1,1);return 0;
}