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

#include <bits/stdc++.h>
using namespace std;

template <typename T, typename U> using pv = vector<pair<T, U>>;
template <typename T> using matrix = vector<vector<T>>;
template <typename T> using pque = priority_queue<T>;
template <typename T> using lpque = priority_queue<T, vector<T>, greater<T>>;

using ll = long long;
using intpair = pair<int, int>;
using llpair = pair<ll, ll>;
using ilpair = pair<int, ll>;
using lipair = pair<ll, int>;
using intvec = vector<int>;
using llvec = vector<ll>;
using intq = queue<int>;
using llq = queue<ll>;
using intmat = vector<intvec>;
using llmat = vector<llvec>;

#define PI 3.141592653589793
#define INTINF ((1 << 30) - 1)
#define LLINF ((1LL << 62) - 1)
#define MPRIME 1000000007
#define MPRIME9 998244353
#define MMPRIME ((1ll << 61) - 1)

#define len length()
#define pushb push_back
#define fi first
#define se second

#define setpr fixed << setprecision(15)
#define all(name) name.begin(), name.end()
#define rall(name) name.rbegin(), name.rend()
#define gsort(vbeg, vend) sort(vbeg, vend, greater<>())

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

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

template <class T> inline void init(T &v) {
  for (auto &a : v)
    cin >> a;
}
template <class T, class U> inline void init(vector<pair<T, U>> &v) {
  for (auto &a : v)
    cin >> a.first >> a.second;
}
template <class T, class N> inline void init(T &v, N n) {
  v.resize(n);
  for (auto &a : v)
    cin >> a;
}
template <class T, class U, class N>
inline void init(vector<pair<T, U>> &v, N n) {
  v.resize(n);
  for (auto &a : v)
    cin >> a.first >> a.second;
}

inline void out() { cout << endl; }
template <class T, class... U> inline void out(T a, U... alist) {
  cout << a << " ";
  out(forward<U>(alist)...);
}

template <class N> void resiz(N n) {
  // empty
}
template <class N, class T, class... U> void resiz(N n, T &&hd, U &&...tl) {
  hd.resize(n);
  resiz(n, forward<U>(tl)...);
}

bool isout(int h, int w, int H, int W) {
  return (h < 0 || h >= H || w < 0 || w >= W);
}

ll binpow(ll a, ll ex, ll p) {
  ll result = 1;
  while (ex > 0) {
    if (ex & 1)
      result = result * a % p;
    ex >>= 1;
    a = a * a % p;
  }
  return result;
}

template <class T> class Matrix {
  vector<vector<T>> mat;

public:
  Matrix() : Matrix(0, 0) {}
  Matrix(int h, int w) { make(h, w); }
  Matrix(int h, int w, T init) { make(h, w, init); }

  void make(int h, int w) { mat = vector<vector<T>>(h, vector<T>(w)); }
  void make(int h, int w, T init) {
    mat = vector<vector<T>>(h, vector<T>(w, init));
  };

  void in() {
    for (int i = 0; i < mat.size(); i++)
      for (int j = 0; j < mat[i].size(); j++) {
        cin >> mat[i][j];
      }
  }

  void out() {
    for (int i = 0; i < mat.size(); i++) {
      int wm = mat[i].size();
      for (int j = 0; j < wm; j++) {
        cout << mat[i][j] << (wm == j + 1 ? '\\n' : ' ');
      }
    }
    cout << flush;
  }

  inline vector<T> &operator[](int idx) {
    assert(0 <= idx && idx < mat.size());
    return mat[idx];
  }
};

int H, W, sh, sw, gh, gw;
Matrix<char> mp(1001, 1001, '#');
Matrix<int> cost(1001, 1001, INTINF);
int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
int ddy[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
             0,  1,  1,  1,  1,  2,  2,  2,  2,  2},
    ddx[] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
             2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

void input() {
  cin >> H >> W >> sh >> sw >> gh >> gw;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++) {
      cin >> mp[i][j];
    }
}

