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

#ifndef _TEMPLATE_ROOT
#define _TEMPLATE_ROOT
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define repl(i, a, b) for (ll i = a; i < (b); ++i)
#define repd(i, a, b) for (int i = a; i >= (b); --i)
#define repdl(i, a, b) for (ll i = a; i >= (b); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
#endif
#ifndef _TEMPLATE_IO
#define _TEMPLATE_IO

template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void writef(T... tail) {
  write(tail...);
  cout << flush;
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  if (v.size()) {
    rep(i, 0, sz(v) - 1) out << v[i] << " ";
    out << v.back();
  }
  return out;
}
#endif
using namespace std;

struct Hist {
  int dist, i, j;
  const bool operator<(const Hist &h) const { return dist > h.dist; }
};

char mp[1005][1005];
int md[1005][1005];
priority_queue<Hist> q;
int h, w;
const int inf = 0x3f3f3f3f;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  read(h, w);
  rep(i, 0, 1005) rep(j, 0, 1005) md[i][j] = inf;
  int ch, cw, dh, dw;
  read(ch, cw, dh, dw);
  md[ch][cw] = 0;
  q.push(Hist{0, ch, cw});
  cin >> skipws;
  rep(i, 1, h + 1) rep(j, 1, w + 1) read(mp[i][j]);
  while (!q.empty()) {
    Hist hi = q.top();
    q.pop();
    int i = hi.i, j = hi.j;
    if (hi.dist > md[i][j])
      continue;
    rep(dx, -2, 3) rep(dy, -2, 3) {
      int ni = i + dx, nj = j + dy;
      int dd = (abs(dx) + abs(dy) == 1) ? 0 : 1;
      if (1 <= ni && ni <= h && 1 <= nj && nj <= w) {
        if (md[ni][nj] > md[i][j] + dd && mp[ni][nj] != '#') {
          md[ni][nj] = md[i][j] + dd;
          q.push(Hist{md[ni][nj], ni, nj});
        }
      }
    }
  }
  if (md[dh][dw] == inf)
    write(-1);
  else
    write(md[dh][dw]);
}


Error: TLE

Fixed submission:

#ifndef _TEMPLATE_ROOT
#define _TEMPLATE_ROOT
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define repl(i, a, b) for (ll i = a; i < (b); ++i)
#define repd(i, a, b) for (int i = a; i >= (b); --i)
#define repdl(i, a, b) for (ll i = a; i >= (b); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
#endif
#ifndef _TEMPLATE_IO
#define _TEMPLATE_IO

template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void writef(T... tail) {
  write(tail...);
  cout << flush;
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  if (v.size()) {
    rep(i, 0, sz(v) - 1) out << v[i] << " ";
    out << v.back();
  }
  return out;
}
#endif
using namespace std;

struct Hist {
  int dist, i, j;
  const bool operator<(const Hist &h) const { return dist > h.dist; }
};

char mp[1005][1005];
int md[1005][1005];
queue<pii> q;
int h, w;
const int inf = 0x3f3f3f3f;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  read(h, w);
  rep(i, 0, 1005) rep(j, 0, 1005) md[i][j] = inf;
  int ch, cw, dh, dw;
  read(ch, cw, dh, dw);
  md[ch][cw] = 0;
  q.push({ch, cw});
  cin >> skipws;
  rep(i, 1, h + 1) rep(j, 1, w + 1) read(mp[i][j]);
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front();
    q.pop();
    rep(dx, -2, 3) rep(dy, -2, 3) {
      int ni = i + dx, nj = j + dy;
      int dd = (abs(dx) + abs(dy) == 1) ? 0 : 1;
      if (1 <= ni && ni <= h && 1 <= nj && nj <= w) {
        if (md[ni][nj] > md[i][j] + dd && mp[ni][nj] != '#') {
          md[ni][nj] = md[i][j] + dd;
          q.push({ni, nj});
        }
      }
    }
  }
  if (md[dh][dw] == inf)
    write(-1);
  else
    write(md[dh][dw]);
}