Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes, T no) {
  cout << (val ? yes : no) << "\n";
}

void say(bool val, string yes = "Yes", string no = "No") {
  say<string>(val, yes, no);
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }

// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
  int g = gcd(n, m);
  return n * m / g;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 2 "main.cpp"

// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<int>> dp(h, vi(w, 50000000));
  priority_queue<pair<int, pair<int, int>>> q;

  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};

  q.push({0, {ch, cw}});
  auto isIn = [&](int i, int j) -> bool {
    return 0 <= i && i < h && 0 <= j && j < w;
  };
  while (!q.empty()) {
    auto [score, pos] = q.top();
    auto [i, j] = pos;
    q.pop();
    if (dp[i][j] <= score)
      continue;
    dp[i][j] = score;
    REP(k, 4) {
      int ni = i + dx[k], nj = j + dy[k];
      if (isIn(ni, nj) && s[ni][nj] == '.' && dp[ni][nj] > dp[i][j]) {
        q.push({score, {ni, nj}});
      }
    }
    for (int di = -2; di <= 2; di++) {
      for (int dj = -2; dj <= 2; dj++) {
        int ni = i + di, nj = j + dj;
        if (isIn(ni, nj) && s[ni][nj] == '.' && dp[ni][nj] > dp[i][j] + 1) {
          q.push({score + 1, {ni, nj}});
        }
      }
    }
  }
  if (dp[dh][dw] == 50000000) {
    cout << -1 << endl;
  } else {
    cout << dp[dh][dw] << endl;
  }

  return 0;
}


Fixed submission:
#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes, T no) {
  cout << (val ? yes : no) << "\n";
}

void say(bool val, string yes = "Yes", string no = "No") {
  say<string>(val, yes, no);
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }

// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
  int g = gcd(n, m);
  return n * m / g;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 2 "main.cpp"

// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<int>> dp(h, vi(w, 50000000));
  using pii = pair<int, pair<int, int>>;
  priority_queue<pii, vector<pii>, greater<pii>> q;

  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};

  q.push({0, {ch, cw}});
  auto isIn = [&](int i, int j) -> bool {
    return 0 <= i && i < h && 0 <= j && j < w;
  };
  while (!q.empty()) {
    auto [score, pos] = q.top();
    auto [i, j] = pos;
    q.pop();
    if (dp[i][j] <= score)
      continue;
    dp[i][j] = score;
    REP(k, 4) {
      int ni = i + dx[k], nj = j + dy[k];
      if (isIn(ni, nj) && s[ni][nj] == '.' && dp[ni][nj] > dp[i][j]) {
        q.push({score, {ni, nj}});
      }
    }
    for (int di = -2; di <= 2; di++) {
      for (int dj = -2; dj <= 2; dj++) {
        int ni = i + di, nj = j + dj;
        if (isIn(ni, nj) && s[ni][nj] == '.' && dp[ni][nj] > dp[i][j] + 1) {
          q.push({score + 1, {ni, nj}});
        }
      }
    }
  }
  if (dp[dh][dw] == 50000000) {
    cout << -1 << endl;
  } else {
    cout << dp[dh][dw] << endl;
  }

  return 0;
}