嗨害嗨ac题解
2024-09-26 21:31:08
发布于:广东
6阅读
0回复
0点赞
/只因AC实在太美/
仅供参考请勿抄袭
#include<bits/stdc++.h>
using namespace std;
int n,m,a;
char mp[50][50];
bool vis[50][50];
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
struct node{int x,y,s;};
bool check(int x,int y){return x>=1 and x<=n and y>=1 and y<=m and mp[x][y]=='.' and !vis[x][y];}
int bfs(){
vis[1][1]=1;
queue<node>q;
q.push({1,1,1});
while(!q.empty()){
node f=q.front();
if(f.x==n and f.y==m)return a+1;
a=f.s;
q.pop();
for(int i=0;i<4;i++){
int nx=f.x+dx[i];
int ny=f.y+dy[i];
if(check(nx,ny)){
q.push({nx,ny,f.s+1});
vis[nx][ny]=1;
}
}
}
return a;
}
int main(){
cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> mp[i][j];
}
}
cout << bfs();
}
这里空空如也
有帮助,赞一个