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

// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define all(a) a.begin(), a.end()
#define MM << " " <<

template <typename T> using MaxHeap = std::priority_queue<T>;
template <typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> void vdeb(std::vector<T> &da) {
  for (int i = 0; i < da.size(); i++) {
    if (i == da.size() - 1)
      std::cout << da[i];
    else
      std::cout << da[i] << ' ';
  }
  std::cout << '\\n';
}
template <typename T> void vdeb(std::vector<std::vector<T>> &da) {
  for (int i = 0; i < da.size(); i++) {
    std::cout << i << ' ';
    vdeb(da[i]);
  }
  std::cout << '\\n';
}

using namespace std;

struct P {
  int x, y, d;
};

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> da(h);
  rip(i, h, 0) cin >> da[i];
  vector<vector<int>> gr(h, vector<int>(w, -1));
  queue<P> q;
  rip(i, 4, 0) {
    gr[x1][y1] = 0;
    q.push({x1, y1, 0});
  }
  auto in_table = [&](int x, int y) {
    return 0 <= x && x < h && 0 <= y && y < w && da[x][y] == '.';
  };
  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    rip(i, 4, 0) {
      int nx = now.x, ny = now.y, nd = now.d, l = gr[nx][ny];
      rip(j, k, 0) {
        nx += dx[i], ny += dy[i];
        if (in_table(nx, ny) && gr[nx][ny] == -1) {
          gr[nx][ny] = l + 1;
          q.push({nx, ny, i});
        } else if (in_table(nx, ny) && gr[nx][ny] == l + 1)
          continue;
        else
          break;
      }
    }
  }
  cout << gr[x2][y2] << endl;
}


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


Fixed submission:

// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define all(a) a.begin(), a.end()
#define MM << " " <<

template <typename T> using MaxHeap = std::priority_queue<T>;
template <typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> void vdeb(std::vector<T> &da) {
  for (int i = 0; i < da.size(); i++) {
    if (i == da.size() - 1)
      std::cout << da[i];
    else
      std::cout << da[i] << ' ';
  }
  std::cout << '\\n';
}
template <typename T> void vdeb(std::vector<std::vector<T>> &da) {
  for (int i = 0; i < da.size(); i++) {
    std::cout << i << ' ';
    vdeb(da[i]);
  }
  std::cout << '\\n';
}

using namespace std;

struct P {
  int x, y, d;
};

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

int main() {
  ifstream inf("./testcase.txt");
  int h, w, k;
  inf >> h >> w >> k;
  int x1, y1, x2, y2;
  inf >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> da(h);
  rip(i, h, 0) inf >> da[i];
  vector<vector<int>> gr(h, vector<int>(w, -1));
  queue<P> q;
  rip(i, 4, 0) {
    gr[x1][y1] = 0;
    q.push({x1, y1});
  }
  auto in_table = [&](int x, int y) {
    return 0 <= x && x < h && 0 <= y && y < w && da[x][y] == '.';
  };
  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    rip(i, 4, 0) {
      int nx = now.x, ny = now.y, nd = now.d, l = gr[nx][ny];
      rip(j, k, 0) {
        nx += dx[i], ny += dy[i];
        if (in_table(nx, ny) && gr[nx][ny] == -1) {
          gr[nx][ny] = l + 1;
          q.push({nx, ny, i});
        } else if (in_table(nx, ny) && gr[nx][ny] == l) {
          j = -1;
        } else {
          break;
        }
      }
    }
  }
  cout << gr[x2][y2] << endl;
}