chenkaiwen @ 2021-07-24 17:25:42
#include<bits/stdc++.h>
using namespace std;
struct as{
int x,y,fx;
}z[10010],q1,q2;
int t=0,w=0,s1,s2,n;
int z1,z2;
bool b[101][101];
int f[101][101];
char Map[101][101];
int fx1[4]={1,-1,0,0};
int fx2[4]={0,0,1,-1};
void push(int x,int y,int fx){
b[x][y]=1;
q1.x=x,q1.y=y,q1.fx=fx;
z[t++]=q1;
}
as top(){
return z[w++];
}
int Fx(int x1,int y1,int x2,int y2){//←1↓2→3↑4
if(x1<x2)return 1;
else if(x1>x2)return 3;
else if(y1<y2)return 4;
else return 2;
}
void In(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>Map[i][j];
if(Map[i][j]=='A')s1=i,s2=j;
if(Map[i][j]=='B')z1=i,z2=j;
}
}
}
void Work(){
memset(f,0x3f,sizeof(f));
push(s1,s2,0);
f[s1][s2]=0;
while(t-w>0){
q2=top();
int x=q2.x,y=q2.y,fx=q2.fx;
// cout<<x<<" "<<y<<" "<<f[x][y]<<endl;
for(int i=0;i<4;i++){
int u=x+fx1[i],v=y+fx2[i];
int fxt=Fx(x,y,u,v);
if(u>=1&&u<=n&&v>=1&&v<=n){
if(Map[u][v]!='x'&&!b[u][v]){
b[u][v]=1;
push(u,v,fxt);
if(fxt==fx){
if(f[u][v]>f[x][y])f[u][v]=f[x][y];
}else{
if(f[u][v]>f[x][y]+1)f[u][v]=f[x][y]+1;
}
}
}
}
}
cout<<f[z1][z2]-1<<endl;
}
int main(){
In();
Work();
return 0;
}
by chenkaiwen @ 2021-07-24 17:26:13
只有后面两个点过了