void solve() {
  intq qh, qw, qqh, qqw;
  qh.push(sh), qw.push(sw);
  cost[sh][sw] = 0;
  while (!qh.empty() || !qqh.empty()) {
    while (!qh.empty()) {
      int h = qh.front(), w = qw.front();
      qh.pop(), qw.pop();
      for (int i = 0; i < 4; i++) {
        int nh = h + dy[i], nw = w + dx[i];
        if (nh < 0 || nh > H || nw < 0 || nw > W || mp[nh][nw] == '#' ||
            cost[nh][nw] <= cost[h][w])
          continue;
        cost[nh][nw] = cost[h][w];
        qh.push(nh), qw.push(nw);
        qqh.push(nh), qqw.push(nw);
      }
      qqh.push(h), qqw.push(w);
    }
    while (!qqh.empty()) {
      int h = qqh.front(), w = qqw.front();
      qqh.pop(), qqw.pop();
      for (int i = 0; i < 20; i++) {
        int nh = h + ddy[i], nw = w + ddx[i];
        if (nh < 0 || nh > H || nw < 0 || nw > W || mp[nh][nw] == '#' ||
            cost[nh][nw] <= cost[h][w] + 1)
          continue;
        cost[nh][nw] = cost[h][w] + 1;
        qh.push(nh), qw.push(nw);
      }
    }
  }
  cout << (cost[gh][gw] == INTINF ? -1 : cost[gh][gw]) << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int t = 1;
  while (t) {
    input();
    solve();
    t--;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <typename T, typename U> using pv = vector<pair<T, U>>;
template <typename T> using matrix = vector<vector<T>>;
template <typename T> using pque = priority_queue<T>;
template <typename T> using lpque = priority_queue<T, vector<T>, greater<T>>;

using ll = long long;
using intpair = pair<int, int>;
using llpair = pair<ll, ll>;
using ilpair = pair<int, ll>;
using lipair = pair<ll, int>;
using intvec = vector<int>;
using llvec = vector<ll>;
using intq = queue<int>;
using llq = queue<ll>;
using intmat = vector<intvec>;
using llmat = vector<llvec>;

#define PI 3.141592653589793
#define INTINF ((1 << 30) - 1)
#define LLINF ((1LL << 62) - 1)
#define MPRIME 1000000007
#define MPRIME9 998244353
#define MMPRIME ((1ll << 61) - 1)

#define len length()
#define pushb push_back
#define fi first
#define se second

#define setpr fixed << setprecision(15)
#define all(name) name.begin(), name.end()
#define rall(name) name.rbegin(), name.rend()
#define gsort(vbeg, vend) sort(vbeg, vend, greater<>())

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

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

template <class T> inline void init(T &v) {
  for (auto &a : v)
    cin >> a;
}
template <class T, class U> inline void init(vector<pair<T, U>> &v) {
  for (auto &a : v)
    cin >> a.first >> a.second;
}
template <class T, class N> inline void init(T &v, N n) {
  v.resize(n);
  for (auto &a : v)
    cin >> a;
}
template <class T, class U, class N>
inline void init(vector<pair<T, U>> &v, N n) {
  v.resize(n);
  for (auto &a : v)
    cin >> a.first >> a.second;
}

inline void out() { cout << endl; }
template <class T, class... U> inline void out(T a, U... alist) {
  cout << a << " ";
  out(forward<U>(alist)...);
}

template <class N> void resiz(N n) {
  // empty
}
template <class N, class T, class... U> void resiz(N n, T &&hd, U &&...tl) {
  hd.resize(n);
  resiz(n, forward<U>(tl)...);
}

bool isout(int h, int w, int H, int W) {
  return (h < 0 || h >= H || w < 0 || w >= W);
}

ll binpow(ll a, ll ex, ll p) {
  ll result = 1;
  while (ex > 0) {
    if (ex & 1)
      result = result * a % p;
    ex >>= 1;
    a = a * a % p;
  }
  return result;
}

template <class T> class Matrix {
  vector<vector<T>> mat;

public:
  Matrix() : Matrix(0, 0) {}
  Matrix(int h, int w) { make(h, w); }
  Matrix(int h, int w, T init) { make(h, w, init); }

  void make(int h, int w) { mat = vector<vector<T>>(h, vector<T>(w)); }
  void make(int h, int w, T init) {
    mat = vector<vector<T>>(h, vector<T>(w, init));
  };

  void in() {
    for (int i = 0; i < mat.size(); i++)
      for (int j = 0; j < mat[i].size(); j++) {
        cin >> mat[i][j];
      }
  }

  void out() {
    for (int i = 0; i < mat.size(); i++) {
      int wm = mat[i].size();
      for (int j = 0; j < wm; j++) {
        cout << mat[i][j] << (wm == j + 1 ? '\\n' : ' ');
      }
    }
    cout << flush;
  }

  inline vector<T> &operator[](int idx) {
    assert(0 <= idx && idx < mat.size());
    return mat[idx];
  }
};

int H, W, sh, sw, gh, gw;
Matrix<char> mp(1001, 1001, '#');
Matrix<int> cost(1001, 1001, INTINF);
int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
int ddy[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
             0,  1,  1,  1,  1,  2,  2,  2,  2,  2},
    ddx[] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
             2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

void input() {
  cin >> H >> W >> sh >> sw >> gh >> gw;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++) {
      cin >> mp[i][j];
    }
}

void solve() {
  intq qh, qw, qqh, qqw;
  qh.push(sh), qqw.push(sw);
  cost[sh][sw] = 0;
  while (!qh.empty() || !qqh.empty()) {
    while (!qh.empty()) {
      int h = qh.front(), w = qw.front();
      qh.pop(), qw.pop();
      for (int i = 0; i < 4; i++) {
        int nh = h + dy[i], nw = w + dx[i];
        if (nh < 0 || nh > H || nw < 0 || nw > W || mp[nh][nw] == '#' ||
            cost[nh][nw] <= cost[h][w])
          continue;
        cost[nh][nw] = cost[h][w];
        qh.push(nh), qw.push(nw);
        qqh.push(nh), qqw.push(nw);
      }
      qqh.push(h), qqw.push(w);
    }
    while (!qqh.empty()) {
      int h = qqh.front(), w = qqw.front();
      qqh.pop(), qqw.pop();
      for (int i = 0; i < 20; i++) {
        int nh = h + ddy[i], nw = w + ddx[i];
        if (nh < 0 || nh > H || nw < 0 || nw > W || mp[nh][nw] == '#' ||
            cost[nh][nw] <= cost[h][w] + 1)
          continue;
        cost[nh][nw] = cost[h][w] + 1;
        qh.push(nh), qw.push(nw);
      }
    }
  }
  cout << (cost[gh][gw] == INTINF ? -1 : cost[gh][gw]) << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int t = 1;
  while (t) {
    input();
    solve();
    t--;
  }
}