// Author: yangwang2013 #include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define dbg(x) cerr << #x << ” = ” << (x) << “\n” using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; const int MAXN = 105; int n,sx,sy,vst[MAXN][MAXN],dis[MAXN][MAXN],ex,ey; int dx[4] = {1,0,-1,0},dy[4] = {0,-1,0,1}; char a[MAXN][MAXN];
void bfs(){ memset(dist,0x3f,sizeof dist); deque<tuple<int,int,int>> q; q.push_front(make_tuple(sx,sy,0)); q.push_front(make_tuple(sx,sy,1)); q.push_front(make_tuple(sx,sy,2)); q.push_front(make_tuple(sx,sy,3)); vst[sx][sy] = 1; dist[sx][sy] = 0; while (!q.empty()){ tuple<int,int,int> u; u = q.front(); q.pop(); int x = get<0>(u); int y = get<1>(u); int z = get<2>(u); for (int i = 0;i < 4;i++){ int nx = x + dx[i],ny = y + dy[i]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && !vst[nx][ny] && a[nx][ny] != ‘x’){ if (i == d){ if (dis[nx][ny] > dis[x][y] + 1){ dis[nx][ny] = dis[x][y] + 1; q.push(make_tuple(nx,ny,i)); } }else if (abs(i-d) == 1){ } } } } }
void solve() { 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; if (a[i][j] == ‘B’) ex = i,ey = j; } bfs(); if (dis[ex][ey] == INF) cout << -1 << endl; else cout << dis[ex][ey] << endl; }
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t—) {
solve();
}
return 0;
}