Problem p02644 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int INF = 1e9 + 239;
const int N = 1e2 + 7;
const vector<int> dx = {0, 0, 1, -1};
const vector<int> dy = {1, -1, 0, 0};

int d[4 * N];
vector<int> g[4 * N];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m, K;
  cin >> n >> m >> K;
  int i1, i2, j1, j2;
  cin >> i1 >> j1 >> i2 >> j2;
  i1--;
  i2--;
  j1--;
  j2--;
  vector<vector<int>> ban(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      ban[i][j] = (c == '@');
    }
  }
  fill(d, d + 4 * n * m, INF);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < 4; k++) {
        int ni = i + dx[k];
        int nj = j + dy[k];
        if (0 <= ni && ni < n && 0 <= nj && nj < m && !ban[i][j] &&
            !ban[ni][nj]) {
          g[i * m + j + k * n * m].push_back(ni * m + nj + k * n * m);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!ban[i][j]) {
        for (int k = 0; k < 4; k++) {
          g[i * m + j + k * n * m].push_back(i * m + j + ((k + 1) % 4) * n * m);
        }
      }
    }
  }
  d[i1 * m + j1] = 0;
  set<pair<int, int>> s;
  s.insert({d[i1 * m + j1], i1 * m + j1});
  while (!s.empty()) {
    int v = s.begin()->second;
    s.erase(s.begin());
    // cerr << d[v] << endl;
    for (auto t : g[v]) {
      if (abs(t - v) % (n * m)) {
        if (d[t] > d[v] + 1) {
          s.erase({d[t], t});
          d[t] = d[v] + 1;
          s.insert({d[t], t});
        }
      } else {
        if (d[t] > ((d[v] + K - 1) / K) * K) {
          s.erase({d[t], t});
          d[t] = ((d[v] + K - 1) / K) * K;
          s.insert({d[t], t});
        }
      }
    }
  }
  int ans = d[i2 * m + j2];
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << (ans + K - 1) / K << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int INF = 1e9 + 239;
const int N = 1e6 + 7;
const vector<int> dx = {0, 0, 1, -1};
const vector<int> dy = {1, -1, 0, 0};

int d[4 * N];
vector<int> g[4 * N];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m, K;
  cin >> n >> m >> K;
  int i1, i2, j1, j2;
  cin >> i1 >> j1 >> i2 >> j2;
  i1--;
  i2--;
  j1--;
  j2--;
  vector<vector<int>> ban(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      ban[i][j] = (c == '@');
    }
  }
  fill(d, d + 4 * n * m, INF);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < 4; k++) {
        int ni = i + dx[k];
        int nj = j + dy[k];
        if (0 <= ni && ni < n && 0 <= nj && nj < m && !ban[i][j] &&
            !ban[ni][nj]) {
          g[i * m + j + k * n * m].push_back(ni * m + nj + k * n * m);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!ban[i][j]) {
        for (int k = 0; k < 4; k++) {
          g[i * m + j + k * n * m].push_back(i * m + j + ((k + 1) % 4) * n * m);
        }
      }
    }
  }
  d[i1 * m + j1] = 0;
  set<pair<int, int>> s;
  s.insert({d[i1 * m + j1], i1 * m + j1});
  while (!s.empty()) {
    int v = s.begin()->second;
    s.erase(s.begin());
    // cerr << d[v] << endl;
    for (auto t : g[v]) {
      if (abs(t - v) % (n * m)) {
        if (d[t] > d[v] + 1) {
          s.erase({d[t], t});
          d[t] = d[v] + 1;
          s.insert({d[t], t});
        }
      } else {
        if (d[t] > ((d[v] + K - 1) / K) * K) {
          s.erase({d[t], t});
          d[t] = ((d[v] + K - 1) / K) * K;
          s.insert({d[t], t});
        }
      }
    }
  }
  int ans = d[i2 * m + j2];
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << (ans + K - 1) / K << endl;
  }
}