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

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

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<vector<bool>> avail(H);
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    avail[i].resize(W);
    for (int j = 0; j < W; j++)
      avail[i][j] = (s[j] == '.');
  }

  int dx[] = {-1, 1, 0, 0};
  int dy[] = {0, 0, -1, 1};
  auto inside = [&](int i, int j) {
    return i >= 0 && i < H && j >= 0 && j < W && avail[i][j];
  };

  vector<vector<int>> dist(H, vector<int>(W, 1e9));
  queue<pair<int, int>> que;
  que.emplace(sx, sy);
  dist[sx][sy] = 0;

  while (que.size()) {
    auto [i, j] = que.front();
    que.pop();
    for (int k = 0; k < 4; k++)
      for (int d = 1; d <= K; d++) {
        int x = i + d * dx[k], y = j + d * dy[k];
        if (!inside(x, y) || dist[x][y] <= dist[i][j])
          break;
        if (dist[x][y] == 1e9) {
          dist[x][y] = dist[i][j] + 1;
          que.emplace(x, y);
        }
      }
  }

  int ans = dist[gx][gy];
  if (ans == 1e9)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define bit(n) (1 << (n))
#define ii(x)                                                                  \\
  int x;                                                                       \\
  cin >> (x)
#define ii2(x, y)                                                              \\
  int x, y;                                                                    \\
  cin >> (x) >> (y)
#define ii3(x, y, z)                                                           \\
  int x, y, z;                                                                 \\
  cin >> (x) >> (y) >> (z)
#define yn(x, ok, ng) cout << ((x) ? (ok) : (ng)) << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int const INF = 1001001001;

const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};

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

  int H, W, K;
  cin >> H >> W >> K;

  pii s, g;
  cin >> s.first >> s.second;
  s.first--;
  s.second--;
  cin >> g.first >> g.second;
  g.first--;
  g.second--;

  vector<string> fi(H);
  rep(h, H) cin >> fi[h];

  vector<vvi> ori(4, vvi(H, vi(W)));

  rep(h, H) {
    int cur = 0;
    rep(w, W) {
      if (fi[h][w] == '@')
        cur = 0;
      else
        cur++;
      ori[0][h][w] = cur;
    }
  }

  rep(h, H) {
    int cur = 0;
    drep(w, W) {
      if (fi[h][w] == '@')
        cur = 0;
      else
        cur++;
      ori[1][h][w] = cur;
    }
  }

  rep(w, W) {
    int cur = 0;
    rep(h, H) {
      if (fi[h][w] == '@')
        cur = 0;
      else
        cur++;
      ori[2][h][w] = cur;
    }
  }

  rep(w, W) {
    int cur = 0;
    drep(h, H) {
      if (fi[h][w] == '@')
        cur = 0;
      else
        cur++;
      ori[3][h][w] = cur;
    }
  }

  vvi dp(H, vi(W, -1));
  queue<pii> que;

  dp[s.first][s.second] = 0;
  que.push(s);

  while (!que.empty()) {
    pii p = que.front();
    que.pop();
    rep(i, 4) {
      int x = p.first + dx[i];
      int y = p.second + dy[i];
      if (x < 0 || x >= H || y < 0 || y >= W)
        continue;
      if (dp[x][y] != -1 || fi[x][y] == '@')
        continue;
      int max = ori[i][p.first][p.second];
      rrep(j, max) {
        int nx = p.first + j * dx[i];
        int ny = p.second + j * dy[i];
        if (dp[nx][ny] != -1)
          continue;
        dp[nx][ny] = dp[p.first][p.second] + 1;
        que.push(pii{nx, ny});
      }
    }
  }
  cout << dp[g.first][g.second] << endl;
}