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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef tuple<ll, ll, ll> ti;

#define REP(a, b, c) for (ll a = b; a < (c); a++)
#define PER(a, b, c) for (ll a = b; a >= (c); a--)

inline ll ii() {
  ll x;
  cin >> x;
  return x;
}
inline string is() {
  string x;
  cin >> x;
  return x;
}
inline ld id() {
  ld x;
  cin >> x;
  return x;
}
inline void oi(ll x) { cout << x; }
inline void od(ld x) { cout << fixed << setprecision(10) << x; }
inline void os(string x) { cout << x; }
inline void oe() { cout << endl; }
inline void oie(ll x) {
  oi(x);
  oe();
}
inline void ode(ld x) {
  od(x);
  oe();
}
inline void ose(string x) {
  os(x);
  oe();
}
inline void maxin(ll &a, ll b) { a = max(a, b); }

ll m[1234][1234];
ll t[1234][1234];

int main() {
  ll H = ii(), W = ii();
  ll CH = ii() - 1, CW = ii() - 1;
  ll DH = ii() - 1, DW = ii() - 1;
  REP(y, 0, H) {
    string S = is();
    REP(x, 0, W) {
      if (S[x] == '#') {
        m[x][y] = 0;
      } else {
        m[x][y] = 1;
      }
      t[x][y] = LLONG_MAX;
    }
  }

  if (CH == DH && CW == DW) {
    oie(0);
    return 0;
  }

  deque<pii> q;
  q.push_front(make_pair(CW, CH));
  t[CW][CH] = 0;
  while (!q.empty()) {
    ll cx = q.front().first;
    ll cy = q.front().second;
    q.pop_front();
    for (ll x = max((ll)0, cx - 2); x <= cx + 2; x++) {
      for (ll y = max((ll)0, cy - 2); y <= cy + 2; y++) {
        ll cc = t[cx][cy];
        bool prit;

        if (abs(cx - x) == 1 && abs(cy - y) == 0) {
          prit = true;
        } else if (abs(cx - x) == 0 && abs(cy - y) == 1) {
          prit = true;
        } else if (cx == x && cy == y) {
          continue;
        } else {
          prit = false;
          cc++;
        }

        if (m[x][y] == 1) {
          if (cc < t[x][y]) {
            t[x][y] = cc;
            if (prit) {
              q.push_front(make_pair(x, y));
            } else {
              q.push_back(make_pair(x, y));
            }
          }
        }
      }
    }
  }

  if (t[DW][DH] < LLONG_MAX) {
    oie(t[DW][DH]);
    return 0;
  }

  oie(-1);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef tuple<ll, ll, ll> ti;

#define REP(a, b, c) for (ll a = b; a < (c); a++)
#define PER(a, b, c) for (ll a = b; a >= (c); a--)

inline ll ii() {
  ll x;
  cin >> x;
  return x;
}
inline string is() {
  string x;
  cin >> x;
  return x;
}
inline ld id() {
  ld x;
  cin >> x;
  return x;
}
inline void oi(ll x) { cout << x; }
inline void od(ld x) { cout << fixed << setprecision(10) << x; }
inline void os(string x) { cout << x; }
inline void oe() { cout << endl; }
inline void oie(ll x) {
  oi(x);
  oe();
}
inline void ode(ld x) {
  od(x);
  oe();
}
inline void ose(string x) {
  os(x);
  oe();
}
inline void maxin(ll &a, ll b) { a = max(a, b); }

ll m[1234][1234];
ll t[1234][1234];

int main() {
  ll H = ii(), W = ii();
  ll CH = ii() - 1, CW = ii() - 1;
  ll DH = ii() - 1, DW = ii() - 1;
  REP(y, 0, H) {
    string S = is();
    REP(x, 0, W) {
      if (S[x] == '#') {
        m[x][y] = 0;
      } else {
        m[x][y] = 1;
      }
      t[x][y] = LLONG_MAX;
    }
  }

  if (CH == DH && CW == DW) {
    oie(0);
    return 0;
  }

  queue<pii> q;
  q.push(make_pair(CW, CH));
  t[CW][CH] = 0;
  while (!q.empty()) {
    ll cx = q.front().first;
    ll cy = q.front().second;
    q.pop();
    for (ll x = max((ll)0, cx - 2); x <= cx + 2; x++) {
      for (ll y = max((ll)0, cy - 2); y <= cy + 2; y++) {
        ll cc = t[cx][cy];

        if (abs(cx - x) == 1 && abs(cy - y) == 0) {
        } else if (abs(cx - x) == 0 && abs(cy - y) == 1) {
        } else if (cx == x && cy == y) {
        } else {
          cc++;
        }

        if (m[x][y] == 1) {
          if (cc < t[x][y] && cc <= t[DW][DH]) {
            t[x][y] = cc;
            q.push(make_pair(x, y));
          }
        }
      }
    }
  }

  if (t[DW][DH] < LLONG_MAX) {
    oie(t[DW][DH]);
    return 0;
  }

  oie(-1);
  return 0;
}