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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define VIEW(x)                                                                \\
  do {                                                                         \\
    cerr << #x << ": ";                                                        \\
    for (auto i : x)                                                           \\
      cerr << i << " ";                                                        \\
    cerr << endl;                                                              \\
  } while (0)
#define ALL(x) (x).begin(), (x).end()
template <class T> bool umax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool umin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
template <typename T> void FILL(vector<T> &v, const T &x) {
  fill(v.begin(), v.end(), x);
}
template <typename T> void FILL(vector<vector<T>> &v, const T &x) {
  for (auto &i : v)
    fill(i.begin(), i.end(), x);
}

vector<pair<int, int>> direction = {make_pair(1, 0), make_pair(0, 1),
                                    make_pair(-1, 0), make_pair(0, -1)};

vector<pair<int, int>> direction5 = {
    make_pair(2, 0), make_pair(0, 2),   make_pair(-2, 0), make_pair(0, -2),
    make_pair(2, 2), make_pair(-2, -2), make_pair(-2, 2), make_pair(2, -2),
    make_pair(1, 2), make_pair(-1, -2), make_pair(-1, 2), make_pair(1, -2),
    make_pair(2, 1), make_pair(-2, -1), make_pair(-2, 1), make_pair(2, -1),
    make_pair(1, 1), make_pair(-1, -1), make_pair(-1, 1), make_pair(1, -1)};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  --ch, --cw, --dh, --dw;

  vector<string> grid(h);
  rep(i, h) cin >> grid[i];

  const int INF = 1 << 30;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[ch][cw] = 0;

  priority_queue<tuple<int, int, int>> pq;
  pq.emplace(0, ch, cw);

  while (pq.size()) {
    auto [d, hh, ww] = pq.top();
    pq.pop();
    d = -d;
    if (dist[hh][ww] < d)
      continue;
    for (auto [dhh, dww] : direction) {
      int hhh = hh + dhh, www = ww + dww;
      if (hhh < 0 || hhh >= h || www < 0 || www >= w)
        continue;
      if (grid[hhh][www] == '#')
        continue;
      if (dist[hhh][www] <= d)
        continue;
      dist[hhh][www] = d;
      pq.emplace(-d, hhh, www);
    }
    for (auto [dhh, dww] : direction5) {
      int hhh = hh + dhh, www = ww + dww;
      if (hhh < 0 || hhh >= h || www < 0 || www >= w)
        continue;
      if (grid[hhh][www] == '#')
        continue;
      if (dist[hhh][www] <= d + 1)
        continue;
      dist[hhh][www] = d + 1;
      pq.emplace(-d - 1, hhh, www);
    }
  }
  if (dist[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dist[dh][dw] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define VIEW(x)                                                                \\
  do {                                                                         \\
    cerr << #x << ": ";                                                        \\
    for (auto i : x)                                                           \\
      cerr << i << " ";                                                        \\
    cerr << endl;                                                              \\
  } while (0)
#define ALL(x) (x).begin(), (x).end()
template <class T> bool umax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool umin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
template <typename T> void FILL(vector<T> &v, const T &x) {
  fill(v.begin(), v.end(), x);
}
template <typename T> void FILL(vector<vector<T>> &v, const T &x) {
  for (auto &i : v)
    fill(i.begin(), i.end(), x);
}

vector<pair<int, int>> direction = {make_pair(1, 0), make_pair(0, 1),
                                    make_pair(-1, 0), make_pair(0, -1)};

vector<pair<int, int>> direction5 = {
    make_pair(2, 0), make_pair(0, 2), make_pair(-2, 0), make_pair(0, -2),
    make_pair(2, 2), make_pair(2, 2), make_pair(-2, 2), make_pair(2, -2),
    make_pair(1, 2), make_pair(1, 2), make_pair(-1, 2), make_pair(1, -2),
    make_pair(2, 1), make_pair(2, 1), make_pair(-2, 1), make_pair(2, -1),
    make_pair(1, 1), make_pair(1, 1), make_pair(-1, 1), make_pair(1, -1)};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  --ch, --cw, --dh, --dw;

  vector<string> grid(h);
  rep(i, h) cin >> grid[i];

  const int INF = 1 << 30;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[ch][cw] = 0;

  priority_queue<tuple<int, int, int>> pq;
  pq.emplace(0, ch, cw);

  while (pq.size()) {
    auto [d, hh, ww] = pq.top();
    pq.pop();
    if (dist[hh][ww] < d)
      continue;
    for (auto [dhh, dww] : direction) {
      int hhh = hh + dhh, www = ww + dww;
      if (hhh < 0 || hhh >= h || www < 0 || www >= w)
        continue;
      if (grid[hhh][www] == '#')
        continue;
      if (dist[hhh][www] <= d)
        continue;
      dist[hhh][www] = d;
      pq.emplace(d, hh + dhh, ww + dww);
    }
    for (auto [dhh, dww] : direction5) {
      int hhh = hh + dhh, www = ww + dww;
      if (hhh < 0 || hhh >= h || www < 0 || www >= w)
        continue;
      if (grid[hhh][www] == '#')
        continue;
      if (dist[hhh][www] <= d + 1)
        continue;
      dist[hhh][www] = d + 1;
      pq.emplace(d + 1, hh + dhh, ww + dww);
    }
  }
  if (dist[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dist[dh][dw] << endl;
}