dingyibo @ 2023-07-27 16:36:09
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#define MAX_N 1001
using namespace std;
int n;
int X1, Y1, X2, Y2;
int place[MAX_N][MAX_N];
int offset[4][2] = {{0, -1}, {1, 0}, {-1, 0}, {0, 1}};
bool visits[MAX_N][MAX_N];
struct Point
{
int x, y;
int time;
};
int BFS()
{
queue<Point> q;
Point p, t;
p.x = X1;
p.y = Y1;
p.time = 0;
visits[X1][Y1] = true;
q.push(p);
int x, y, tx, ty;
while(!q.empty())
{
p = q.front();
q.pop();
x = p.x;
y = p.y;
if(x == X2 && y == Y2)
return p.time;
for(int i=0;i<4;++i)
{
tx = x + offset[i][0];
ty = y + offset[i][1];
if(tx >= 1 && tx <= n && ty >= 1 && ty <= n && visits[tx][ty] == false && place[tx][ty] == 0)
{
visits[tx][ty] = true;
t.x = tx;
t.y = ty;
t.time = p.time+1;
q.push(t);
}
}
}
return -1;
}
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
int i, j;
cin>>n;
for(i=1;i<=n;++i)
for(j=1;j<=n;++j)
cin>>place[i][j];
cin>>X1>>Y1>>X2>>Y2;
cout<<BFS()<<endl;
return 0;
}
by AstaSunch_ @ 2023-07-27 16:42:53
@dingyibo 你 BFS()
里面没有输出
by lcy_123 @ 2023-07-27 17:03:07
@dingyibo 输入错了,原题中的地图输入中间没有空格,可以把存图的数组定义成char来解决
by lcy_123 @ 2023-07-27 17:04:25
@dingyibo 如果存图的数组用成了int就会少输入了很多,程序会认为还没有输入完毕,而非没有输出
by dingyibo @ 2023-07-27 21:04:02
@AstaTab 感谢大佬
by dingyibo @ 2023-07-27 21:04:22
@lcy12 感谢大佬