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

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define f first
#define s second
#define matrix vector<vector<ll>>
#define zero(n, m) matrix(n, vector<ll>(m, 0))
#define one(n, m) matrix(n, vector<ll>(m, 1))
#define pii pair<ll, pair<ll, ll>>
const int N = 1e3 + 5;

ll n, m, stX, stY, enX, enY, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0},
                             cost[N][N];
char a[N][N];

bool isValid(int i, int j) {
  if (i < 1 || i > n || j < 1 || j > m || a[i][j] == '#')
    return 0;
  return 1;
}

ll dijs() {
  priority_queue<pii, vector<pii>, greater<pii>> q;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++)
      cost[i][j] = 1e18;
  }

  cost[stX][stY] = 0;
  q.push({0, {stX, stY}});

  while (q.size()) {
    ll i = q.top().s.f, j = q.top().s.s, magic = q.top().f;
    q.pop();
    if (cost[i][j] < magic)
      continue;

    for (int k = 0; k < 4; k++) {
      int ii = i + dx[k], jj = j + dy[k];
      if (isValid(ii, jj) && cost[ii][jj] > magic) {
        cost[ii][jj] = magic;
        q.push({cost[ii][jj], {ii, jj}});
      }
    }

    int st = i - 2, stj = j - 2, en = i + 2, enj = j + 2;
    for (int k = st; k <= en; k++) {
      for (int kk = stj; kk <= enj; kk++) {
        if (isValid(k, kk) && cost[k][kk] > magic + 1) {
          cost[k][kk] = magic + 1;
          q.push({cost[k][kk], {k, kk}});
        }
      }
    }
  }
  if (cost[enX][enY] == 1e18)
    cout << "-1\\n";
  else
    cout << cost[enX][enY] << "\\n";
  return 0;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  cin >> stX >> stY >> enX >> enY;

  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; j++)
      a[i][j] = s[j - 1];
  }

  dijs();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define f first
#define s second
#define matrix vector<vector<ll>>
#define zero(n, m) matrix(n, vector<ll>(m, 0))
#define one(n, m) matrix(n, vector<ll>(m, 1))
#define pii pair<ll, pair<ll, ll>>
const int N = 1e3 + 5;

ll n, m, stX, stY, enX, enY, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0},
                             cost[N][N];
char a[N][N];

bool isValid(int i, int j) {
  if (i < 1 || i > n || j < 1 || j > m || a[i][j] == '#')
    return 0;
  return 1;
}

ll dijs() {
  priority_queue<pii, vector<pii>, greater<pii>> q;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++)
      cost[i][j] = 1e18;
  }

  cost[stX][stY] = 0;
  q.push({0, {stX, stY}});

  while (q.size()) {
    ll i = q.top().s.f, j = q.top().s.s, magic = q.top().f;
    q.pop();
    if (cost[i][j] < magic)
      continue;

    for (int k = 0; k < 4; k++) {
      int ii = i + dx[k], jj = j + dy[k];
      if (isValid(ii, jj) && cost[ii][jj] > magic) {
        cost[ii][jj] = magic;
        q.push({cost[ii][jj], {ii, jj}});
      }
    }

    int st = i - 2, stj = j - 2, en = i + 2, enj = j + 2;
    for (int k = st; k <= en; k++) {
      for (int kk = stj; kk <= enj; kk++) {
        if (isValid(k, kk) && cost[k][kk] > magic + 1) {
          cost[k][kk] = magic + 1;
          q.push({cost[k][kk], {k, kk}});
        }
      }
    }
  }
  if (cost[enX][enY] == 1e18)
    cout << "-1\\n";
  else
    cout << cost[enX][enY] << "\\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  cin >> stX >> stY >> enX >> enY;

  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; j++)
      a[i][j] = s[j - 1];
  }

  dijs();

  return 0;
}