jzdywf @ 2019-10-04 16:25:58
#include<bits/stdc++.h>
using namespace std;
int n;
int ans=99999;
int sx,sy,ex,ey;
bool b[110][110];
char a[110][110];
int u[4]={0,0,1,-1};
int v[4]={1,-1,0,0};
void dfs(int x,int y,int cnt,int last){
if(cnt>=ans){
return;
}
if(x==ex&&y==ey){
ans=min(ans,cnt);
return;
}
for(int k=0;k<4;k++){
int ii=x+u[k];
int jj=y+v[k];
if(ii>=1&&ii<=n&&jj>=1&&jj<=n&&b[ii][jj]==0){
b[ii][jj]=1;
if(last==5){
dfs(ii,jj,cnt,k);
}
else{
if(last!=k){
dfs(ii,jj,cnt+1,k);
}
else{
dfs(ii,jj,cnt,k);
}
}
b[ii][jj]=0;
}
}
}
int main(){
scanf("%d",&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;
}
if(a[i][j]=='B'){
ex=i,ey=j;
}
if(a[i][j]=='x'){
b[i][j]=1;
}
}
}
b[1][1]=1;
dfs(sx,sy,0,5);
if(ans==99999){
cout<<-1;
return 0;
}
printf("%d",ans);
return 0;
}
by syf2008 @ 2020-08-22 11:12:41
@jzdywf 吸氧
by 变异哥斯拉 @ 2022-03-06 19:15:23
o2