Problem p02579 (C++) — buggy submission, verdict: Runtime Error

/**
 * @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;

// std::cout << std::setprecision(20) << 1.1 << endl;

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;
      }
    }
  }

  // BFS preparation
  // First=row(y), Second=col(x)
  deque<Pair> q;
  vector<vector<int64_t>> distances(H, vector<int64_t>(W, kInf));
  auto UpdateFront = [&](const Pair target, const int64_t distance) -> void {
    if (distance < distances[target.first][target.second]) {
      distances[target.first][target.second] = distance;
      q.emplace_front(target);
    }
  };
  auto UpdateBack = [&](const Pair target, const int64_t distance) -> void {
    if (distance < distances[target.first][target.second]) {
      distances[target.first][target.second] = distance;
      q.emplace_back(target);
    }
  };

  // BFS
  // First=row(y), Second=col(x)
  UpdateFront(Pair(start_r, start_c), 0);
  while (!q.empty()) {
    const Pair target = q.front();
    q.pop_front();
    for (int64_t next_row = target.first - 2; next_row <= target.first + 2;
         ++next_row) {
      for (int64_t next_col = target.second - 2; next_col <= target.second + 2;
           ++next_col) {
        if (next_row >= 0 && next_row < H && next_col >= 0 && next_col < W &&
            is_open[next_row][next_col]) {
          const int64_t mdist =
              abs(next_col - target.second) + abs(next_row - target.first);
          if (mdist == 0) {
            // DO NOTHING
          } else if (mdist <= 1) {
            UpdateFront(Pair(next_row, next_col),
                        distances[target.first][target.second]);
          } else {
            UpdateBack(Pair(next_row, next_col),
                       distances[target.first][target.second] + 1);
          }
        }
      }
    }
  }

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


Error: 0

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;

// std::cout << std::setprecision(20) << 1.1 << endl;

const int64_t di[] = {-1, 0, 1, 0}; // Left, Up, Right, Down
const int64_t dj[] = {0, -1, 0, 1}; // Left, Up, Right, Down

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

  int64_t H, W;
  cin >> H >> W;
  Pair start;
  cin >> start.second >> start.first;
  --start.first;
  --start.second;
  Pair goal;
  cin >> goal.second >> goal.first;
  --goal.first;
  --goal.second;

  using QQ = pair<int64_t, Pair>;
  priority_queue<QQ> to_visit;

  vector<vector<bool>> is_open(H, vector<bool>(W, true));
  for (int64_t i = 0; i < H; ++i) {
    string s;
    cin >> s;
    for (int64_t j = 0; j < W; ++j) {
      const char ch = s.c_str()[j];
      if (ch == '#') {
        is_open[i][j] = false;
      }
    }
  }

  // BFS preparation
  queue<Pair> 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, const int64_t distance) -> void {
    if (visited[target.second][target.first]) {
      return;
    }
    visited[target.second][target.first] = true;
    distances[target.second][target.first] = distance;
    q.emplace(target);
    const int64_t next_distance = distance + 1;
    for (int64_t dx = -2; dx <= 2; ++dx) {
      for (int64_t dy = -2; dy <= 2; ++dy) {
        if (dx != 0 || dy != 0) {
          const int64_t nn_x = target.first + dx;
          const int64_t nn_y = target.second + dy;
          if (nn_y >= 0 && nn_y < H && nn_x >= 0 && nn_x < W &&
              is_open[nn_y][nn_x] && next_distance < distances[nn_y][nn_x]) {
            to_visit.emplace(QQ(next_distance, Pair(nn_x, nn_y)));
            distances[nn_y][nn_x] = next_distance;
          }
        }
      }
    }
  };

  auto Start = [&](const Pair start_from, const int64_t init_dist) -> void {
    Update(Pair(start_from.first, start_from.second), init_dist);
    const int64_t d_nexts[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    while (!q.empty()) {
      const Pair target = q.front();
      q.pop();
      for (const auto &d : d_nexts) {
        const int64_t next_row = target.second + d[0];
        const int64_t next_col = target.first + d[1];
        if (next_row >= 0 && next_row < H && next_col >= 0 && next_col < W) {
          if (is_open[next_row][next_col]) {
            Update(Pair(next_row, next_col), init_dist);
          }
        }
      }
    }
  };

  to_visit.emplace(QQ(0, Pair(start.first, start.second)));
  distances[start.second][start.first] = 0;
  while (!to_visit.empty()) {
    const Pair c_start = to_visit.top().second;
    to_visit.pop();
    // cerr << c_start.first << ", " << c_start.second << ", d=" <<
    // distances[c_start.first][c_start.second] << endl;
    Start(Pair(c_start.first, c_start.second),
          distances[c_start.second][c_start.first]);
    if (distances[goal.second][goal.first] ==
        distances[c_start.second][c_start.first]) {
      break;
    }
  }

  if (distances[goal.second][goal.first] == kInf) {
    cout << -1 << endl;
  } else {
    cout << distances[goal.second][goal.first] << endl;
  }
  return 0;
}