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

#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

using P = pair<int, int>;

const int INF = 1 << 30;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> grid(H);
  for (auto &g : grid) {
    cin >> g;
  }
  vector<vector<int>> dp(H, vector<int>(W, INF));
  queue<P> que;
  que.emplace(x1, y1);
  dp[x1][y1] = 0;
  vector<int> dh = {-1, 0, 1, 0};
  vector<int> dw = {0, -1, 0, 1};
  while (!que.empty()) {
    P now = que.front();
    que.pop();
    int h = now.first;
    int w = now.second;
    rep(i, 0, 4) {
      int nh = h;
      int nw = w;
      rep(j, 0, K) {
        nh += dh[i];
        nw += dw[i];
        if (nh < 0 || nh >= H || nw < 0 || nh >= W) {
          break;
        }
        if (grid[nh][nw] == '@') {
          break;
        }
        if (dp[nh][nw] <= dp[h][w]) {
          break;
        }
        if (dp[nh][nw] == INF) {
          dp[nh][nw] = dp[h][w] + 1;
          que.emplace(nh, nw);
        }
      }
    }
  }
  if (dp[x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[x2][y2] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

using P = pair<int, int>;

const int INF = 1 << 30;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> grid(H);
  for (auto &g : grid) {
    cin >> g;
  }
  vector<vector<int>> dp(H, vector<int>(W, INF));
  queue<P> que;
  que.emplace(x1, y1);
  dp[x1][y1] = 0;
  vector<int> dh = {-1, 0, 1, 0};
  vector<int> dw = {0, -1, 0, 1};
  while (!que.empty()) {
    P now = que.front();
    que.pop();
    int h = now.first;
    int w = now.second;
    rep(i, 0, 4) {
      int nh = h;
      int nw = w;
      rep(j, 0, K) {
        nh += dh[i];
        nw += dw[i];
        if (nh < 0 || nh >= H || nw < 0 || nh >= W) {
          break;
        }
        if (grid[nh][nw] == '@') {
          break;
        }
        if (dp[nh][nw] <= dp[h][w]) {
          break;
        }
        dp[nh][nw] = dp[h][w] + 1;
        que.emplace(nh, nw);
      }
    }
  }
  if (dp[x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[x2][y2] << endl;
  }
  return 0;
}