wudidepangzi @ 2024-03-10 13:00:49
//跑了下结果也正确,仍然WA,一个都没过
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
typedef pair<int,int> PII;
queue<PII> p;
int x3,x2;
int y3,y2;
int n;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int dio[N][N];//mp存地图,dio存距离
char mp[N][N];
int bfs(int x,int y)
{
memset(dio,-1,sizeof dio);//初始化距离
p.push({x,y});//当前坐标入队
dio[x][y] = 0;
while (p.size())
{
PII t = p.front();//记录队首元素
p.pop();//出队
for (int i=0;i < 4;i++){
int a = t.first + dx[i];
int b = t.second + dy[i];
//这两行代表遍历四个方位
if (dio[a][b] > 0) continue;//证明被加过
if(a<1 || b<1 || a>n || b>n) continue; //边界
if (mp[a][b] != '0') continue;
dio[a][b] = dio[t.first][t.second] + 1;
//该把a,b坐标扔进去了
p.push({a,b});
if (a==x2&&b==y2)//到达地点
return dio[x2][y2];
}
}
return dio[x2][y2];
}
int main()
{
scanf("%d",&n);
getchar();
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
scanf("%c",&mp[i][j]);
}
if (i != n)
getchar();
}
scanf("%d %d %d %d",&x3,&y3,&x2,&y2);
int res = bfs(x3,y3);
printf("%d",res);
return 0;
}
by shadow_leader @ 2024-03-19 20:21:05
换种写法吧,用结构体(x,y,距离)这样处理应该没事了
by wudidepangzi @ 2024-03-31 08:19:23
@shadow_leader 嗯嗯,谢谢,用“ c”就好了,我现在才看见回复,谢谢啦。