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;
using ll = long long;
using pii = pair<int, int>;

struct Edge {
  int d, x, y;
};

bool operator<(const Edge &a, const Edge &b) { return a.d < b.d; }

bool operator>(const Edge &a, const Edge &b) { return a.d > b.d; }

const int INF = 1e8;

int main() {
  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];

  priority_queue<Edge, vector<Edge>, greater<Edge>> q;
  q.push(Edge{0, cw, ch});

  vector<vector<int>> d(h, vector<int>(w, INF));
  d[ch][cw] = 0;

  while (!q.empty()) {
    Edge e = q.top();
    q.pop();

    if (d[e.y][e.x] < e.d)
      continue;

    vector<int> dx = {1, 0, -1, 0};
    vector<int> dy = {0, 1, 0, -1};

    rep(i, 4) {
      int nx = e.x + dx[i];
      int ny = e.y + dy[i];
      if (nx < 0 || ny < 0 || nx >= w || ny >= h)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (d[ny][nx] > e.d) {
        d[ny][nx] = e.d;
        q.push(Edge{d[ny][nx], nx, ny});
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = e.x + j;
        int ny = e.y + i;
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (d[ny][nx] > e.d + 1) {
          d[ny][nx] = e.d + 1;
          q.push(Edge{d[ny][nx], nx, ny});
        }
      }
    }
  }

  if (d[dh][dw] < INF)
    cout << d[dh][dw] << endl;
  else
    cout << -1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

struct Edge {
  int step, x, y;
};

bool operator<(const Edge &a, const Edge &b) { return a.step < b.step; }

const int INF = 1e8;

int main() {
  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];

  priority_queue<Edge> q;
  q.push(Edge{0, cw, ch});

  vector<vector<int>> d(h, vector<int>(w, INF));
  d[ch][cw] = 0;

  while (!q.empty()) {
    Edge e = q.top();
    q.pop();

    if (d[e.y][e.x] < e.step)
      continue;

    vector<int> dx = {1, 0, -1, 0};
    vector<int> dy = {0, 1, 0, -1};

    rep(i, 4) {
      int nx = e.x + dx[i];
      int ny = e.y + dy[i];
      if (nx < 0 || ny < 0 || nx >= w || ny >= h)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (d[ny][nx] > d[e.y][e.x]) {
        d[ny][nx] = d[e.y][e.x];
        q.push(Edge{d[ny][nx], nx, ny});
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = e.x + j;
        int ny = e.y + i;
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (d[ny][nx] > d[e.y][e.x] + 1) {
          d[ny][nx] = d[e.y][e.x] + 1;
          q.push(Edge{d[ny][nx], nx, ny});
        }
      }
    }
  }

  if (d[dh][dw] < INF)
    cout << d[dh][dw] << endl;
  else
    cout << -1 << endl;
  return 0;
}