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

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, -1, 0, 1};
int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));

  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    rep(v, 4) {
      int ni = i + di[v];
      int nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }

    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }

  int ans = dist[ti][tj];
  if (ans == INF)
    cout << -1;
  else
    cout << ans;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;

int h, w;
int ch, cw;
int dh, dw;
char s[1005][1005];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int D[1005][1005];
int d[1000000];
bool used[1000000];
int start, goal;
int k = -1;
vector<P> G[1000000];
void dfs(int x, int y) {
  if (x == ch && y == cw)
    start = k;
  if (x == dh && y == dw)
    goal = k;
  s[x][y] = '#';
  G[k].push_back(P(x, y));
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < h && 0 <= ny && ny < w) {
      if (s[nx][ny] == '.')
        dfs(nx, ny);
    }
  }
}

int main() {
  rep(i, 1005) {
    rep(j, 1005) { D[i][j] = -1; }
  }
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, h) {
    rep(j, w) { cin >> s[i][j]; }
  }

  int res = 0;

  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '.') {
        k++;
        dfs(i, j);
        res++;
      }
    }
  }

  for (int i = 0; i <= k; i++) {
    for (int j = 0; j <= k; j++) {
      for (ll m = 0; m < (ll)G[i].size(); m++) {
        for (ll n = 0; n < (ll)G[j].size(); n++) {
          if (abs(G[i][m].first - G[j][n].first) <= 2 &&
              abs(G[i][m].second - G[j][n].second) <= 2) {
            D[i][j] = 1;
            continue;
          }
        }
      }
    }
  }

  // 始点から各島への最短距離を求める
  fill(d, d + k + 1, 3000);
  fill(used, used + k + 1, false);
  d[start] = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u <= k; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;
    for (int u = 0; u <= k; u++) {
      if (D[v][u] > 0)
        d[u] = min(d[u], d[v] + D[v][u]);
    }
  }
  if (start == goal)
    cout << 0;
  else if (d[goal] != 3000)
    cout << d[goal];
  else
    cout << -1;
  return 0;
}