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

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

signed main() {

  int H, W;
  cin >> H >> W;
  int sx, sy;
  cin >> sy >> sx;
  int gx, gy;
  cin >> gy >> gx;
  sx--, sy--, gx--, gy--;

  vector<string> s(H);
  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }

  auto dp = make_vec<int>(H, W);
  fill(dp, INF);
  dp[sy][sx] = 0;

  struct S {
    int y, x, d;
    bool operator<(const S &p) const { return d < p.d; }
    bool operator>(const S &p) const { return d > p.d; }
  };
  priority_queue<S, vector<S>, greater<S>> q; // 最小値
  q.push({sy, sx, 0});
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int y = p.y, x = p.x, d = p.d;
    if (dp[y][x] > d)
      continue;
    int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || H <= ny || nx < 0 || W <= nx)
        continue;
      if (s[ny][nx] == '.' && dp[ny][nx] > dp[y][x]) {
        dp[ny][nx] = dp[y][x];
        q.push({ny, nx, d});
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = y + i;
        int nx = x + j;
        if (ny < 0 || H <= ny || nx < 0 || W <= nx)
          continue;
        if (s[ny][nx] == '.' && dp[ny][nx] > d + 1) {
          dp[ny][nx] = d + 1;
          q.push({ny, nx, d + 1});
        }
      }
    }
  }

  int ans = dp[gy][gx];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

signed main() {

  int H, W;
  cin >> H >> W;
  int sx, sy;
  cin >> sy >> sx;
  int gx, gy;
  cin >> gy >> gx;
  sx--, sy--, gx--, gy--;

  vector<string> s(H);
  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }

  auto dp = make_vec<int>(H, W);
  fill(dp, INF);
  dp[sy][sx] = 0;

  auto dfs = [&](auto &&dfs, int y, int x, int d) -> void {
    if (dp[y][x] > d)
      return;
    int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || H <= ny || nx < 0 || W <= nx)
        continue;
      if (s[ny][nx] == '.' && dp[ny][nx] > dp[y][x]) {
        dp[ny][nx] = dp[y][x];
        dfs(dfs, ny, nx, dp[y][x]);
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = y + i;
        int nx = x + j;
        if (ny < 0 || H <= ny || nx < 0 || W <= nx)
          continue;
        if (s[ny][nx] == '.' && dp[ny][nx] > dp[y][x] + 1) {
          dp[ny][nx] = dp[y][x] + 1;
          dfs(dfs, ny, nx, dp[y][x] + 1);
        }
      }
    }
  };
  dfs(dfs, sy, sx, 0);

  int ans = dp[gy][gx];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}