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

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")

using namespace std;
using ll = long long;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define F first
#define S second
#define Ceil(x, y) ((x + y - 1) / y)
#define PI acos(-1)
const int mod = (int)1000000007;
int r, c, cc, cr, dr, dc;
vector<vector<int>> M(1005, vector<int>(1005));
auto vis = M;
int k = 1;
vector<int> G[600000];
int pred[600000], dist[600000];

void dfs(int i, int j) {
  if (i < 0 || j < 0 || i >= r || j >= c || vis[i][j] || M[i][j] == 0)
    return;
  vis[i][j] = k;
  dfs(i, j + 1);
  dfs(i, j - 1);
  dfs(i + 1, j);
  dfs(i - 1, j);
}

void makeEdge(int a, int b) {
  set<int> s;
  for (int i = max(0, a - 2); i <= min(r - 1, a + 2); i++) {
    for (int j = max(0, b - 2); j <= min(c - 1, b + 2); j++)
      if (vis[i][j] && vis[a][b] != vis[i][j]) {
        s.insert(vis[i][j]);
      }
  }
  for (auto i : s)
    G[vis[a][b]].push_back(i);
}

bool BFS(int src, int dest, int v) {
  list<int> queue;
  bool visited[v];
  for (int i = 0; i < v; i++) {
    visited[i] = false;
    dist[i] = INT_MAX;
    pred[i] = -1;
  }

  visited[src] = true;
  dist[src] = 0;
  queue.push_back(src);

  while (!queue.empty()) {
    int u = queue.front();
    queue.pop_front();
    for (int i = 0; i < G[u].size(); i++) {
      if (visited[G[u][i]] == false) {
        visited[G[u][i]] = true;
        dist[G[u][i]] = dist[u] + 1;
        pred[G[u][i]] = u;
        queue.push_back(G[u][i]);

        if (G[u][i] == dest)
          return true;
      }
    }
  }

  return false;
}

void solve() {

  cin >> r >> c;
  cin >> cr >> cc;
  cin >> dr >> dc;

  for (int i = 0; i < r; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < c; j++)
      if (str[j] != '#')
        M[i][j] = 1;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
      if (not vis[i][j] && M[i][j]) {
        dfs(i, j);
        k++;
      }
  }
  if (vis[cr - 1][cc - 1] == vis[dr - 1][dc - 1]) {
    cout << 0;
    return;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
      if (M[i][j])
        makeEdge(i, j);
  }

  if (BFS(vis[cr - 1][cc - 1], vis[dr - 1][dc - 1], 600000) == false)
    cout << -1;
  else
    cout << dist[vis[dr - 1][dc - 1]] << endl;
}

int main(int argc, char *argv[]) {
  clock_t clk_start = clock();
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int cases = 1;
  // cin>>cases;
  while (cases--) {
    solve();
  }
  cerr << "Execution Time: " << fixed
       << double(clock() - clk_start) / double(CLOCKS_PER_SEC)
       << setprecision(6) << " sec\\n";
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")

using namespace std;
using ll = long long;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define F first
#define S second
#define Ceil(x, y) ((x + y - 1) / y)
#define PI acos(-1)
const int mod = (int)1000000007;
int r, c, cc, cr, dr, dc;
vector<vector<int>> M(1001, vector<int>(1001));
auto vis = M;
int k = 1;
vector<int> G[1001];
int pred[1001], dist[1001];

void dfs(int i, int j) {
  if (i < 0 || j < 0 || i >= r || j >= c || vis[i][j] || M[i][j] == 0)
    return;
  vis[i][j] = k;
  dfs(i, j + 1);
  dfs(i, j - 1);
  dfs(i + 1, j);
  dfs(i - 1, j);
}

void makeEdge(int a, int b) {
  set<int> s;
  for (int i = max(0, a - 2); i <= min(r - 1, a + 2); i++) {
    for (int j = max(0, b - 2); j <= min(c - 1, b + 2); j++)
      if (vis[i][j] && vis[a][b] != vis[i][j]) {
        s.insert(vis[i][j]);
      }
  }
  for (auto i : s)
    G[vis[a][b]].push_back(i);
}

bool BFS(int src, int dest, int v) {
  list<int> queue;
  bool visited[v];
  for (int i = 0; i < v; i++) {
    visited[i] = false;
    dist[i] = INT_MAX;
    pred[i] = -1;
  }

  visited[src] = true;
  dist[src] = 0;
  queue.push_back(src);

  while (!queue.empty()) {
    int u = queue.front();
    queue.pop_front();
    for (int i = 0; i < G[u].size(); i++) {
      if (visited[G[u][i]] == false) {
        visited[G[u][i]] = true;
        dist[G[u][i]] = dist[u] + 1;
        pred[G[u][i]] = u;
        queue.push_back(G[u][i]);

        if (G[u][i] == dest)
          return true;
      }
    }
  }

  return false;
}

void solve() {

  cin >> r >> c;
  cin >> cr >> cc;
  cin >> dr >> dc;
  for (int i = 0; i < r; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < c; j++)
      if (str[j] != '#')
        M[i][j] = 1;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
      if (not vis[i][j] && M[i][j]) {
        dfs(i, j);
        k++;
      }
  }
  if (vis[cr - 1][cc - 1] == vis[dr - 1][dr - 1]) {
    cout << 0;
    return;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
      if (M[i][j])
        makeEdge(i, j);
  }

  if (BFS(vis[cr - 1][cc - 1], vis[dr - 1][dc - 1], 1001) == false)
    cout << -1;
  else
    cout << dist[vis[dr - 1][dc - 1]] << endl;
}

int main(int argc, char *argv[]) {
  clock_t clk_start = clock();
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int cases = 1;
  // cin>>cases;
  while (cases--) {
    solve();
  }
  cerr << "Execution Time: " << fixed
       << double(clock() - clk_start) / double(CLOCKS_PER_SEC)
       << setprecision(6) << " sec\\n";
}