穿越雷区(bfs)
```cpp
#include<bits/stdc++.h>
using namespace std;
int n,x,y;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int vis[111][111]={0};
char maze[111][111];
struct node{
int x;
int y;
string fu;
// int step;
};
node q[1000];
bool judge(node temp,int tx,int ty)
{
if(vis[tx][ty]||maze[tx][ty]==maze[temp.x][temp.y])
return true;
else
return false;
}
bool bfs()
{
int front=0,end=0;
node s,t,next;
s.x=x;
s.y=y;
// s.step=0;
s.fu="A";
vis[x][y]=1;
q[end++]=s;
while(front<end)
{
t=q[front];
front++;
// cout<<t.x<<t.y<<maze[t.x][t.y]<<t.step<<endl;
for(int i=0;i<4;i++)
{
int dx=t.x+dir[i][0];
int dy=t.y+dir[i][1];
if(maze[dx][dy]=='B')
{
cout<<t.fu.length()<<endl;
return true;
}
if(dx<0||dx>=n||dy<0||dy>=n)continue;
if(judge(t,dx,dy))continue;
vis[dx][dy]=1;
next.x=dx;
next.y=dy;
// next.step=t.step++;
next.fu=t.fu+maze[dx][dy];
q[end++]=next;
}
}
return false;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>maze[i][j];
if(maze[i][j]=='A')
{
x=i;
y=j;
}
}
}
if(!bfs())cout<<-1<<endl;
return 0;
}