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

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> ss(h);
  for (auto &&s : ss) {
    cin >> s;
  }

  auto inside = [](int64_t a, int64_t b, int64_t c, int64_t d) {
    return 0 <= a && a < c && 0 <= b && b < d;
  };

  vector<int64_t> dd = {0, 1, 0, -1, 0};

  int64_t inf = (1LL << 60);

  vector<vector<int64_t>> dist(h, vector<int64_t>(w, inf));
  priority_queue<pair<int64_t, pair<int64_t, int64_t>>,
                 vector<pair<int64_t, pair<int64_t, int64_t>>>, greater<>>
      pq;
  dist[ch][cw] = 0;
  pq.push({dist[ch][cw], {ch, cw}});

  while (!pq.empty()) {
    int64_t d = pq.top().first;
    int64_t i = pq.top().second.first;
    int64_t j = pq.top().second.second;
    pq.pop();

    if (dist[i][j] < d) {
      continue;
    }

    for (int k = 0; k < 4; k++) {
      int64_t ni = i + dd[k];
      int64_t nj = j + dd[k + 1];

      if (inside(ni, nj, h, w) && (ss[ni][nj] == '.') &&
          (dist[i][j] < dist[ni][nj])) {
        dist[ni][nj] = dist[i][j];
        pq.push({dist[ni][nj], {ni, nj}});
      }
    }

    for (int64_t di = -2; di <= 2; di++) {
      for (int64_t dj = -2; dj <= 2; dj++) {
        int64_t ni = i + di;
        int64_t nj = j + dj;

        if (inside(ni, nj, h, w) && (ss[ni][nj] == '.') &&
            (dist[i][j] + 1 < dist[ni][nj])) {
          dist[ni][nj] = dist[i][j] + 1;
          pq.push({dist[ni][nj], {ni, nj}});
        }
      }
    }
  }

  if (dist[dh][dw] == inf) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[dh][dw] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> ss(h);
  for (auto &&s : ss) {
    cin >> s;
  }

  auto inside = [](int64_t a, int64_t b, int64_t c, int64_t d) {
    return 0 <= a && a < c && 0 <= b && b < d;
  };

  vector<int64_t> dd = {0, 1, 0, -1, 0};

  int64_t inf = (1LL << 60);

  vector<vector<int64_t>> dist(h, vector<int64_t>(w, inf));
  queue<pair<int64_t, pair<int64_t, int64_t>>> que;
  dist[ch][cw] = 0;
  que.push({dist[ch][cw], {ch, cw}});

  while (!que.empty()) {
    int64_t d = que.front().first;
    int64_t i = que.front().second.first;
    int64_t j = que.front().second.second;
    que.pop();

    if (dist[i][j] < d) {
      continue;
    }

    for (int k = 0; k < 4; k++) {
      int64_t ni = i + dd[k];
      int64_t nj = j + dd[k + 1];

      if (inside(ni, nj, h, w) && (ss[ni][nj] == '.') &&
          (dist[i][j] < dist[ni][nj])) {
        dist[ni][nj] = dist[i][j];
        que.push({dist[ni][nj], {ni, nj}});
      }
    }

    for (int64_t di = -2; di <= 2; di++) {
      for (int64_t dj = -2; dj <= 2; dj++) {
        int64_t ni = i + di;
        int64_t nj = j + dj;

        if (inside(ni, nj, h, w) && (ss[ni][nj] == '.') &&
            (dist[i][j] + 1 < dist[ni][nj])) {
          dist[ni][nj] = dist[i][j] + 1;
          que.push({dist[ni][nj], {ni, nj}});
        }
      }
    }
  }

  if (dist[dh][dw] == inf) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[dh][dw] << endl;

  return 0;
}