#include<bits/stdc++.h>
using namespace std;
char a[100][100];
bool flag[100][100];
int n,m;
struct node{
int x,y,step;
};
queue<node>q;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
void dfs(){
node s= {1,1,1};
flag[1][1]=1;
q.push(s);
while(q.size()){
s = q.front();
q.pop();
if(s.x==n&&s.y==m){
cout<<s.step;
return;
}
for(int i=0;i<4;i++){
node t;
t.x=s.x+dx[i];
t.y=s.y+dy[i];
if(a[t.x][t.y]=='.'&&flag[t.x][t.y]==0){
flag[t.x][t.y]=1;
t.step=s.step+1;
q.push(t);
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
dfs();
return 0;
}
CSPOJCSPOJ1432: 【基础】走出迷宫的最少步数
最新推荐文章于 2024-09-15 11:40:06 发布