RingTouSou @ 2023-10-23 13:19:56
#include<bits/stdc++.h>
using namespace std;
const int N=1100;
int n;
int g[N][N];//存地图
int dist[N][N];//距离
queue<pair<int,int>> q;
int x5,y5,x2,y2;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
void bfs(int x,int y)
{
memset(dist,-1,sizeof(dist));
q.push({x,y});
dist[x][y]=0;
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int a=t.first+dx[i];
int b=t.second+dy[i];
if(a<1||a>n||b<1||b>n)
{
continue;
}
if(g[a][b]!=0) continue;
if(dist[a][b]>=0) continue;
q.push({a,b});
dist[a][b]=dist[t.first][t.second]+1;
if(a==x2&&b==y2)
{
return;
}
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>g[i][j];
}
}
cin>>x5>>y5>>x2>>y2;
bfs(x5,y5);
cout<<dist[x2][y2];
return 0;
}
by Miss_SGT @ 2023-10-23 14:12:10
输入g的时候,你的是int,它输入的没有间隔,会输多,改成char就好了
by RingTouSou @ 2023-10-23 16:30:53
@zhouchenqiao1 啊这,cin自带空格间隔啊....学到了!谢谢