Problem p02579 (C++)

Buggy submission (status: Runtime Error):
/* In The Name of Anton*/

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, a, n) for (int i = a; i <= n; i++)
#define repb(i, a, n) for (int i = a; i >= n; i--)
#define trav(a, x) for (auto a = x.begin(); a != x.end(); a++)
#define all(x) x.begin(), x.end()
#define fst first
#define snd second
#define pb push_back
#define mp make_pair
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

const int mxN = 1e3 + 5;
int a[mxN][mxN];
string s[mxN];
int temp;
int n, m;
vi adj[1000005];

void pre() {}

void dfs(int i, int j) {
  a[i][j] = temp;

  if (i + 1 < n) {
    if (a[i + 1][j] == 0 && s[i + 1][j] == '.') {
      dfs(i + 1, j);
    }
  }

  if (i - 1 >= 0) {
    if (a[i - 1][j] == 0 && s[i - 1][j] == '.') {
      dfs(i - 1, j);
    }
  }

  if (j - 1 >= 0) {
    if (a[i][j - 1] == 0 && s[i][j - 1] == '.') {
      dfs(i, j - 1);
    }
  }

  if (j + 1 < m) {
    if (a[i][j + 1] == 0 && s[i][j + 1] == '.') {
      dfs(i, j + 1);
    }
  }
}

void solve() {
  temp = 0;
  cin >> n >> m;

  for (int i = 0; i < mxN; i++) {
    for (int j = 0; j < mxN; j++) {
      a[i][j] = 0;
    }
  }

  int sx, sy, dx, dy;
  cin >> sx >> sy >> dx >> dy;
  sx--;
  sy--;
  dx--;
  dy--;

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 0 && s[i][j] == '.') {
        temp++;
        dfs(i, j);
      }
    }
  }

  map<pii, int> m1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {

      int ctemp1 = i - 2, ctemp2 = i + 2;
      int dtemp1 = j - 2, dtemp2 = j + 2;

      for (int x = ctemp1; x <= ctemp2; x++) {
        for (int y = dtemp1; y <= dtemp2; y++) {
          if (x >= 0 && x < n && y >= 0 && y < m) {
            if (a[i][j] != a[x][y] && a[i][j] != 0 && a[x][y] != 0) {
              if (m1[{a[i][j], a[x][y]}] == 0) {
                m1[{a[i][j], a[x][y]}] = 1;
                adj[a[i][j]].pb(a[x][y]);
              }
            }
          }
        }
      }
    }
  }

  int s = a[sx][sy];
  int d = a[dx][dy];

  int dis[mxN];
  bool used[mxN];
  memset(dis, -1, sizeof(dis));
  memset(used, false, sizeof(used));

  dis[s] = 0;
  used[s] = true;
  queue<int> q;
  q.push(s);

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : adj[v]) {
      if (!used[u]) {
        used[u] = true;
        q.push(u);
        dis[u] = dis[v] + 1;
      }
    }
  }

  cout << dis[d] << "\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  pre();
  int t;
  t = 1;
  rep(i, t) solve();
  return 0;
}


Fixed submission:
/* In The Name of Anton*/

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, a, n) for (int i = a; i <= n; i++)
#define repb(i, a, n) for (int i = a; i >= n; i--)
#define trav(a, x) for (auto a = x.begin(); a != x.end(); a++)
#define all(x) x.begin(), x.end()
#define fst first
#define snd second
#define pb push_back
#define mp make_pair
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

const int mxN = 1e3 + 5;
int a[mxN][mxN];
string s[mxN];
int temp;
int n, m;
vi adj[1000005];

void pre() {}

void dfs(int i, int j) {
  a[i][j] = temp;

  if (i + 1 < n) {
    if (a[i + 1][j] == 0 && s[i + 1][j] == '.') {
      dfs(i + 1, j);
    }
  }

  if (i - 1 >= 0) {
    if (a[i - 1][j] == 0 && s[i - 1][j] == '.') {
      dfs(i - 1, j);
    }
  }

  if (j - 1 >= 0) {
    if (a[i][j - 1] == 0 && s[i][j - 1] == '.') {
      dfs(i, j - 1);
    }
  }

  if (j + 1 < m) {
    if (a[i][j + 1] == 0 && s[i][j + 1] == '.') {
      dfs(i, j + 1);
    }
  }
}

void solve() {
  temp = 0;
  cin >> n >> m;

  for (int i = 0; i < mxN; i++) {
    for (int j = 0; j < mxN; j++) {
      a[i][j] = 0;
    }
  }

  int sx, sy, dx, dy;
  cin >> sx >> sy >> dx >> dy;
  sx--;
  sy--;
  dx--;
  dy--;

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 0 && s[i][j] == '.') {
        temp++;
        dfs(i, j);
      }
    }
  }

  map<pii, int> m1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {

      int ctemp1 = i - 2, ctemp2 = i + 2;
      int dtemp1 = j - 2, dtemp2 = j + 2;

      for (int x = ctemp1; x <= ctemp2; x++) {
        for (int y = dtemp1; y <= dtemp2; y++) {
          if (x >= 0 && x < n && y >= 0 && y < m) {
            if (a[i][j] != a[x][y] && a[i][j] != 0 && a[x][y] != 0) {
              if (m1[{a[i][j], a[x][y]}] == 0) {
                m1[{a[i][j], a[x][y]}] = 1;
                adj[a[i][j]].pb(a[x][y]);
              }
            }
          }
        }
      }
    }
  }

  int s = a[sx][sy];
  int d = a[dx][dy];

  int dis[1000005];
  bool used[1000005];
  memset(dis, -1, sizeof(dis));
  memset(used, false, sizeof(used));

  dis[s] = 0;
  used[s] = true;
  queue<int> q;
  q.push(s);

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u : adj[v]) {
      if (!used[u]) {
        used[u] = true;
        q.push(u);
        dis[u] = dis[v] + 1;
      }
    }
  }

  cout << dis[d] << "\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  pre();
  int t;
  t = 1;
  rep(i, t) solve();
  return 0;
}