没事
2025-07-30 18:46:26
发布于:河北
0阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int maxn=55;
int n,m;
char mp[maxn][maxn];
int vis[maxn][maxn];
int dx[]={-1,1,0,0};//方向数组
int dy[]={0,0,-1,1};//上下左右
int s,e,s1,e1;
struct node{
int x;
int y;
};
queue <node> q;
bool ok=false;
void bfs(){
if (ok==true)return ;
while (!q.empty()){
node tmp=q.front();
q.pop();
if (tmp.x==s1&&tmp.y==e1){
ok=true;
return;
}
for (int i=0;i<4;i++){
int tx=tmp.x+dx[i];
int ty=tmp.y+dy[i];
if (tx>=1&&tx<=n&&
ty>=1&&ty<=m&&
vis[tx][ty]==0&&
mp[tx][ty]=='.')
{
vis[tx][ty]=vis[tmp.x][tmp.y]+1;
q.push({tx,ty});
}
}
}
}
int main(){
freopen ("dawn.in","r",stdin);
freopen ("dawn.out","w",stdout);
cin >> n >> m;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin >> mp[i][j];//char类型输入
}
}
cin >> s >> e >> s1 >> e1;
vis[s][e]=1;//你所在位置标记为1
q.push({s,e});
bfs();
cout << vis[s1][e1]-1;
fclose (stdin);
fclose (stdout);
return 0;
}
这里空空如也
有帮助,赞一个