Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vec = vector<ll>;
using vecp = vector<P>;
using mat = vector<vec>;
using matp = vector<vecp>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define all(v) v.begin(), v.end()

int main() {
  ll H, W, Ch, Dh, Cw, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  rep(i, H) { cin >> S.at(i); }
  vector<int> h, w;
  h = {1, 0,  -1, 0,  1,  1,  -1, -1, 2, 2, 2, 1,
       0, -1, -2, -2, -2, -2, -2, -1, 0, 1, 2, 2};
  w = {0, 1, 0, -1, 1, -1, 1,  -1, 0,  1,  2,  2,
       2, 2, 2, 1,  0, -1, -2, -2, -2, -2, -2, -1};
  queue<T> A, B;
  mat dp(H, vec(W, INF));
  dp.at(Ch).at(Cw) = 0;
  A.push(T(0, Ch, Cw));
  while (!A.empty() || !B.empty()) {
    ll x, y, z, k = 0;
    if (!A.empty()) {
      tie(x, y, z) = A.front();
      A.pop();
    } else {
      tie(x, y, z) = B.front();
      B.pop();
    }
    rep(i, 24) {
      if (y + h.at(i) >= 0 && y + h.at(i) <= H - 1 && z + w.at(i) >= 0 &&
          z + w.at(i) <= W - 1) {
        if (S.at(y + (ll)h.at(i)).at(z + (ll)w.at(i)) == '.') {
          if (i < 4) {
            if (dp.at(y + h.at(i)).at(z + w.at(i)) > dp.at(y).at(z)) {
              dp.at(y + h.at(i)).at(z + w.at(i)) = dp.at(y).at(z);
              A.push(T(dp.at(y + h.at(i)).at(z + w.at(i)), y + h.at(i),
                       z + w.at(i)));
              k++;
              if (Dh == y + h.at(i) && Dw == z + w.at(i)) {
                cout << dp.at(Dh).at(Dw) << endl;
                return 0;
              }
            }
          } else {
            if (dp.at(y + h.at(i)).at(z + w.at(i)) > dp.at(y).at(z) + 1) {
              dp.at(y + h.at(i)).at(z + w.at(i)) = dp.at(y).at(z) + 1;
              B.push(T(dp.at(y + h.at(i)).at(z + w.at(i)), y + h.at(i),
                       z + w.at(i)));
            }
          }
        }
      }
    }
  }
  if (dp.at(Dh).at(Dw) == INF) {
    cout << -1 << endl;
  } else {
    cout << dp.at(Dh).at(Dw) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vec = vector<ll>;
using vecp = vector<P>;
using mat = vector<vec>;
using matp = vector<vecp>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define all(v) v.begin(), v.end()

int main() {
  ll H, W, Ch, Dh, Cw, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  rep(i, H) { cin >> S.at(i); }
  vector<int> h, w;
  h = {1, 0,  -1, 0,  1,  1,  -1, -1, 2, 2, 2, 1,
       0, -1, -2, -2, -2, -2, -2, -1, 0, 1, 2, 2};
  w = {0, 1, 0, -1, 1, -1, 1,  -1, 0,  1,  2,  2,
       2, 2, 2, 1,  0, -1, -2, -2, -2, -2, -2, -1};
  queue<T> A;
  mat dp(H, vec(W, INF));
  dp.at(Ch).at(Cw) = 0;
  A.push(T(0, Ch, Cw));
  while (!A.empty()) {
    ll x, y, z, k = 0;
    tie(x, y, z) = A.front();
    A.pop();
    rep(i, 24) {
      if (y + h.at(i) >= 0 && y + h.at(i) <= H - 1 && z + w.at(i) >= 0 &&
          z + w.at(i) <= W - 1) {
        if (S.at(y + (ll)h.at(i)).at(z + (ll)w.at(i)) == '.') {
          if (i < 4) {
            if (dp.at(y + h.at(i)).at(z + w.at(i)) > dp.at(y).at(z)) {
              dp.at(y + h.at(i)).at(z + w.at(i)) = dp.at(y).at(z);
              A.push(T(dp.at(y + h.at(i)).at(z + w.at(i)), y + h.at(i),
                       z + w.at(i)));
              k++;
            }
          } else {
            if (dp.at(y + h.at(i)).at(z + w.at(i)) > dp.at(y).at(z) + 1) {
              dp.at(y + h.at(i)).at(z + w.at(i)) = dp.at(y).at(z) + 1;
              A.push(T(dp.at(y + h.at(i)).at(z + w.at(i)), y + h.at(i),
                       z + w.at(i)));
            }
          }
        }
      }
    }
  }
  if (dp.at(Dh).at(Dw) == INF) {
    cout << -1 << endl;
  } else {
    cout << dp.at(Dh).at(Dw) << endl;
  }
}