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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int h, w;
int ch, cw;
int dh, dw;
char s[1000][1000];
int g[1000][1000];
struct qs {
  int y;
  int x;
  int d;
};
set<pair<pair<int, int>, int>> q;

void q_push(int y, int x, int d) {
  if (y < 0 || y >= h || x < 0 || x >= w || s[y][x] == '#' ||
      (g[y][x] >= 0 && g[y][x] < d))
    return;
  // printf("q_push(%d, %d, %d)\\n", y, x, d);
  q.insert(make_pair(make_pair(y, x), d));
}

int main() {

  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
      g[i][j] = -1;
    }
  }

  q_push(ch - 1, cw - 1, 0);
  while (q.size() > 0) {
    int x, y, d;
    auto it = q.begin();
    d = it->second;
    y = it->first.first;
    x = it->first.second;
    q.erase(*it);
    if (y < 0 || y >= h || x < 0 || x >= w || s[y][x] == '#' ||
        (g[y][x] >= 0 && g[y][x] <= d))
      continue;
    // printf("g[%d][%d] = %d\\n", y, x, d);
    g[y][x] = d;

    q_push(y - 1, x, d);
    q_push(y + 1, x, d);
    q_push(y, x - 1, d);
    q_push(y, x + 1, d);

    q_push(y - 1, x - 1, d + 1);
    q_push(y - 1, x + 1, d + 1);
    q_push(y + 1, x - 1, d + 1);
    q_push(y + 1, x + 1, d + 1);

    for (int i = -2; i <= 2; i++) {
      q_push(y + i, x - 2, d + 1);
      q_push(y + i, x + 2, d + 1);
    }
    for (int i = -1; i <= 1; i++) {
      q_push(y - 2, x + i, d + 1);
      q_push(y + 2, x + i, d + 1);
    }
  }

  cout << g[dh - 1][dw - 1] << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int h, w;
int ch, cw;
int dh, dw;
char s[1000][1000];
int g[1000][1000];
bool cnt[20000][20000];
set<int> path;
int dist[20000];

void draw(int x, int y, int gn) {
  if (x < 0 || x >= h || y < 0 || y >= w || s[x][y] == '#' || g[x][y] != 0)
    return;
  g[x][y] = gn;
  draw(x, y + 1, gn);
  draw(x, y - 1, gn);
  draw(x + 1, y, gn);
  draw(x - 1, y, gn);
}
void calc(int g, int gn) {
  for (int i = 1; i <= gn; i++) {
    if (i == g)
      continue;
    if (dist[i] > 0)
      continue;
    // printf("cnt[%d][%d] = %d\\n", g, i, cnt[g][i]);
    if (cnt[g][i]) {
      // printf("dist[%d] = dist[%d]+1(%d)\\n", i, g, dist[g]+1);
      dist[i] = dist[g] + 1;
      calc(i, gn);
    }
  }
}
int main() {

  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
    }
  }

  int gn = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#' || g[i][j] != 0)
        continue;
      if (i == 0 || s[i - 1][j] == '#') {
        if (j == 0 || s[i][j - 1] == '#') {
          draw(i, j, gn);
          gn++;
        }
      }
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (g[i][j] == 0)
        continue;
      for (int dx = -2; dx <= 2; dx++) {
        if (j + dx < 0)
          continue;
        if (j + dx >= w)
          break;
        for (int dy = -2; dy <= 2; dy++) {
          if (i + dy < 0)
            continue;
          if (i + dy >= h)
            break;
          if (g[i + dy][j + dx] == 0)
            continue;
          if (cnt[g[i][j]][g[i + dy][j + dx]])
            continue;
          if (g[i][j] != g[i + dy][j + dx]) {
            cnt[g[i][j]][g[i + dy][j + dx]] = true;
            cnt[g[i + dy][j + dx]][g[i][j]] = true;
          }
        }
      }
    }
  }

  gn--;

  for (int i = 0; i < gn; i++) {
    dist[i] = 0;
  }
  calc(g[ch - 1][cw - 1], gn);

  /*
  for (int i=0; i<h; i++) {
    for (int j=0; j<w; j++) {
      printf("%2d ", g[i][j]);
    }
    printf("\\n");
  }

  for (int i=1; i<gn; i++) {
    for (int j=i+1; j<=gn; j++) {
      printf("cnt[%d][%d] = %d\\n", i, j, cnt[i][j]);
    }
  }

printf("g[%d][%d] = %d\\n", dh, dw, g[dh-1][dw-1]);

    */
  if (g[ch - 1][cw - 1] == g[dh - 1][dw - 1]) {
    printf("0\\n");
    return 0;
  }
  if (dist[g[dh - 1][dw - 1]] == 0) {
    printf("-1\\n");
    return 0;
  }
  cout << dist[g[dh - 1][dw - 1]] << endl;
  return 0;
}