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

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0)
// #define MULTI_TEST
// #define GOOGLE
#ifdef LOCAL
#include "debugger.h"
#else
#define db(...)
#endif
#define ll long long
#define v32 vector<int>
#define v64 vector<ll>
#define s32 set<int>
#define s64 set<ll>
#define p32 pair<int, int>
#define p64 pair<ll, ll>
#define sz(v) (ll)(v).size()
#define fi first
#define se second
#define ln '\\n'
const ll MOD = 1e9 + 7;
inline ll add(ll a, ll b, ll m) { return (((a % m + b % m) % m + m) % m); }
inline ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }

vector<p32> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
bool check(int i, int j, int n, int m) {
  return (i >= 0 && i < n && j >= 0 && j < m);
}
int getDir(int i, int j, int x, int y) {
  if (y == j && i > x)
    return 0;
  if (i == x && y > j)
    return 1;
  if (y == j && i < x)
    return 2;
  if (i == x && y < j)
    return 3;
  return -1;
}

void solve(int tc) {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> arr(h);
  for (int i = 0; i < h; i++)
    cin >> arr[i];
  queue<p32> Q;
  vector<v32> dist(h, v32(w, 1e9));
  dist[x1][y1] = 0;
  Q.push({x1, y1});
  while (!Q.empty()) {
    auto p = Q.front();
    Q.pop();
    int x = p.fi, y = p.se;
    for (auto itr : dir) {
      for (int j = 1; j < k + 1; j++) {
        int nx = x + itr.fi * j;
        int ny = y + itr.se * j;
        if (!check(nx, ny, h, w))
          continue;
        if (arr[nx][ny] == '@')
          break;
        if (dist[nx][ny] <= dist[x][y])
          break;
        if (dist[nx][ny] == 1e9) {
          dist[nx][ny] = dist[x][y] + 1;
          Q.push({nx, ny});
        }
      }
    }
  }
  cout << (dist[x2][y2] == 1e9 ? -1 : dist[x2][y2]) << ln;
  return;
}

int main() {
#ifndef LOCAL
  FASTIO;
#endif
  int t = 1;
#ifdef LOCAL
  clock_t start, stop;
  start = clock();
#endif
#ifdef MULTI_TEST
  cin >> t;
#endif
  for (int tc = 1; tc < t + 1; tc++) {
#ifdef GOOGLE
    cout << "Case #" << tc << ": ";
#endif
    solve(tc);
  }
#ifdef LOCAL
  stop = clock();
  cerr << "Time :" << fixed << setprecision(5)
       << double(stop - start) / double(CLOCKS_PER_SEC) << "s" << ln;
#endif
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0)
// #define MULTI_TEST
// #define GOOGLE
#ifdef LOCAL
#include "debugger.h"
#else
#define db(...)
#endif
#define ll long long
#define v32 vector<int>
#define v64 vector<ll>
#define s32 set<int>
#define s64 set<ll>
#define p32 pair<int, int>
#define p64 pair<ll, ll>
#define sz(v) (ll)(v).size()
#define fi first
#define se second
#define ln '\\n'
const ll MOD = 1e9 + 7;
inline ll add(ll a, ll b, ll m) { return (((a % m + b % m) % m + m) % m); }
inline ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }

vector<p32> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
bool check(int i, int j, int n, int m) {
  return (i >= 0 && i < n && j >= 0 && j < m);
}
int getDir(int i, int j, int x, int y) {
  if (y == j && i > x)
    return 0;
  if (i == x && y > j)
    return 1;
  if (y == j && i < x)
    return 2;
  if (i == x && y < j)
    return 3;
  return -1;
}

void solve(int tc) {
  freopen("rand_1000_02.txt", "r", stdin);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> arr(h);
  for (int i = 0; i < h; i++)
    cin >> arr[i];
  queue<p32> Q;
  vector<v32> dist(h, v32(w, 1e9));
  dist[x1][y1] = 0;
  Q.push({x1, y1});
  while (!Q.empty()) {
    auto p = Q.front();
    Q.pop();
    int x = p.fi, y = p.se;
    for (auto itr : dir) {
      for (int j = 1; j < k + 1; j++) {
        int nx = x + itr.fi * j;
        int ny = y + itr.se * j;
        if (!check(nx, ny, h, w))
          continue;
        if (arr[nx][ny] == '@')
          break;
        if (dist[nx][ny] <= dist[x][y] + 1)
          break;
        if (dist[nx][ny] > dist[x][y] + 1) {
          dist[nx][ny] = dist[x][y] + 1;
          Q.push({nx, ny});
        }
      }
    }
  }
  cout << (dist[x2][y2] == 1e9 ? -1 : dist[x2][y2]) << ln;
  return;
}

int main() {
#ifndef LOCAL
  FASTIO;
#endif
  int t = 1;
#ifdef LOCAL
  clock_t start, stop;
  start = clock();
#endif
#ifdef MULTI_TEST
  cin >> t;
#endif
  for (int tc = 1; tc < t + 1; tc++) {
#ifdef GOOGLE
    cout << "Case #" << tc << ": ";
#endif
    solve(tc);
  }
#ifdef LOCAL
  stop = clock();
  cerr << "Time :" << fixed << setprecision(5)
       << double(stop - start) / double(CLOCKS_PER_SEC) << "s" << ln;
#endif
  return 0;
}