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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <assert.h>
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <complex>  // complex
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
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;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H, W, K;
  cin >> H >> W >> K;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> G(H);
  for (int i = 0; i < H; i++)
    cin >> G[i];
  queue<P> que;
  que.emplace(sy, sx);
  vector<vector<int>> dist(H, vector<int>(W, inf));
  dist[sy][sx] = 0;
  while (que.size()) {
    int y, x;
    tie(y, x) = que.front();
    que.pop();
    for (int d = 0; d < 4; d++) {
      for (int k = 1; k <= K; k++) {
        ll ny = y + dy[d] * k;
        ll nx = x + dx[d] * k;
        if (ny < 0 || ny >= H || nx < 0 || nx >= W)
          break;
        if (G[ny][nx] == '@')
          break;
        if (dist[ny][nx] <= dist[y][x])
          break;
        if (dist[ny][nx] == inf) {
          dist[ny][nx] = dist[y][x] + 1;
          que.emplace(ny, nx);
        }
      }
    }
  }
  if (dist[gy][gx] == inf) {
    cout << -1 << "\\n";
  } else {
    cout << dist[gy][gx] << "\\n";
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <assert.h>
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <complex>  // complex
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
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;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H, W, K;
  cin >> H >> W >> K;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> G(H);
  for (int i = 0; i < H; i++)
    cin >> G[i];
  queue<P> que;
  que.emplace(sy, sx);
  vector<vector<int>> dist(H, vector<int>(W, inf));
  dist[sy][sx] = 0;
  while (que.size()) {
    int y, x;
    tie(y, x) = que.front();
    que.pop();
    for (int d = 0; d < 4; d++) {
      for (int k = 1; k <= K; k++) {
        ll ny = y + dy[d] * k;
        ll nx = x + dx[d] * k;
        if (ny < 0 || ny >= H || nx < 0 || nx >= W)
          break;
        if (G[ny][nx] == '@')
          break;
        if (dist[ny][nx] <= dist[y][x])
          break;
        if (dist[x][y] == inf) {
          dist[ny][nx] = dist[y][x] + 1;
          que.emplace(ny, nx);
        }
      }
    }
  }
  if (dist[gy][gx] == inf) {
    cout << -1 << "\\n";
  } else {
    cout << dist[gy][gx] << "\\n";
  }
  return 0;
}