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

#include "bits/stdc++.h"

using namespace std;

#define endl "\\n"
typedef long long int ll;
#define f first
#define s second

inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
inline void setPrecision(int n) { cout.precision(n); }

// DEBUG
#define dbg(x) cerr << (#x) << ": " << x << endl
#define dbgV(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << it << " ";                                                         \\
  cerr << endl;
#define dbgS(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << it << " ";                                                         \\
  cerr << endl;
#define dbgM(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << "[" << it.f << ", " << it.s << "] ";                               \\
  cerr << endl;
#define dbg2D(x)                                                               \\
  cerr << (#x) << ": \\n";                                                      \\
  for (auto y : x) {                                                           \\
    for (auto it : y)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << endl;                                                              \\
  }                                                                            \\
  cerr << endl;
#define dbgA(x, n)                                                             \\
  cerr << (#x) << ": ";                                                        \\
  for (int i = 0; i < n; ++i)                                                  \\
    cerr << x[i] << " ";                                                       \\
  cerr << endl;
#define dbgVP(x)                                                               \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << "[" << it.f << ", " << it.s << "] ";                               \\
  cerr << endl;

ll INF = 1e10;
int MOD = 1e9 + 7;

int n, m;
int sx, sy;
int ex, ey;
vector<string> s;
vector<vector<int>> id;

const int mxid = 1e6 + 5;

inline bool valid(int i, int j) {
  return (i >= 0 && j >= 0 && i < n && j < m && s[i][j] == '.');
}

vector<pair<int, int>> wut[mxid];

void go(int x, int y, int it) {
  if (!valid(x, y) || (id[x][y] != -1))
    return;
  wut[it].push_back({x, y});
  id[x][y] = it;
  go(x + 1, y, it);
  go(x, y + 1, it);
  go(x - 1, y, it);
  go(x, y - 1, it);
}

vector<pair<int, int>> goTo(int i, int j) {
  int tx = i - 2, ty = j - 2;
  vector<pair<int, int>> can;
  for (int ox = 0; ox < 5; ++ox) {
    for (int oy = 0; oy < 5; ++oy)
      can.push_back({tx + ox, ty + oy});
  }
  return can;
}

vector<int> adj[mxid];
bool vis[mxid];

void dfs(int cur) {
  if (vis[cur])
    return;
  vis[cur] = 1;
  for (auto p : wut[cur]) {
    auto possi = goTo(p.f, p.s);
    for (auto could : possi) {
      if (!valid(could.f, could.s))
        continue;
      if (id[could.f][could.s] != cur) {
        adj[cur].push_back(id[could.f][could.s]);
        adj[id[could.f][could.s]].push_back(cur);
        dfs(id[could.f][could.s]);
      }
    }
  }
}

int bfs(int N) {
  list<pair<int, int>> trck;
  vector<bool> done(N);
  int st = id[sx][sy];
  int goal = id[ex][ey];
  trck.push_back({st, 0});
  done[0] = 1;
  while (!trck.empty()) {
    auto cur = trck.front();
    trck.pop_front();
    if (cur.f == goal)
      return cur.s;
    for (auto nxt : adj[cur.f]) {
      if (!done[nxt]) {
        done[nxt] = 1;
        trck.push_back({nxt, cur.s + 1});
      }
    }
  }
  return -1;
}

int main() {
  fastio();

  cin >> n >> m >> sx >> sy >> ex >> ey;
  --sx;
  --sy;
  --ex;
  --ey;
  s.resize(n);
  id.resize(n, vector<int>(m, -1));
  for (int i = 0; i < n; ++i)
    cin >> s[i];

  int it = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j)
      if (id[i][j] == -1) {
        go(i, j, it);
        ++it;
      }
  }

  int N = it - 1;
  // total no of nodes
  for (int i = 0; i < N; ++i) {
    if (!vis[i]) {
      dfs(i);
    }
  }

  cout << bfs(N);

  return 0;
}

/*

4 4
1 1
4 4
..#.
..#.
.#..
.#..

*/

