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

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

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

  int64_t H, W;
  cin >> H >> W;
  int64_t start_r, start_c;
  cin >> start_r >> start_c;
  start_r--, start_c--;
  int64_t goal_r, goal_c;
  cin >> goal_r >> goal_c;
  goal_r--, goal_c--;

  vector<vector<bool>> is_open(H, vector<bool>(W, false));
  for (int64_t r = 0; r < H; ++r) {
    string s;
    cin >> s;
    for (int64_t c = 0; c < W; ++c) {
      if (s[c] == '.') {
        is_open[r][c] = true;
      }
    }
  }

  using QQ = pair<int64_t, Pair>;
  priority_queue<QQ> q;
  vector<vector<int64_t>> distances(H, vector<int64_t>(W, kInf));
  vector<vector<bool>> visited(H, vector<bool>(W, false));

  auto Update = [&](const Pair target) -> void {
    if (visited[target.first][target.second]) {
      return;
    }
    visited[target.first][target.second] = true;
    for (int64_t d_row = -2; d_row <= 2; ++d_row) {
      const int64_t n_row = target.first + d_row;
      for (int64_t d_col = -2; d_col <= 2; ++d_col) {
        const int64_t n_col = target.second + d_col;
        const int64_t step = std::abs(d_row) + std::abs(d_col);
        int64_t d;
        if (step == 0) {
          d = -1;
        } else if (step == 1) {
          d = 0;
        } else {
          d = 1;
        }
        if (d >= 0 && n_row >= 0 && n_row < H && n_col >= 0 && n_col < W &&
            is_open[n_row][n_col]) {
          const int64_t next_distance =
              distances[target.first][target.second] + d;
          if (next_distance < distances[n_row][n_col]) {
            distances[n_row][n_col] = next_distance;
            q.emplace(QQ(-next_distance, Pair(n_row, n_col)));
          }
        }
      }
    }
  };

  distances[start_r][start_c] = 0;
  q.emplace(QQ(0, Pair(start_r, start_c)));
  while (!q.empty()) {
    const Pair next = q.top().second;
    q.pop();
    Update(next);
  }

  if (distances[goal_r][goal_c] == kInf) {
    cout << -1 << endl;
  } else {
    cout << distances[goal_r][goal_c] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

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

  int64_t H, W;
  cin >> H >> W;
  int64_t start_r, start_c;
  cin >> start_r >> start_c;
  start_r--, start_c--;
  int64_t goal_r, goal_c;
  cin >> goal_r >> goal_c;
  goal_r--, goal_c--;

  vector<vector<bool>> is_open(H, vector<bool>(W, false));
  for (int64_t r = 0; r < H; ++r) {
    string s;
    cin >> s;
    for (int64_t c = 0; c < W; ++c) {
      if (s[c] == '.') {
        is_open[r][c] = true;
      }
    }
  }

  using QQ = pair<int64_t, Pair>;
  priority_queue<QQ> q;
  vector<vector<int64_t>> distances(H, vector<int64_t>(W, kInf));

  auto Update = [&](const Pair target) -> void {
    for (int64_t d_row = -2; d_row <= 2; ++d_row) {
      const int64_t n_row = target.first + d_row;
      for (int64_t d_col = -2; d_col <= 2; ++d_col) {
        const int64_t n_col = target.second + d_col;
        const int64_t step = std::abs(d_row) + std::abs(d_col);
        int64_t d;
        if (step == 0) {
          d = -1;
        } else if (step == 1) {
          d = 0;
        } else {
          d = 1;
        }
        if (d >= 0 && n_row >= 0 && n_row < H && n_col >= 0 && n_col < W &&
            is_open[n_row][n_col]) {
          const int64_t next_distance =
              distances[target.first][target.second] + d;
          if (next_distance < distances[n_row][n_col]) {
            distances[n_row][n_col] = next_distance;
            q.emplace(QQ(next_distance, Pair(n_row, n_col)));
          }
        }
      }
    }
  };

  distances[start_r][start_c] = 0;
  q.emplace(QQ(0, Pair(start_r, start_c)));
  while (!q.empty()) {
    const Pair next = q.top().second;
    q.pop();
    Update(next);
  }

  if (distances[goal_r][goal_c] == kInf) {
    cout << -1 << endl;
  } else {
    cout << distances[goal_r][goal_c] << endl;
  }
  return 0;
}