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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;
const ll INF = 1e15;

typedef tuple<ll, ll, ll, ll> T;

ll D[1001000][4];
ll H, W, K;
vector<vector<bool>> C;

inline int idx(int y, int x) { return y * W + x; }

bool check(int y, int x) {
  if (y < 0 || y >= H)
    return false;
  if (x < 0 || x >= W)
    return false;
  if (C[y][x])
    return false;
  return true;
}

void dijkstra(ll y1, ll x1) {
  REP(i, 4)
  D[idx(y1, x1)][i] = 0;
  priority_queue<T, vector<T>, greater<T>> que;

  REP(i, 4)
  que.push(T(0, y1, x1, i));

  while (!que.empty()) {
    auto t = que.top();
    que.pop();

    ll d = get<0>(t);
    ll y = get<1>(t);
    ll x = get<2>(t);
    ll h = get<3>(t);

    if (d > D[idx(y, x)][h])
      continue;

    const int DY[] = {1, -1, 0, 0};
    const int DX[] = {0, 0, 1, -1};

    // REP(i,4){
    // int dy = DY[i];
    // int dx = DX[i];
    int dy = DY[h];
    int dx = DX[h];

    int nty = y + dy;
    int ntx = x + dx;
    if (!check(nty, ntx))
      continue;

    REP(nth, 4) {
      ll dd = d + 1;
      ll ntd = dd;
      if (nth != h && ntd % K != 0)
        ntd = ntd + (K - ntd % K);
      if (ntd < D[idx(nty, ntx)][nth]) {
        D[idx(nty, ntx)][nth] = ntd;
        que.push(T(ntd, nty, ntx, nth));
      }
    }
    //}
  }
}

int main() {
  cin >> H >> W >> K;

  ll x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--, y1--, x2--, y2--;

  C.resize(H, vector<bool>(W));
  REP(i, H) {
    REP(j, W) {
      char c;
      cin >> c;
      if (c == '@')
        C[i][j] = true;
    }
  }

  REP(i, H * W)
  REP(j, 4)
  D[i][j] = INF;

  dijkstra(y1, x1);

  ll ans = INF;
  REP(i, 4)
  chmin(ans, D[idx(y2, x2)][i]);

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << (ans + K - 1) / K << endl;
  // cout << ans/K << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;

#define HOR 0
#define VER 1
#define NONE 2

int main() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--, y1--, x2--, y2--;

  vector<vector<ll>> T(H + 100, vector<ll>(W + 100, 0));

  REP(i, H) {
    REP(j, W) {
      char c;
      cin >> c;
      if (c == '.')
        T[i][j] = 1;
    }
  }

  typedef tuple<ll, ll, ll, ll> A;
  priority_queue<A> que;
  que.push(A(0, y2, x2, NONE));

  vector<vector<ll>> D(H + 100, vector<ll>(W + 100, 1e14));

  while (!que.empty()) {
    auto top = que.top();
    que.pop();
    ll dist, Y, X, mode;

    dist = get<0>(top);
    Y = get<1>(top);
    X = get<2>(top);
    mode = get<3>(top);

    if (dist > D[Y][X])
      continue;
    D[Y][X] = dist;

    if (mode == VER || mode == NONE) {
      for (int k = 1; k <= K; k++) {
        if (X + k >= W)
          break;
        if (!T[Y][X + k])
          break;
        if (k == K) {
          if (D[Y][X + k] > dist + 1)
            que.push(A(dist + 1, Y, X + k, NONE));
        } else if (D[Y][X + k] > dist + 1)
          que.push(A(dist + 1, Y, X + k, HOR));
      }
      for (int k = 1; k <= K; k++) {
        if (X - k < 0)
          break;
        if (!T[Y][X - k])
          break;
        if (k == K) {
          if (D[Y][X - k] > dist + 1)
            que.push(A(dist + 1, Y, X - k, NONE));
        } else if (D[Y][X - k] > dist + 1)
          que.push(A(dist + 1, Y, X - k, HOR));
      }
    }

    if (mode == HOR || mode == NONE) {
      for (int k = 1; k <= K; k++) {
        if (Y + k >= H)
          break;
        if (!T[Y + k][X])
          break;
        if (k == K) {
          if (D[Y + k][X] > dist + 1)
            que.push(A(dist + 1, Y + k, X, NONE));
        } else if (D[Y + k][X] > dist + 1)
          que.push(A(dist + 1, Y + k, X, VER));
      }
      for (int k = 1; k <= K; k++) {
        if (Y - k < 0)
          break;
        if (!T[Y - k][X])
          break;
        if (k == K) {
          if (D[Y - k][X] > dist + 1)
            que.push(A(dist + 1, Y - k, X, NONE));
        } else if (D[Y - k][X] > dist + 1)
          que.push(A(dist + 1, Y - k, X, VER));
      }
    }
  }

  ll ans = D[y1][x1];
  if (ans == 1e14)
    ans = -1;

  cout << ans << endl;
  return 0;
}