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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b)                                                           \\
  for (long long i = (a); ((a) < (b) ? i <= (b) : i >= (b));                   \\
       ((a) < (b) ? ++i : --i))
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
using namespace std;
using llong = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
constexpr int INF = 1e9;
constexpr double EPS = 1e-9;
constexpr int MOD = 1e9 + 7;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    cerr << (cnt++ ? ' ' : '>');
    cerr << elem;
  }
  cerr << endl;
}

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

template <class Type_a, class Type_b>
inline bool chmax(Type_a &a, const Type_b &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class Type_a, class Type_b>
inline bool chmin(Type_a &a, const Type_b &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

bool jud(int y, int x, int h, int w) {
  return (0 <= y and y < h) and (0 <= x and x < w);
}

int main() {
  using type = tuple<int, int, int, int, int>; // tuple(0 or 1,cnt,y,x,dp).

  int h, w;
  cin >> h >> w;

  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<string> s(h);
  REP(i, h) cin >> s[i];

  vvi dp(h, vi(w, INF));
  dp[ch][cw] = 0;
  priority_queue<type> pque;
  int cnt = 0;
  pque.push(type(1, cnt--, ch, cw, 0));
  while (!pque.empty()) {
    int y, x, value;
    tie(ignore, ignore, y, x, value) = pque.top();
    pque.pop();
    if (value > dp[y][x])
      continue;

    FOR(i, -2, 2) FOR(j, -2, 2) {
      int ny = y + i, nx = x + j;
      if (jud(ny, nx, h, w)) {
        bool flag = false;
        REP(k, 4) {
          if (i == dy[k] and j == dx[k])
            flag = true;
        }

        if (s[ny][nx] == '.') {
          if (chmin(dp[ny][nx], dp[y][x] + (flag ? 0 : 1)))
            pque.push(type((flag ? 1 : 0), cnt--, ny, nx, dp[ny][nx]));
        }
      }
    }
  }

  if (dp[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dp[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b)                                                           \\
  for (long long i = (a); ((a) < (b) ? i <= (b) : i >= (b));                   \\
       ((a) < (b) ? ++i : --i))
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
using namespace std;
using llong = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
constexpr int INF = 1e9;
constexpr double EPS = 1e-9;
constexpr int MOD = 1e9 + 7;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    cerr << (cnt++ ? ' ' : '>');
    cerr << elem;
  }
  cerr << endl;
}

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

template <class Type_a, class Type_b>
inline bool chmax(Type_a &a, const Type_b &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class Type_a, class Type_b>
inline bool chmin(Type_a &a, const Type_b &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

bool jud(int y, int x, int h, int w) {
  return (0 <= y and y < h) and (0 <= x and x < w);
}

int main() {
  using type = tuple<int, int, int, int, int>; // tuple(0 or 1,cnt,y,x,dp).

  int h, w;
  cin >> h >> w;

  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<string> s(h);
  REP(i, h) cin >> s[i];

  vvi dp(h, vi(w, INF));
  dp[ch][cw] = 0;
  priority_queue<type> pque;
  int cnt = 0;
  pque.push(type(1, cnt--, ch, cw, 0));
  while (!pque.empty()) {
    int y, x, value;
    tie(ignore, ignore, y, x, value) = pque.top();
    pque.pop();
    if (value > dp[y][x])
      continue;

    FOR(i, -2, 2) FOR(j, -2, 2) {
      int ny = y + i, nx = x + j;
      if (jud(ny, nx, h, w)) {
        bool flag = false;
        REP(k, 4) {
          if (i == dy[k] and j == dx[k])
            flag = true;
        }

        if (s[ny][nx] == '.') {
          if (chmin(dp[ny][nx], dp[y][x] + (flag ? 0 : 1)))
            pque.push(type((flag ? 0 : 1), cnt--, ny, nx, dp[ny][nx]));
        }
      }
    }
  }

  if (dp[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dp[dh][dw] << endl;
}