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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int const N = 1001;
int n, m, a[N][N], color[N][N], colors = 0;
vector<set<int>> adj;

vector<int> di = {1, -1, 0, 0}, dj = {0, 0, 1, -1};
bool valid(int i, int j) {
  return i >= 0 && j >= 0 && i < n && j < m && a[i][j];
}
void dfs(int i, int j) {
  color[i][j] = colors;
  for (int k = 0; k < 4; k++) {
    if (valid(i + di[k], j + dj[k]) && !color[i + di[k]][j + dj[k]]) {
      dfs(i + di[k], j + dj[k]);
    }
  }
}

void solvetask() {
  cin >> n >> m;
  int ai, aj, bi, bj;
  cin >> ai >> aj >> bi >> bj;
  ai--;
  aj--;
  bi--;
  bj--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      a[i][j] = (c == '.');
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!color[i][j] && a[i][j]) {
        ++colors;
        dfs(i, j);
      }
    }
  }
  adj.resize(colors + 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!valid(i, j))
        continue;
      for (int dii = -2; dii <= 2; dii++) {
        for (int djj = -2; djj <= 2; djj++) {
          if (valid(i + dii, j + djj)) {
            int c1 = color[i][j], c2 = color[i + dii][j + djj];
            if (c1 != c2) {
              adj[c1].insert(c2);
              adj[c2].insert(c1);
            }
          }
        }
      }
    }
  }
  queue<int> f;
  vector<int> dist(colors + 1, INT_MAX);
  f.push(color[ai][aj]);
  dist[color[ai][aj]] = 0;
  while (!f.empty()) {
    int top = f.front();
    f.pop();
    for (int c : adj[top]) {
      if (dist[c] > dist[top] + 1) {
        dist[c] = dist[top] + 1;
        f.push(c);
      }
    }
  }
  if (dist[color[bi][bj]] == INT_MAX)
    cout << "-1\\n";
  else
    cout << dist[color[bi][bj]] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1; // cin >> t;
  while (t--)
    solvetask();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int const N = 1001;
int n, m, a[N][N], color[N][N], colors = 0;
set<int> adj[N];

vector<int> di = {1, -1, 0, 0}, dj = {0, 0, 1, -1};
bool valid(int i, int j) {
  return i >= 0 && j >= 0 && i < n && j < m && a[i][j];
}
void dfs(int i, int j) {
  color[i][j] = colors;
  for (int k = 0; k < 4; k++) {
    if (valid(i + di[k], j + dj[k]) && !color[i + di[k]][j + dj[k]]) {
      dfs(i + di[k], j + dj[k]);
    }
  }
}

void solvetask() {
  cin >> n >> m;
  int ai, aj, bi, bj;
  cin >> ai >> aj >> bi >> bj;
  ai--;
  aj--;
  bi--;
  bj--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      a[i][j] = (c == '.');
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!color[i][j] && a[i][j]) {
        ++colors;
        dfs(i, j);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!valid(i, j))
        continue;
      for (int dii = -2; dii <= 2; dii++) {
        for (int djj = -2; djj <= 2; djj++) {
          if (valid(i + dii, j + djj)) {
            int c1 = color[i][j], c2 = color[i + dii][j + djj];
            if (c1 != c2) {
              adj[c1].insert(c2);
              adj[c2].insert(c1);
            }
          }
        }
      }
    }
  }
  queue<int> f;
  vector<int> dist(colors + 1, INT_MAX);
  f.push(color[ai][aj]);
  dist[color[ai][aj]] = 0;
  while (!f.empty()) {
    int top = f.front();
    f.pop();
    for (int c : adj[top]) {
      if (dist[c] > dist[top] + 1) {
        dist[c] = dist[top] + 1;
        f.push(c);
      }
    }
  }
  if (dist[color[bi][bj]] == INT_MAX)
    cout << "-1\\n";
  else
    cout << dist[color[bi][bj]] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1; // cin >> t;
  while (t--)
    solvetask();
}