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

#include <bits/stdc++.h>
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1001001001;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<int>> dist(h, vector<int>(w, INF));

  deque<P> q;
  q.emplace_back(ch, cw);
  dist[ch][cw] = 0;

  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();

    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dist[nx][ny] <= dist[x][y])
        continue;
      dist[nx][ny] = dist[x][y];
      q.emplace_front(nx, ny);
    }

    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (dist[nx][ny] <= dist[x][y] + 1)
          continue;
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace_back(nx, ny);
      }
    }
  }

  int ans = dist[dh][dw];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1001001001;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

vector<vector<int>> ids;
int h, w;
vector<string> s;
int graph[2010][2010];

void dfs(int x, int y, int id) {
  ids[x][y] = id;

  rep(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < h && 0 <= ny && ny < w) {
      if (s[nx][ny] == '#')
        continue;
      if (ids[nx][ny] != -1)
        continue;
      dfs(nx, ny, id);
    }
  }
}

int main() {
  cin >> h >> w;
  int ch, cw, dh, dw;
  s.resize(h);
  cin >> ch >> cw >> dh >> dw;
  rep(i, h) cin >> s[i];

  ch--;
  cw--;
  dh--;
  dw--;

  ids.resize(h, vector<int>(w, -1));
  int max_id = 0;

  rep(i, h) {
    rep(j, w) {
      if (ids[i][j] == -1 && s[i][j] == '.') {
        dfs(i, j, max_id);
        max_id++;
      }
    }
  }

  rep(i, h) rep(j, w) {
    int cur_id = ids[i][j];
    if (cur_id == -1 || s[i][j] == '#')
      continue;

    for (int x = -2; x <= 2; ++x) {
      for (int y = -2; y <= 2; ++y) {

        int nx = i + x;
        int ny = j + y;
        if (0 <= nx && nx < h && 0 <= ny && ny < w) {
          int next_id = ids[nx][ny];
          if (next_id == -1 || s[nx][ny] == '#')
            continue;
          graph[cur_id][next_id] = 1;
        }
      }
    }
  }

  queue<int> q;
  int start = ids[ch][cw];
  int goal = ids[dh][dw];
  q.push(start);
  vector<int> dist(max_id, INF);
  dist[start] = 0;

  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int cur_id = p;
    rep(next_id, max_id) {
      if (graph[cur_id][next_id] == 1) {
        int next_dist = dist[cur_id] + 1;
        if (next_dist < dist[next_id]) {
          q.push(next_id);
          dist[next_id] = next_dist;
        }
      }
    }
  }

  int ans = dist[goal];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}