Sqrt49 @ 2024-05-22 16:02:22
样例能过,but一交全RE(QWQ)~
#include<bits/stdc++.h>
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int n, m, sx, sy, ex, ey;
bool vis[1005][1005], mp[1005][1005];
struct node
{
int x, y, step;
}now, p, pp;
int bfs(int x, int y)
{
queue<node> que;
node now = {x, y, -1};
vis[x][y] = 1;
que.push(now);
while(!que.empty())
{
p = que.front();
que.pop();
for(int i = 0; i < 4; i++)
{
int xx = p.x + dx[i];
int yy = p.y + dy[i];
while(xx >= 1 && xx <= n && yy >= 1 && yy <= n && !mp[xx][yy])
{
pp.x = xx;
pp.y = yy;
pp.step = p.step + 1;
que.push(pp);
mp[xx][yy] = 1;
if(pp.x == ex && pp.y == ey)
{
cout << pp.step;
return 0;
}
xx = xx + dx[i];
yy = yy + dy[i];
}
}
}
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
char v;
cin >> v;
if(v == '.')
{
mp[i][j] = 0;
}
else if(v == 'x')
{
mp[i][j] = 1;
}
else if(v == 'A')
{
mp[i][j] = 0;
sx = i;
sy = i;
}
else if(v == 'B')
{
mp[i][j] = 0;
ex = i;
ey = i;
}
}
}
bfs(sx, sy);
if(pp.step == 0)
{
cout << "-1";
}
return 0;
}
求调