sansesantongshun @ 2024-02-28 22:24:48
双端队列广搜WA on #8
#include<bits/stdc++.h>
using namespace std;
int n,sx,sy,ex,ey,x,y,z,b[105][105][4],xx,yy,zz,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
char a[105][105];
struct node
{
int x,y,f;
};
deque<node> d;
int main()
{
memset(b,-1,sizeof(b));
cin>>n;
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
{
cin>>a[i][j];
if (a[i][j]=='A')
{
sx=i;
sy=j;
}
else if (a[i][j]=='B')
{
ex=i;
ey=j;
}
}
for (int i=0;i<4;++i)
{
b[sx][sy][i]=0;
d.push_back((node){sx,sy,i});
}
while (!d.empty())
{
x=d.front().x;
y=d.front().y;
z=d.front().f;
d.pop_front();
xx=x+dx[z];
yy=y+dy[z];
if (0<xx && xx<=n && 0<yy && yy<=n && a[xx][yy]!='x' && b[xx][yy][z]==-1)
{
b[xx][yy][z]=b[x][y][z];
if (xx==ex && yy==ey)
{
cout<<b[xx][yy][z];
return 0;
}
d.push_front((node){xx,yy,z});
}
for (int i=1;i<=3;i+=2)
{
zz=(z+i)%4;
if (b[x][y][zz]==-1)
{
b[x][y][zz]=b[x][y][z]+1;
d.push_back((node){x,y,zz});
}
}
}
cout<<-1;
}