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

/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ll, pair<ll, ll>> plll;
#define fastread() (ios_base::sync_with_stdio(false), cin.tie(NULL));
#define vll(v) v.begin(), v.end()
#define all(x) x.rbegin(), x.rend()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define x first
#define y second
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define minheap int, vector<int>, greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x) (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch)                                                            \\
  ((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ||          \\
   (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'))
#define bug cout << "BUG" << endl;
const int Max = 1e6 + 10;
const int Mod = 1e9 + 7;
const double PI = 3.141592653589793238463;
bool compare(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}

ll n, m, k;

pll s, e;

vector<char> ara[Max + 10];
vector<ll> cost[Max + 10];

void soln() {
  ll i, j, p, a, sum = 0, t, b, c, d, cnt = 0, g, l, r;

  bool flag = false;

  string str;

  cin >> n >> m >> k;

  cin >> s.x >> s.y >> e.x >> e.y;

  char ch;
  for (ll i = 1; i <= n; i++) {
    ara[i].eb('@');
    cost[i].eb(1e9);
    for (j = 1; j <= m; j++) {

      cin >> ch;
      ara[i].eb(ch);

      cost[i].eb(1e9);
    }
  }

  priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>>
      q;

  q.push({0ll, {s.x, s.y}});

  cost[s.x][s.y] = 0;

  while (!q.empty()) {
    auto u = q.top().y;

    q.pop();

    i = u.x;
    j = u.y;

    if (i == e.x && j == e.y)
      break;

    // cout<<i<<" "<<j<<" "<<cost[i][j]<<endl;

    i++;

    while (i <= n && i <= (u.x + k) && ara[i][j] == '.' &&
           cost[u.x][u.y] < cost[i][j]) {

      if (cost[u.x][u.y] + 1 < cost[i][j]) {
        cost[i][j] = cost[u.x][u.y] + 1;
        q.push({cost[i][j], {i, j}});
      }
      i++;
    }

    i = u.x;

    i--;

    while (i >= 1 && i >= (u.x - k) && ara[i][j] == '.' &&
           cost[u.x][u.y] < cost[i][j]) {
      if (cost[u.x][u.y] + 1 < cost[i][j]) {
        cost[i][j] = cost[u.x][u.y] + 1;
        q.push({cost[i][j], {i, j}});
      }
      i--;
    }

    i = u.x;
    j = u.y;

    j++;

    while (j <= m && j <= (u.y + k) && ara[i][j] == '.' &&
           cost[u.x][u.y] < cost[i][j]) {
      if (cost[u.x][u.y] + 1 < cost[i][j]) {
        cost[i][j] = cost[u.x][u.y] + 1;
        q.push({cost[i][j], {i, j}});
      }
      j++;
    }

    i = u.x;
    j = u.y;
    j--;

    while (j >= 1 && j >= (u.y - k) && ara[i][j] == '.' &&
           cost[u.x][u.y] < cost[i][j]) {
      if (cost[u.x][u.y] + 1 < cost[i][j]) {
        cost[i][j] = cost[u.x][u.y] + 1;
        q.push({cost[i][j], {i, j}});
      }
      j--;
    }
  }

  if (cost[e.x][e.y] == 1e9)
    cost[e.x][e.y] = -1;

  cout << cost[e.x][e.y] << endl;
}

int main() {
  // in;

  fastread();

  soln();
}


Error: -11

Fixed submission:

/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ll, pair<ll, ll>> plll;
#define fastread() (ios_base::sync_with_stdio(false), cin.tie(NULL));
#define vll(v) v.begin(), v.end()
#define all(x) x.rbegin(), x.rend()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define x first
#define y second
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define minheap int, vector<int>, greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x) (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch)                                                            \\
  ((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ||          \\
   (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'))
#define bug cout << "BUG" << endl;
const int Max = 1e6 + 10;
const int Mod = 1e9 + 7;
const double PI = 3.141592653589793238463;
bool compare(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}

ll n, m, k;

pll s, e;

vector<char> ara[Max + 10];
vector<ll> cost[Max + 10];

void soln() {
  ll i, j, p, a, sum = 0, t, b, c, d, cnt = 0, g, l, r;

  bool flag = false;

  string str;

  cin >> n >> m >> k;

  cin >> s.x >> s.y >> e.x >> e.y;

  char ch;
  for (ll i = 1; i <= n; i++) {
    ara[i].eb('.');
    cost[i].eb(1e9);
    for (j = 1; j <= m; j++) {

      cin >> ch;
      ara[i].eb(ch);

      cost[i].eb(1e9);
    }
  }

  priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>>
      q;

  q.push({0ll, {s.x, s.y}});

  cost[s.x][s.y] = 0;

  while (!q.empty()) {
    auto u = q.top().y;

    q.pop();

    i = u.x;
    j = u.y;

    i++;

    while (i <= n && i <= (u.x + k) && ara[i][j] == '.' &&
           cost[u.x][u.y] + 1 <= cost[i][j]) {
      cost[i][j] = cost[u.x][u.y] + 1;
      q.push({cost[i][j], {i, j}});
      i++;
    }

    i = u.x;
    j = u.y;

    j++;

    while (j <= m && j <= (u.y + k) && ara[i][j] == '.' &&
           cost[u.x][u.y] + 1 <= cost[i][j]) {
      cost[i][j] = cost[u.x][u.y] + 1;
      q.push({cost[i][j], {i, j}});
      j++;
    }

    i = u.x;

    i--;

    while (i >= 1 && i >= (u.x - k) && ara[i][j] == '.' &&
           cost[u.x][u.y] + 1 <= cost[i][j]) {
      cost[i][j] = cost[u.x][u.y] + 1;
      q.push({cost[i][j], {i, j}});
      i--;
    }

    i = u.x;
    j = u.y;
    j--;

    while (j >= 1 && j >= (u.y - k) && ara[i][j] == '.' &&
           cost[u.x][u.y] + 1 <= cost[i][j]) {
      cost[i][j] = cost[u.x][u.y] + 1;
      q.push({cost[i][j], {i, j}});
      j--;
    }
  }

  if (cost[e.x][e.y] == 1e9)
    cost[e.x][e.y] = -1;

  cout << cost[e.x][e.y] << endl;
}

int main() {
  in;

  fastread();

  soln();
}