O1480 【基础】走出迷宫的最少步数BFS版

#include <bits/stdc++.h>
using namespace std;
char g[505][505];
int r,c;
struct stu{int x,y,step;};
int dx[]={0,0,0,1,-1},dy[]={0,1,-1,0,0};

void bfs(){
	queue<stu> q;
	q.push((stu){1,1,1});
	g[1][1]='#';
	while(!q.empty()){
		stu f=q.front();
		q.pop();
		if(f.x==r && f.y==c){
			cout<<f.step;return;
		}
		for(int i=1;i<=4;i++){
			int nx=f.x+dx[i],ny=f.y+dy[i];
			if(g[nx][ny]=='.'){
				g[nx][ny]='#';
				q.push((stu){nx,ny,f.step+1});
			}
		}
	}
	cout<<-1<<endl;
}
int main() {
	memset(g,'#',sizeof(g));
	cin>>r>>c;
	for(int i=1;i<=r;i++)
		for(int j=1;j<=c;j++)cin>>g[i][j];	
	bfs();
	return 0;
}