题解
2024-01-07 11:44:52
发布于:浙江
174阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int n,m;
int dxy[][2]={0,1,1,0,-1,0,0,-1};
struct dian{
int x,y,c;
}t,s;
queue<dian> q;
char a[45][45];
bool v[50][50];
void bfs(int x,int y)
{
t.x=x;
t.y=y;
t.c=1;
q.push(t);
while(!q.empty()){
t=q.front();
q.pop();
if(t.x==n&&t.y==m){
cout<<t.c<<endl;
return ;
}
for(int i=0;i<4;i++){
int xx=t.x+dxy[i][0];
int yy=t.y+dxy[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&v[xx][yy]==0&&a[xx][yy]!='#'){
s.x=xx;
s.y=yy;
s.c=t.c+1;
v[xx][yy]=1;
q.push(s);
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
v[1][1]=1;
bfs(1,1);
return 0;
}
全部评论 1
未免有点长了吧...可以在缩点
2024-01-21 来自 北京
0
有帮助,赞一个