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

#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
// #define ll long long
#define pb emplace_back
#define mp make_pair
#define nt _ll128
#define ld long double
long double PI = 3.14159265358979323846;
using ll = long long;

const ll modo = 1e9 + 7;
const ll inf = LONG_LONG_MAX;
const ll ms = (2e5) + 5;

int main() {
  idfc;

  //   freopen("inptf1.txt","r",stdin);

  ll n, m, k;
  cin >> n >> m >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  ll i, j;
  vector<vector<ll>> ar(n + 1, vector<ll>(m + 1));
  vector<vector<ll>> dis(n + 1, vector<ll>(m + 1));
  //  ll ar[n+1][m+1];
  //  ll dis[n+1][m+1];

  char e;
  string s;
  cin.ignore();
  for (i = 1; i <= n; i++) {
    getline(cin, s);
    for (j = 1; j <= m; j++) {
      e = s[j - 1];
      // cout<<e<<" ";
      dis[i][j] = inf;

      if (e == '.')
        ar[i][j] = 1;
      else
        ar[i][j] = 0;
    }
    //    cout<<"\\n";
  }

  set<pair<ll, pair<ll, ll>>> ss;
  dis[x1][y1] = 0;
  ss.insert({0, {x1, y1}});
  ll d;
  ll x, y;

  while (!ss.empty()) {
    x1 = ss.begin()->second.first;
    y1 = ss.begin()->second.second;
    ss.erase(*ss.begin());
    d = dis[x1][y1];
    d++;

    for (i = 1; i <= k; i++) {
      y = i + y1;
      if (y <= m && (dis[x1][y] >= d) && ar[x1][y]) {
        ss.erase({dis[x1][y], {x1, y}});
        dis[x1][y] = d;
        ss.insert({dis[x1][y], {x1, y}});
      } else
        break;
    }

    for (i = 1; i <= k; i++) {
      y = y1 - i;
      if (y >= 1 && (dis[x1][y] >= d) && ar[x1][y]) {
        ss.erase({dis[x1][y], {x1, y}});
        dis[x1][y] = d;
        ss.insert({dis[x1][y], {x1, y}});
      } else
        break;
    }

    for (i = 1; i <= k; i++) {
      x = x1 + i;
      if (x <= n && (dis[x][y1] >= d) && ar[x][y1]) {
        ss.erase({dis[x][y1], {x, y1}});
        dis[x][y1] = d;
        ss.insert({dis[x][y1], {x, y1}});
      } else
        break;
    }

    for (i = 1; i <= k; i++) {
      x = x1 - i;
      if (x >= 1 && (dis[x][y1] >= d) && ar[x][y1]) {
        ss.erase({dis[x][y1], {x, y1}});
        dis[x][y1] = d;
        ss.insert({dis[x][y1], {x, y1}});
      } else
        break;
    }
  }

  if (dis[x2][y2] == inf)
    cout << "-1";
  else
    cout << dis[x2][y2] << "\\n";

  return 0;
}


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


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
// #define ll long long
#define pb emplace_back
#define mp make_pair
#define nt _ll128
#define ld long double
long double PI = 3.14159265358979323846;
using ll = long long;

const ll modo = 1e9 + 7;
const ll inf = LONG_LONG_MAX;
const ll ms = (2e5) + 5;

int main() {
  // idfc;

  freopen("inptf1.txt", "r", stdin);

  ll n, m, k;
  cin >> n >> m >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  ll i, j;
  vector<vector<ll>> ar(n + 1, vector<ll>(m + 1));
  vector<vector<ll>> dis(n + 1, vector<ll>(m + 1));
  //  ll ar[n+1][m+1];
  //  ll dis[n+1][m+1];

  char e;
  string s;
  cin.ignore();
  for (i = 1; i <= n; i++) {
    getline(cin, s);
    for (j = 1; j <= m; j++) {
      e = s[j - 1];
      // cout<<e<<" ";
      dis[i][j] = inf;

      if (e == '.')
        ar[i][j] = 1;
      else
        ar[i][j] = 0;
    }
    //    cout<<"\\n";
  }

  set<pair<ll, pair<ll, ll>>> ss;
  dis[x1][y1] = 0;
  ss.insert({0, {x1, y1}});
  ll d;
  ll x, y;

  while (!ss.empty()) {
    x1 = ss.begin()->second.first;
    y1 = ss.begin()->second.second;
    ss.erase(*ss.begin());
    d = dis[x1][y1];
    d++;

    for (i = 1; i <= k; i++) {
      y = i + y1;
      if (y <= m && (dis[x1][y] >= d) && ar[x1][y]) {
        ss.erase({dis[x1][y], {x1, y}});
        dis[x1][y] = d;
        ss.insert({dis[x1][y], {x1, y}});
      } else
        break;
    }

    for (i = 1; i <= k; i++) {
      y = y1 - i;
      if (y >= 1 && (dis[x1][y] >= d) && ar[x1][y]) {
        ss.erase({dis[x1][y], {x1, y}});
        dis[x1][y] = d;
        ss.insert({dis[x1][y], {x1, y}});
      } else
        break;
    }

    for (i = 1; i <= k; i++) {
      x = x1 + i;
      if (x <= n && (dis[x][y1] >= d) && ar[x][y1]) {
        ss.erase({dis[x][y1], {x, y1}});
        dis[x][y1] = d;
        ss.insert({dis[x][y1], {x, y1}});
      } else
        break;
    }

    for (i = 1; i <= k; i++) {
      x = x1 - i;
      if (x >= 1 && (dis[x][y1] >= d) && ar[x][y1]) {
        ss.erase({dis[x][y1], {x, y1}});
        dis[x][y1] = d;
        ss.insert({dis[x][y1], {x, y1}});
      } else
        break;
    }
  }

  if (dis[x2][y2] == inf)
    cout << "-1";
  else
    cout << dis[x2][y2] << "\\n";

  return 0;
}