Thông tin
include<bits/stdc++.h>
define ulli unsigned long long int
define ll long long
define f first
define se second
define endl "\n"
define maxn 1000006
using namespace std; const int MOD = 1e9+7; const int tnh = 1008;
ll dx[4] = {-1,0,0,1}; ll dy[4] = {0,1,-1,0}; ll n,m; char a[1008][1008]; pair<ll,ll> na,nb; bool found = false; map<pair<ll,ll>,ll> f; void bfs(ll x,ll y) { queue<pair<ll,ll> >q; while(!q.empty()) q.pop(); q.push({x,y}); a[x][y] = '#'; while(!q.empty() || found == false) { pair<ll,ll> top = q.front(); cout<<top.f<<" "<<top.se<<endl; q.pop(); for(ll i = 0;i < 4;i++) { ll u = top.f + dx[i]; ll v = top.se + dy[i]; pair<ll,ll> np = {u,v}; if(1 > u || u > n || 1 > v || v > m || a[u][v] == '#') continue; if(np == nb) { f[nb] = f[top]; found = true; break; } f[np] = f[top] + 1; a[u][v] = '#'; q.push({u,v}); } } } int main() { //freopen("DEMPHONG.INP","r",stdin); //freopen("DEMPHONG.OUT","w",stdout); ios<em>base::syncwith_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n>>m; for(ll i = 1;i <= n;i++) { for(ll j = 1;j <= m;j++) { cin>>a[i][j]; if(a[i][j] == 'A') na = {i,j}; if(a[i][j] == 'B') nb = {i,j}; } } f[na] = 1; bfs(na.f,na.se); cout<<f[nb];
return 0;
}