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

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007, INF = (1LL << 60);
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

int main() {
  /*cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);*/
  ll h, w;
  cin >> h >> w;
  ll g[4];
  for (int i = 0; i < 4; i++) {
    cin >> g[i];
    g[i]--;
  }
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  vector<vector<ll>> dis(h, vector<ll>(w, INF));
  deque<pair<ll, pair<ll, ll>>> q;
  pair<ll, pair<ll, ll>> niko;
  q.push_back({0LL, {g[0], g[1]}});
  ll dy[24] = {0, 0, -1, 1, -2, -2, -2, -2, -2, 2,  2, 2,
               2, 2, 1,  1, 1,  1,  -1, -1, -1, -1, 0, 0};
  ll dx[24] = {-1, 1, 0,  0,  -2, -1, 0,  1,  2, -2, -1, 0,
               1,  2, -2, -1, 1,  2,  -2, -1, 1, 2,  -2, 2};
  while (!q.empty()) {
    niko = q.front();
    ll y = niko.second.first;
    ll x = niko.second.second;
    ll d = niko.first;
    q.pop_front();
    if (d > dis[y][x])
      continue;
    for (int k = 0; k < 4; k++) {
      ll nowy = y + dy[k];
      ll nowx = x + dx[k];
      if (nowy < 0 || nowy >= h || nowx < 0 || nowx >= w ||
          s[nowy][nowx] == '#')
        continue;
      if (dis[nowy][nowx] > d) {
        dis[nowy][nowx] = d;
        q.push_front({d, {nowy, nowx}});
      }
    }
    for (int k = 4; k < 24; k++) {
      ll nowy = y + dy[k];
      ll nowx = x + dx[k];
      if (nowy < 0 || nowy >= h || nowx < 0 || nowx >= w ||
          s[nowy][nowx] == '#')
        continue;
      if (dis[nowy][nowx] > d + 1) {
        dis[nowy][nowx] = d + 1;
        q.push_back({d + 1, {nowy, nowx}});
      }
    }
  }
  ll ans = dis[g[2]][g[3]];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007, INF = (1LL << 60);
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll h, w;
  cin >> h >> w;
  ll g[4];
  for (int i = 0; i < 4; i++) {
    cin >> g[i];
    g[i]--;
  }
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  vector<vector<ll>> dis(h, vector<ll>(w, INF));
  deque<pair<ll, pair<ll, ll>>> q;
  pair<ll, pair<ll, ll>> niko;
  q.push_back({0LL, {g[0], g[1]}});
  ll dy[24] = {0, 0, -1, 1, -2, -2, -2, -2, -2, 2,  2, 2,
               2, 2, 1,  1, 1,  1,  -1, -1, -1, -1, 0, 0};
  ll dx[24] = {-1, 1, 0,  0,  -2, -1, 0,  1,  2, -2, -1, 0,
               1,  2, -2, -1, 1,  2,  -2, -1, 1, 2,  -2, 2};
  while (!q.empty()) {
    niko = q.front();
    ll y = niko.second.first;
    ll x = niko.second.second;
    ll d = niko.first;
    q.pop_front();
    if (d > dis[y][x])
      continue;
    for (int k = 0; k < 4; k++) {
      ll nowy = y + dy[k];
      ll nowx = x + dx[k];
      if (nowy < 0 || nowy >= h || nowx < 0 || nowx >= w ||
          s[nowy][nowx] == '#')
        continue;
      if (dis[nowy][nowx] > d) {
        dis[nowy][nowx] = d;
        q.push_front({d, {nowy, nowx}});
      }
    }
    for (int k = 4; k < 25; k++) {
      ll nowy = y + dy[k];
      ll nowx = x + dx[k];
      if (nowy < 0 || nowy >= h || nowx < 0 || nowx >= w ||
          s[nowy][nowx] == '#')
        continue;
      if (dis[nowy][nowx] > d + 1) {
        dis[nowy][nowx] = d + 1;
        q.push_back({d + 1, {nowy, nowx}});
      }
    }
  }
  ll ans = dis[g[2]][g[3]];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}