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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

// aliases
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
template <class T> using Vvec = vector<vector<T>>;

// for debug
template <class T> void debug(T &v) {
  for (auto &x : v)
    cout << x << " ";
  cout << endl;
}

void bfs(vector<string> &s, Vvec<int> &dist, const int sh, const int sw) {
  const int H = s.size();
  const int W = s[0].size();
  deque<Pii> deq;
  deq.emplace_front(0, sh * W + sw);
  const int infty = 1e9;
  for (auto &v : dist) {
    for (auto &e : v)
      e = infty;
  }
  dist[sh][sw] = 0;

  const int dh[] = {1, 0, -1, 0};
  const int dw[] = {0, 1, 0, -1};
  auto isroad = [&](int h, int w) {
    return (h >= 0) && (h < H) && (w >= 0) && (w < W) && (s[h][w] == '.');
  };

  while (!deq.empty()) {
    Pii p = deq.front();
    deq.pop_front();
    int d = p.first;
    int h = p.second / W;
    int w = p.second % W;

    // walk
    for (int i = 0; i < 4; i++) {
      int nh = h + dh[i];
      int nw = w + dw[i];
      if (isroad(nh, nw) && dist[nh][nw] > d) {
        dist[nh][nw] = d;
        deq.emplace_front(d, nh * W + nw);
      }
    }

    // warp
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        int nh = h + i;
        int nw = w + j;
        if (isroad(nh, nw) && dist[nh][nw] > d + 1) {
          dist[nh][nw] = d + 1;
          deq.emplace_back(d + 1, nh * W + nw);
        }
      }
    }
  }
}

// answer
int main() {
  int H, W, sh, sw, th, tw;
  cin >> H >> W >> sh >> sw >> th >> tw;
  sh--;
  sw--;
  th--;
  tw--;
  vector<string> s(H);
  for (auto &e : s)
    cin >> e;

  Vvec<int> dist(H, vector<int>(W));
  bfs(s, dist, sh, sw);

  cout << ((dist[th][tw] > 1e8) ? -1 : dist[th][tw]) << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

// aliases
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
template <class T> using Vvec = vector<vector<T>>;

// for debug
template <class T> void debug(T &v) {
  for (auto &x : v)
    cout << x << " ";
  cout << endl;
}

void bfs(vector<string> &s, Vvec<int> &dist, const int sh, const int sw) {
  const int H = s.size();
  const int W = s[0].size();
  deque<Pii> deq;
  deq.emplace_front(0, sh * H + sw);
  const int infty = 1e9;
  for (auto &v : dist) {
    for (auto &e : v)
      e = infty;
  }
  dist[sh][sw] = 0;

  const int dh[] = {1, 0, -1, 0};
  const int dw[] = {0, 1, 0, -1};
  auto isroad = [&](int h, int w) {
    return (h >= 0) && (h <= H) && (w >= 0) && (w <= W) && (s[h][w] == '.');
  };

  while (!deq.empty()) {
    Pii p = deq.front();
    deq.pop_front();
    int d = p.first;
    int h = p.second / H;
    int w = p.second % H;

    // walk
    for (int i = 0; i < 4; i++) {
      int nh = h + dh[i];
      int nw = w + dw[i];
      if (isroad(nh, nw) && dist[nh][nw] > d) {
        dist[nh][nw] = d;
        deq.emplace_front(d, nh * H + nw);
      }
    }

    // warp
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        int nh = h + i;
        int nw = w + j;
        if (isroad(nh, nw) && dist[nh][nw] > d + 1) {
          dist[nh][nw] = d + 1;
          deq.emplace_back(d + 1, nh * H + nw);
        }
      }
    }
  }
}

// answer
int main() {
  int H, W, sh, sw, th, tw;
  cin >> H >> W >> sh >> sw >> th >> tw;
  sh--;
  sw--;
  th--;
  tw--;
  vector<string> s(H);
  for (auto &e : s)
    cin >> e;

  Vvec<int> dist(H, vector<int>(W));
  bfs(s, dist, sh, sw);

  cout << ((dist[th][tw] > 1e8) ? -1 : dist[th][tw]) << endl;
}