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 rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;
using P = pair<int, int>;
using PP = pair<int, P>;

int main() {
  int h, w;
  cin >> h >> w;
  int c1, d1;
  cin >> c1 >> d1;
  c1--;
  d1--;
  int c2, d2;
  cin >> c2 >> d2;
  c2--;
  d2--;
  vector<vector<char>> mp(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> mp[i][j];
  vector<int> x1 = {1, 0, -1, 0};
  vector<int> y1 = {0, 1, 0, -1};

  priority_queue<PP, vector<PP>, greater<PP>> que;
  vector<vector<int>> dp(h, vector<int>(w, 100100100));
  que.push(make_pair(0, make_pair(c1, d1)));
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int x = p.second.first, y = p.second.second;
    int c = p.first;
    if (c > dp[x][y])
      continue;
    rep(i, 4) {
      int nx = x + x1[i], ny = y + y1[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && mp[nx][ny] == '.') {
        if (dp[nx][ny] > c) {
          dp[nx][ny] = c;
          que.push(make_pair(c, make_pair(nx, ny)));
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i, ny = y + j;
        if (0 <= nx && nx < h && 0 <= ny && ny < w && mp[nx][ny] == '.') {
          if (dp[nx][ny] > c + 1) {
            dp[nx][ny] = c + 1;
            que.push(make_pair(c + 1, make_pair(nx, ny)));
          }
        }
      }
    }
  }
  cout << (dp[c2][d2] == 100100100 ? -1 : dp[c2][d2]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int c1, d1;
  cin >> c1 >> d1;
  c1--;
  d1--;
  int c2, d2;
  cin >> c2 >> d2;
  c2--;
  d2--;
  vector<vector<char>> mp(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> mp[i][j];
  vector<int> x1 = {1, 0, -1, 0};
  vector<int> y1 = {0, 1, 0, -1};

  vector<vector<int>> dp(h, vector<int>(w, 100100100));
  dp[c1][d1] = 0;
  queue<pair<int, int>> que;
  que.push(make_pair(c1, d1));
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    int c = dp[x][y];
    rep(i, 4) {
      int nx = x + x1[i], ny = y + y1[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && mp[nx][ny] == '.') {
        if (dp[nx][ny] > c) {
          dp[nx][ny] = c;
          que.push(make_pair(nx, ny));
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i, ny = y + j;
        if (0 <= nx && nx < h && 0 <= ny && ny < w && mp[nx][ny] == '.') {
          if (dp[nx][ny] > c + 1) {
            dp[nx][ny] = c + 1;
            que.push(make_pair(nx, ny));
          }
        }
      }
    }
  }
  cout << (dp[c2][d2] == 100100100 ? -1 : dp[c2][d2]) << endl;
  return 0;
}