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

#include <bits/stdc++.h>
#define loop2(i, s, n, a) for (int i = int(s); i < int(n); i += a)
#define loop(i, s, n) loop2(i, s, n, 1)
#define rep(i, n) loop(i, 0, n)

#define pub push_back
#define all(in) in.begin(), in.end()

using ll = long long; // typedef long long ll; と同意
using ull = unsigned long long;
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<string> map(h);
  rep(i, h) cin >> map[i];

  vector<vector<int>> data(h, vector<int>(w, 1 << 30));
  queue<pair<int, int>> que;

  data[sx][sy] = 0;
  que.push(make_pair(sx, sy));

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

  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    int x = p.first;
    int y = p.second;
    rep(i, 4) {
      rep(j, k) {
        int nx = x + (j + 1) * dx[i];
        int ny = y + (j + 1) * dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || map[nx][ny] == '@')
          break;
        int newdata = data[x][y] + 1;
        if (data[nx][ny] > newdata) {
          data[nx][ny] = newdata;
          que.push(make_pair(nx, ny));
        } else if (data[nx][ny] < newdata)
          break;
      }
    }
  }
  int ans2 = data[gx][gy];
  if (ans2 == (1 << 30))
    cout << -1 << endl;
  else
    cout << ans2 << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define loop2(i, s, n, a) for (int i = int(s); i < int(n); i += a)
#define loop(i, s, n) loop2(i, s, n, 1)
#define rep(i, n) loop(i, 0, n)
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> map;
  rep(i, h) cin >> map[i];
  int data[h][w];
  rep(i, h) rep(j, w) data[i][j] = 100000;

  queue<pair<int, int>> que;
  pair<int, int> pp;
  pp = make_pair(x1 - 1, y1 - 1);
  que.push(pp);
  data[x1 - 1][y1 - 1] = 0;

  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    if (p.first == x2 - 1 && p.second == y2 - 1)
      break;
    loop(i, 1, k + 1) {
      int nx = p.first + i, ny = p.second;
      if (0 > nx || nx >= h || 0 > ny || ny >= w)
        break;
      if (map[nx][ny] == '@')
        break;
      if (0 <= nx && nx < h && 0 <= ny && ny < w && data[nx][ny] == 100000) {
        pp = make_pair(nx, ny);
        que.push(pp);
        data[nx][ny] = data[p.first][p.second] + 1;
      }
    }
    loop(i, 1, k + 1) {
      int nx = p.first - i, ny = p.second;
      if (0 > nx || nx >= h || 0 > ny || ny >= w)
        break;
      if (map[nx][ny] == '@')
        break;
      if (0 <= nx && nx < h && 0 <= ny && ny < w && data[nx][ny] == 100000) {
        pp = make_pair(nx, ny);
        que.push(pp);
        data[nx][ny] = data[p.first][p.second] + 1;
      }
    }
    loop(i, 1, k + 1) {
      int nx = p.first, ny = p.second + i;
      if (0 > nx || nx >= h || 0 > ny || ny >= w)
        break;
      if (map[nx][ny] == '@')
        break;
      if (0 <= nx && nx < h && 0 <= ny && ny < w && data[nx][ny] == 100000) {
        pp = make_pair(nx, ny);
        que.push(pp);
        data[nx][ny] = data[p.first][p.second] + 1;
      }
    }
    loop(i, 1, k + 1) {
      int nx = p.first, ny = p.second - i;
      if (0 > nx || nx >= h || 0 > ny || ny >= w)
        break;
      if (map[nx][ny] == '@')
        break;
      if (0 <= nx && nx < h && 0 <= ny && ny < w && data[nx][ny] == 100000) {
        pp = make_pair(nx, ny);
        que.push(pp);
        data[nx][ny] = data[p.first][p.second] + 1;
      }
    }
  }
  int ans = data[x2 - 1][y2 - 1];
  if (ans == 100000)
    cout << -1 << endl;
  else
    cout << ans << endl;
}