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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

struct dat {
  int d, r, c;
};

int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  int sr, sc;
  cin >> sr >> sc;
  sr--;
  sc--;

  int gr, gc;
  cin >> gr >> gc;
  gr--;
  gc--;

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

  int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));

  auto c = [](dat a, dat b) { return a.d > b.d; };

  priority_queue<dat, vector<dat>, decltype(c)> q(c);
  q.push({0, sr, sc});
  dist[sr][sc] = 0;

  while (!q.empty()) {

    dat dt = q.top();
    q.pop();
    int r = dt.r;
    int c = dt.c;

    if (dist[r][c] < dt.d)
      continue;
    if (r == gr && c == gc)
      continue;

    rep(i, 4) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (0 <= nr && nr < h && 0 <= nc && nc < w && s[nr][nc] == '.') {
        if (dist[nr][nc] > dist[r][c]) {
          dist[nr][nc] = dist[r][c];
          q.push({dist[nr][nc], nr, nc});
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int nr = r + i;
        int nc = c + j;
        if (0 <= nr && nr < h && 0 <= nc && nc < w && s[nr][nc] == '.') {
          if (dist[nr][nc] > dist[r][c] + 1) {
            dist[nr][nc] = dist[r][c] + 1;
            q.push({dist[nr][nc], nr, nc});
          }
        }
      }
    }
  }

  if (dist[gr][gc] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[gr][gc] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  int sr, sc;
  cin >> sr >> sc;
  sr--;
  sc--;

  int gr, gc;
  cin >> gr >> gc;
  gr--;
  gc--;

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

  int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  queue<pii> q, qw;
  q.push({sr, sc});
  dist[sr][sc] = 0;

  while (!q.empty()) {

    int r = q.front().first;
    int c = q.front().second;
    q.pop();

    if (dist[r][c] >= dist[gr][gc])
      continue;

    rep(i, 4) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (0 <= nr && nr < h && 0 <= nc && nc < w && s[nr][nc] == '.') {
        if (dist[nr][nc] > dist[r][c]) {
          dist[nr][nc] = dist[r][c];
          q.push({nr, nc});
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int nr = r + i;
        int nc = c + j;
        if (0 <= nr && nr < h && 0 <= nc && nc < w && s[nr][nc] == '.') {
          if (dist[nr][nc] > dist[r][c] + 1) {
            dist[nr][nc] = dist[r][c] + 1;
            q.push({nr, nc});
          }
        }
      }
    }
  }

  if (dist[gr][gc] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[gr][gc] << endl;
  }
}