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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;
template <typename T> struct Edge {
  int u, v;
  T cost;
  Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
  bool operator<(const Edge &e) const { return cost < e.cost; }
};

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;

ll ans;
string S[1005];
int dw[] = {0, 0, -1, 1}, dh[] = {-1, 1, 0, 0};
int dp[1005][1005];
queue<P> Q;
int H, W;

void bfs(int h, int w) {
  queue<P> q;
  q.push(mp(h, w));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int sh = p.first, sw = p.second;
    rep(i, -2, 3) rep(j, -2, 3) {
      if (sh + i < 0 || sh + i >= H || sw + j < 0 || sw + j >= W)
        continue;
      if (dp[sh + i][sw + j] > dp[sh][sw] + 1 && S[sh + i][sw + j] != '#') {
        Q.push(mp(sh + i, sw + j));
        dp[sh + i][sw + j] = dp[sh][sw] + 1;
      }
    }
    rep(r, 0, 4) {
      int th = sh + dh[r], tw = sw + dw[r];
      if (tw < 0 || th < 0 || tw >= W || th >= H || S[th][tw] == '#')
        continue;
      if (dp[th][tw] <= dp[sh][sw])
        continue;
      dp[th][tw] = dp[sh][sw];
      q.push(mp(th, tw));
    }
  }
}

int main(void) {
  int Ch, Cw, Dh, Dw;
  cin >> H >> W;
  cin >> Ch >> Cw;
  Ch--;
  Cw--;
  cin >> Dh >> Dw;
  Dh--;
  Dw--;
  rep(i, 0, H) cin >> S[i];

  rep(i, 0, H) rep(j, 0, W) dp[i][j] = INF;

  Q.push(mp(Ch, Cw));
  dp[Ch][Cw] = 0;

  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    bfs(p.first, p.second);
  }

  if (dp[Dh][Dw] != INF)
    cout << dp[Dh][Dw] << endl;
  else
    cout << -1 << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;
template <typename T> struct Edge {
  int u, v;
  T cost;
  Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
  bool operator<(const Edge &e) const { return cost < e.cost; }
};

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;

ll ans;
string S[1005];
int dw[] = {0, 0, -1, 1}, dh[] = {-1, 1, 0, 0};
int dp[1005][1005];
queue<P> Q;
int H, W;

void bfs(int h, int w) {
  queue<P> q;
  q.push(mp(h, w));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int sh = p.first, sw = p.second;
    rep(i, -2, 3) rep(j, -2, 3) {
      if (dp[sh + i][sw + j] > dp[sh][sw] + 1 && S[sh + i][sw + j] != '#') {
        Q.push(mp(sh + i, sw + j));
        dp[sh + i][sw + j] = dp[sh][sw] + 1;
      }
    }
    rep(r, 0, 4) {
      int th = sh + dh[r], tw = sw + dw[r];
      if (tw < 0 || th < 0 || tw >= W || th >= H || S[th][tw] == '#')
        continue;
      if (dp[th][tw] <= dp[sh][sw])
        continue;
      dp[th][tw] = dp[sh][sw];
      q.push(mp(th, tw));
    }
  }
}

int main(void) {
  int Ch, Cw, Dh, Dw;
  cin >> H >> W;
  cin >> Ch >> Cw;
  Ch--;
  Cw--;
  cin >> Dh >> Dw;
  Dh--;
  Dw--;
  rep(i, 0, H) cin >> S[i];

  rep(i, 0, H) rep(j, 0, W) dp[i][j] = INF;

  Q.push(mp(Ch, Cw));
  dp[Ch][Cw] = 0;

  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    bfs(p.first, p.second);
  }

  if (dp[Dh][Dw] != INF)
    cout << dp[Dh][Dw] << endl;
  else
    cout << -1 << endl;
  return 0;
}