Error: WA

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define endl "\\n"
typedef long long int ll;
#define f first
#define s second

inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
inline void setPrecision(int n) { cout.precision(n); }

// DEBUG
#define dbg(x) cerr << (#x) << ": " << x << endl
#define dbgV(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << it << " ";                                                         \\
  cerr << endl;
#define dbgS(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << it << " ";                                                         \\
  cerr << endl;
#define dbgM(x)                                                                \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << "[" << it.f << ", " << it.s << "] ";                               \\
  cerr << endl;
#define dbg2D(x)                                                               \\
  cerr << (#x) << ": \\n";                                                      \\
  for (auto y : x) {                                                           \\
    for (auto it : y)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << endl;                                                              \\
  }                                                                            \\
  cerr << endl;
#define dbgA(x, n)                                                             \\
  cerr << (#x) << ": ";                                                        \\
  for (int i = 0; i < n; ++i)                                                  \\
    cerr << x[i] << " ";                                                       \\
  cerr << endl;
#define dbgVP(x)                                                               \\
  cerr << (#x) << ": ";                                                        \\
  for (auto it : x)                                                            \\
    cerr << "[" << it.f << ", " << it.s << "] ";                               \\
  cerr << endl;

ll INF = 1e10;
int MOD = 1e9 + 7;

int n, m;
int sx, sy;
int ex, ey;
vector<string> s;
vector<vector<int>> id;

inline bool valid(int i, int j) {
  return (i >= 0 && j >= 0 && i < n && j < m && s[i][j] == '.');
}

vector<pair<int, int>> wut[1005];

void go(int x, int y, int it) {
  if (!valid(x, y) || (id[x][y] != -1))
    return;
  wut[it].push_back({x, y});
  id[x][y] = it;
  go(x + 1, y, it);
  go(x, y + 1, it);
  go(x - 1, y, it);
  go(x, y - 1, it);
}

vector<pair<int, int>> goTo(int i, int j) {
  int tx = i - 2, ty = j - 2;
  vector<pair<int, int>> can;
  for (int ox = 0; ox < 5; ++ox) {
    for (int oy = 0; oy < 5; ++oy)
      can.push_back({tx + ox, ty + oy});
  }
  return can;
}

vector<int> adj[1005];
bool vis[1005];

void dfs(int cur) {
  if (vis[cur])
    return;
  vis[cur] = 1;
  for (auto p : wut[cur]) {
    auto possi = goTo(p.f, p.s);
    for (auto could : possi) {
      if (!valid(could.f, could.s))
        continue;
      if (id[could.f][could.s] != cur) {
        adj[cur].push_back(id[could.f][could.s]);
        adj[id[could.f][could.s]].push_back(cur);
        dfs(id[could.f][could.s]);
      }
    }
  }
}

int bfs(int N) {
  list<pair<int, int>> trck;
  vector<bool> done(N);
  int st = id[sx][sy];
  int goal = id[ex][ey];
  trck.push_back({st, 0});
  done[0] = 1;
  while (!trck.empty()) {
    auto cur = trck.front();
    trck.pop_front();
    if (cur.f == goal)
      return cur.s;
    for (auto nxt : adj[cur.f]) {
      if (!done[nxt]) {
        done[nxt] = 1;
        trck.push_back({nxt, cur.s + 1});
      }
    }
  }
  return -1;
}

int main() {
  fastio();

  cin >> n >> m >> sx >> sy >> ex >> ey;
  --sx;
  --sy;
  --ex;
  --ey;
  s.resize(n);
  id.resize(n, vector<int>(m, -1));
  for (int i = 0; i < n; ++i)
    cin >> s[i];

  int it = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j)
      if (id[i][j] == -1) {
        go(i, j, it);
        ++it;
      }
  }

  int N = it - 1;
  // total no of nodes
  for (int i = 0; i < N; ++i) {
    if (!vis[i]) {
      dfs(i);
    }
  }

  cout << bfs(N);

  return 0;
}

/*

4 4
1 1
4 4
..#.
..#.
.#..
.#..

*/