Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// MACROS
#define _crt_secure_no_warnings
#define FREEELO                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F
#define MAX 1001
#define MOD 1000000007
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> usmi;
int n, m, cnt = 0;
int sx, sy, fx, fy;
string g[MAX];
bool used[MAX][MAX];
int comp[MAX][MAX];
int dist[MAX];
vector<vector<int>> v;
int check(int i, int j) {
  if (i <= 0 || i > n || j <= 0 || j > m)
    return 0;
  return 1;
}

void dfs(int i, int j) {
  if (check(i, j) == 0)
    return;
  comp[i][j] = cnt;
  used[i][j] = 1;
  if (!used[i + 1][j] && g[i + 1][j] != '#')
    dfs(i + 1, j);
  if (!used[i - 1][j] && g[i - 1][j] != '#')
    dfs(i - 1, j);
  if (!used[i][j + 1] && g[i][j + 1] != '#')
    dfs(i, j + 1);
  if (!used[i][j - 1] && g[i][j - 1] != '#')
    dfs(i, j - 1);
}

int bfs(int source, int final) {
  dist[source] = 0;
  queue<int> q;
  q.push(source);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int &to : v[cur]) {
      if (dist[to] == -1) {
        dist[to] = dist[cur] + 1;
        q.push(to);
      }
    }
  }
  return dist[final];
}

int main() {
  FREEELO
  cin >> n >> m;
  cin >> sx >> sy >> fx >> fy;
  for (int i = 1; i <= n; i++) {
    cin >> g[i];
    g[i] = " " + g[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (comp[i][j] == 0 && g[i][j] != '#' && used[i][j] == 0) {
        cnt++;
        dfs(i, j);
      }
    }
  }
  map<ii, int> edge;
  v.resize(cnt + 2);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (g[i][j] == '#')
        continue;
      for (int k = i - 2; k <= i + 2; k++) {
        for (int l = j - 2; l <= j + 2; l++) {
          if (check(k, l) && g[k][l] == '.') {
            if (edge[{comp[k][l], comp[i][j]}] == 0 &&
                comp[k][l] != comp[i][j]) {
              edge[{comp[k][l], comp[i][j]}] = 1;
              edge[{comp[i][j], comp[k][l]}] = 1;
              v[comp[i][j]].pb(comp[k][l]);
              v[comp[k][l]].pb(comp[i][j]);
            }
          }
        }
      }
    }
  }
  int source = comp[sx][sy];
  int final = comp[fx][fy];
  memset(dist, -1, sizeof(dist));
  cout << bfs(source, final) << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// MACROS
#define _crt_secure_no_warnings
#define FREEELO                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F
#define MAX 1001
#define MOD 1000000007
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> usmi;
int n, m, cnt = 0;
int sx, sy, fx, fy;
string g[MAX];
bool used[MAX][MAX];
int comp[MAX][MAX];
int dist[MAX * MAX];
vector<vector<int>> v;
int check(int i, int j) {
  if (i <= 0 || i > n || j <= 0 || j > m)
    return 0;
  return 1;
}

void dfs(int i, int j) {
  if (check(i, j) == 0)
    return;
  comp[i][j] = cnt;
  used[i][j] = 1;
  if (!used[i + 1][j] && g[i + 1][j] != '#')
    dfs(i + 1, j);
  if (!used[i - 1][j] && g[i - 1][j] != '#')
    dfs(i - 1, j);
  if (!used[i][j + 1] && g[i][j + 1] != '#')
    dfs(i, j + 1);
  if (!used[i][j - 1] && g[i][j - 1] != '#')
    dfs(i, j - 1);
}

int bfs(int source, int final) {
  dist[source] = 0;
  queue<int> q;
  q.push(source);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int &to : v[cur]) {
      if (dist[to] == -1) {
        dist[to] = dist[cur] + 1;
        q.push(to);
      }
    }
  }
  return dist[final];
}

int main() {
  FREEELO
  cin >> n >> m;
  cin >> sx >> sy >> fx >> fy;
  for (int i = 1; i <= n; i++) {
    cin >> g[i];
    g[i] = " " + g[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (comp[i][j] == 0 && g[i][j] != '#' && used[i][j] == 0) {
        cnt++;
        dfs(i, j);
      }
    }
  }
  map<ii, int> edge;
  v.resize(cnt + 2);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (g[i][j] == '#')
        continue;
      for (int k = i - 2; k <= i + 2; k++) {
        for (int l = j - 2; l <= j + 2; l++) {
          if (check(k, l) && g[k][l] == '.') {
            if (edge[{comp[k][l], comp[i][j]}] == 0 &&
                comp[k][l] != comp[i][j]) {
              edge[{comp[k][l], comp[i][j]}] = 1;
              edge[{comp[i][j], comp[k][l]}] = 1;
              v[comp[i][j]].pb(comp[k][l]);
              v[comp[k][l]].pb(comp[i][j]);
            }
          }
        }
      }
    }
  }
  int source = comp[sx][sy];
  int final = comp[fx][fy];
  memset(dist, -1, sizeof(dist));
  cout << bfs(source, final) << endl;
}