为啥50就爆内存了阿
import java.util.*;
class Main{
static int vis[][];
static char a[][];
static int n;
static int dx[]= {0,0,1,-1};
static int dy[]= {1,-1,0,0};
static int x2;
static int y2;
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
n=in.nextInt();
a=new char [n][n];
int x=0;
int y=0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
String s=in.next();
if(s.equals("A")) {
x=i;
y=j;
}
if(s.equals("B")) {
x2=i;
y2=j;
}
a[i][j]=s.charAt(0);
}
}
vis=new int [n][n];
bfs(x,y);
if(ans==Integer.MAX_VALUE) {
System.out.println(-1);
}else {
System.out.println(ans);
}
}
static int ans=Integer.MAX_VALUE;
static class node{
int x;
int y;
char t;
int dist;
public node(int x, int y, char t,int dist) {
this.x = x;
this.y = y;
this.t = t;
this.dist=dist;
}
}
static void bfs(int x,int y) {
Queue<node> q=new ArrayDeque<Main.node>();
q.add(new node(x,y,'A',0));
vis[0][0]=1;
while(!q.isEmpty()) {
node u=q.peek();
q.poll();
int x1=u.x;
int y1=u.y;
vis[x1][y1]=1;
if(x1==x2&&y1==y2) {
if(u.dist<ans) {
ans=u.dist;
return;
}
}
for(int i=0;i<dx.length;i++) {
int nx=x1+dx[i];
int ny=y1+dy[i];
if(nx>=0&&ny>=0&&nx<n&&ny<n&&vis[nx][ny]==0) {
char t=u.t;
if(t=='+') {
if(a[nx][ny]=='-'||a[nx][ny]=='B') {
q.add(new node(nx,ny,'-',u.dist+1));
}
}else if(t=='-') {
if(a[nx][ny]=='+'||a[nx][ny]=='B') {
q.add(new node(nx,ny,'+',u.dist+1));
}
}else if(t=='A'){
q.add(new node(nx,ny,a[nx][ny],u.dist+1));
}
}
}
}
}
}