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

#include "bits/stdc++.h"
using namespace std;

#define fi first
#define se second
#define pb push_back
// #define int long long
#define all(x) (x).begin(), (x).end()

using LL = long long;
using LD = long double;
using pii = pair<int, int>;
using vii = vector<pii>;

// const int INF = 1e18;
const int MOD = 1e9 + 7;
const int N = 1005;

int ctr;
int n, m;
int cx, cy;
int dx, dy;
char s[N][N];
int a[N][N];
int cc[N][N];
int st, en;

vector<int> vis(1000001);
vector<int> g[1000001];

void go(int x, int y) {
  cc[x][y] = ctr;
  if (x == cx and y == cy)
    st = ctr;
  if (x == dx and y == dy)
    en = ctr;
  if (x + 1 <= n and s[x + 1][y] == '.' and !cc[x + 1][y])
    go(x + 1, y);
  if (y + 1 <= m and s[x][y + 1] == '.' and !cc[x][y + 1])
    go(x, y + 1);
  if (x - 1 >= 1 and s[x - 1][y] == '.' and !cc[x - 1][y])
    go(x - 1, y);
  if (y - 1 >= 1 and s[x][y - 1] == '.' and !cc[x][y - 1])
    go(x, y - 1);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> m;
  cin >> cx >> cy;
  cin >> dx >> dy;

  for (int i = 1; i <= n; i++) {
    cin >> (s[i] + 1);
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '#' or cc[i][j])
        continue;
      ++ctr;
      go(i, j);
    }
  }

  if (st == 0 or en == 0) {
    cout << "-1";
    return 0;
  }

  if (st == en) {
    cout << "0";
    return 0;
  }

  map<pii, bool> done;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '#')
        continue;
      for (int ii = -2; ii <= 2; ii++) {
        for (int jj = -2; jj <= 2; jj++) {
          if (1 <= i - ii and i - ii <= n and 1 <= j - jj and j - jj <= m and
              cc[i][j] != cc[i - ii][j - jj] and cc[i - ii][j - jj]) {
            int x = cc[i][j];
            int y = cc[i - ii][j - jj];
            if (done[{x, y}])
              continue;
            g[x].pb(y);
            done[{x, y}] = 1;
          }
        }
      }
    }
  }

  // for (int i = 1; i <= n; i++) {
  // for (int j = 1; j <= m; j++) {
  // cout << cc[i][j] << " ";
  //}
  // cout << "\\n";
  //}

  // for (int i = 1; i <= 5; i++) {
  // cout << "g[" << i << "]: ";
  // for (int j: g[i]) {
  // cout << j << " ";
  //}
  // cout << "\\n";
  //}

  queue<pii> Q;
  Q.push({st, 0});
  vis[st] = 1;

  while (!Q.empty()) {
    pii p = Q.front();
    if (p.fi == en) {
      cout << p.se;
      return 0;
    }
    Q.pop();

    for (int y : g[p.fi]) {
      if (!vis[y]) {
        vis[y] = 1;
        Q.push({y, p.se + 1});
      }
    }
  }

  cout << "-1";
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define fi first
#define se second
#define pb push_back
#define int long long
#define all(x) (x).begin(), (x).end()

using LL = long long;
using LD = long double;
using pii = pair<int, int>;
using vii = vector<pii>;

const int INF = 1e18;
const int MOD = 1e9 + 7;
const int N = 1005;

int ctr;
int n, m;
int cx, cy;
int dx, dy;
char s[N][N];
int a[N][N];
int cc[N][N];
int st, en;

vector<int> vis(1000001);
vector<int> g[1000001];

void go(int x, int y) {
  cc[x][y] = ctr;
  if (x == cx and y == cy)
    st = ctr;
  if (x == dx and y == dy)
    en = ctr;
  if (x + 1 <= n and s[x + 1][y] == '.' and !cc[x + 1][y])
    go(x + 1, y);
  if (y + 1 <= m and s[x][y + 1] == '.' and !cc[x][y + 1])
    go(x, y + 1);
  if (x - 1 >= 1 and s[x - 1][y] == '.' and !cc[x - 1][y])
    go(x - 1, y);
  if (y - 1 >= 1 and s[x][y - 1] == '.' and !cc[x][y - 1])
    go(x, y - 1);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> m;
  cin >> cx >> cy;
  cin >> dx >> dy;

  for (int i = 1; i <= n; i++) {
    cin >> (s[i] + 1);
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '#' or cc[i][j])
        continue;
      ++ctr;
      go(i, j);
    }
  }

  if (st == 0 or en == 0) {
    cout << "-1";
    return 0;
  }

  if (st == en) {
    cout << "0";
    return 0;
  }

  map<pii, bool> done;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '#')
        continue;
      for (int ii = -2; ii <= 2; ii++) {
        for (int jj = -2; jj <= 2; jj++) {
          if (cc[i][j] != cc[i - ii][j - jj] and cc[i - ii][j - jj]) {
            int x = cc[i][j];
            int y = cc[i - ii][j - jj];
            if (done[{x, y}])
              continue;
            g[x].pb(y);
            done[{x, y}] = 1;
          }
        }
      }
    }
  }
  /*
  for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
          cout << cc[i][j] << " ";
      }
      cout << "\\n";
  }

  for (int i = 1; i <= 5; i++) {
      cout << "g[" << i << "]: ";
      for (int j: g[i]) {
          cout << j << " ";
      }
      cout << "\\n";
  }
  */
  queue<pii> Q;
  Q.push({st, 0});
  vis[st] = 1;

  while (!Q.empty()) {
    pii p = Q.front();
    if (p.fi == en) {
      cout << p.se;
      return 0;
    }
    Q.pop();

    for (int y : g[p.fi]) {
      if (!vis[y]) {
        vis[y] = 1;
        Q.push({y, p.se + 1});
      }
    }
  }

  cout << "-1";
}