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;

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define speedhack()                                                            \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define form(i, n) for (size_t i = 0; i < size_t(n); ++i)
#define forn(i, n) for (size_t i = 1; i <= size_t(n); ++i)
template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long mod(long long a, long long b) { return (a % b + b) % b; }
long long gcd(long long x, long long y) { return __gcd(x, y); }
long long lcm(long long x, long long y) { return x * 1ll * y / gcd(x, y); }

const long long MAXN = 1e9 + 9;
const long long SIZE = 3e5 + 5;
const long long MOD = 1e9 + 7;

int N, M, color = 1, res = -1, C[2], D[2], m[1010][1010];

int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, -1, 1};

queue<pair<int, int>> q;

set<int> adj[1001000];

bool used[1010][1010], was[1001000];

bool ok(int i, int j) {
  if (1 <= i && i <= N && 1 <= j && j <= M)
    return true;
  else
    return false;
}

void paint(int i, int j) {
  m[i][j] = color;
  used[i][j] = true;
  for (int s = 0; s < 4; s++) {
    if (m[i + dx[s]][j + dy[s]] != 1 && !used[i + dx[s]][j + dy[s]] &&
        ok(i + dx[s], j + dy[s]))
      paint(i + dx[s], j + dy[s]);
  }
}

void bfs() {
  q.push({m[C[0]][C[1]], 0});
  was[m[C[0]][C[1]]] = true;

  while (!q.empty()) {
    int v = q.front().first;
    int len = q.front().second;
    q.pop();

    if (v == m[D[0]][D[1]]) {
      res = len;
      return;
    }

    for (int to : adj[v]) {
      if (!was[to]) {
        was[to] = true;
        q.push({to, len + 1});
      }
    }
  }
}

int main() {
  speedhack()

          cin >>
      N >> M;

  cin >> C[0] >> C[1];
  cin >> D[0] >> D[1];

  forn(i, N) {
    forn(j, M) {
      char c;
      cin >> c;
      if (c == '#')
        m[i][j] = 1;
    }
  }

  forn(i, N) {
    forn(j, M) {
      if (m[i][j] != 1 && !used[i][j]) {
        ++color;
        paint(i, j);
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      if (m[i][j] != 1) {
        for (int ii = i - 2; ii <= i + 2; ii++) {
          for (int jj = j - 2; jj <= j + 2; jj++) {
            if (ok(ii, jj) && m[ii][jj] > 1) {
              adj[m[ii][jj]].insert(m[i][j]);
              adj[m[i][j]].insert(m[ii][jj]);
            }
          }
        }
      }
    }
  }

  bfs();

  if (m[C[0]][C[1]] == 1 || m[D[0]][D[1]] == 1)
    res = -1;

  cout << res << endl;

  return 0;
}

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;

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define speedhack()                                                            \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define form(i, n) for (size_t i = 0; i < size_t(n); ++i)
#define forn(i, n) for (size_t i = 1; i <= size_t(n); ++i)
template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long mod(long long a, long long b) { return (a % b + b) % b; }
long long gcd(long long x, long long y) { return __gcd(x, y); }
long long lcm(long long x, long long y) { return x * 1ll * y / gcd(x, y); }

const long long MAXN = 1e9 + 9;
const long long SIZE = 3e5 + 5;
const long long MOD = 1e9 + 7;

int N, M, color = 1, res, C[2], D[2], m[1010][1010];

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

queue<pair<pair<int, int>, int>> q;

set<int> adj[1010];

bool used[1010][1010], was[1010];

bool ok(int i, int j) {
  if (1 <= i && i <= N && 1 <= j && j <= M)
    return true;
  else
    return false;
}

void paint(int i, int j) {
  m[i][j] = color;
  used[i][j] = true;
  for (int s = 0; s < 4; s++) {
    if (m[i + dx[s]][j + dy[s]] != 1 && !used[i + dx[s]][j + dy[s]] &&
        ok(i + dx[s], j + dy[s]))
      paint(i + dx[s], j + dy[s]);
  }
}

void dfs(int v, int len) {
  was[v] = true;

  if (v == m[D[0]][D[1]]) {
    res = min(res, len);
    return;
  }
  for (int to : adj[v]) {
    if (was[to])
      continue;
    dfs(to, len + 1);
  }
}

int main() {
  speedhack()

          cin >>
      N >> M;

  cin >> C[0] >> C[1];
  cin >> D[0] >> D[1];

  forn(i, N) {
    forn(j, M) {
      char c;
      cin >> c;
      if (c == '#')
        m[i][j] = 1;
    }
  }

  forn(i, N) forn(j, M) {
    if (m[i][j] != 1 && !used[i][j]) {
      ++color;
      paint(i, j);
    }
  }

  forn(i, N) forn(j, M) {
    if (m[i][j] == 1) {
      if (m[i][j - 1] > 1 && m[i][j + 1] > 1) {
        adj[m[i][j - 1]].insert(m[i][j + 1]);
        adj[m[i][j + 1]].insert(m[i][j - 1]);
      }

      if (m[i - 1][j] > 1 && m[i + 1][j] > 1) {
        adj[m[i - 1][j]].insert(m[i + 1][j]);
        adj[m[i + 1][j]].insert(m[i - 1][j]);
      }

      if (m[i + 1][j - 1] > 1 && m[i - 1][j + 1] > 1) {
        adj[m[i + 1][j - 1]].insert(m[i - 1][j + 1]);
        adj[m[i - 1][j + 1]].insert(m[i + 1][j - 1]);
      }

      if (m[i - 1][j - 1] > 1 && m[i + 1][j + 1] > 1) {
        adj[m[i - 1][j - 1]].insert(m[i + 1][j + 1]);
        adj[m[i + 1][j + 1]].insert(m[i - 1][j - 1]);
      }
    }
  }

  res = MAXN;
  dfs(m[C[0]][C[1]], 0);

  if (res == MAXN)
    res = -1;

  cout << res << endl;

  return 0;
}