T19372.逃离迷宫2 题解
2024-08-07 15:52:52
发布于:广东
#include<bits/stdc++.h>
#include<queue>
using namespace std;
int b,c;
struct dada{
int n,m;
}s[4]={{1,0},{0,1},{-1,0},{0,-1}};
struct dad{
int x,y,t;
};
queue<dad>q;
char a[45][45];
bool v[45][45];
int f(int x,int y){
q.push({x,y,0});
v[x][y]=1;
if(x==b&&y==c)return 0;
while(q.size()){
dad top=q.front();
q.pop();
if(top.x==b&&top.y==c)return top.t;
for(int i=0,cx=top.x,cy=top.y;i<4;i++){
int ax=cx+s[i].n,ay=cy+s[i].m;
if(ax>0&&ax<=b&&ay>0&&ay<=c&&a[ax][ay]=='.'&&!v[ax][ay]){
q.push({ax,ay,top.t+1});
v[ax][ay]=1;
}
}
}
return -1;
}
int main(){
scanf("%d%d",&b,&c);
for(int i=1;i<=b;i++){
for(int j=1;j<=c;j++){
cin>>a[i][j];
}
}
cout<<f(1,1);
return 0;
}
这里空空如也
有帮助,赞一个