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

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6 + 15;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int mat[1200][1200];
string s[1200];
int mark[1200][1200];
int r, c;
int row[] = {1, 0, -1, 0, 1, -1, 1, -1};
int colm[] = {0, 1, 0, -1, 1, -1, -1, 1};
int cur;
vector<int> graph[MAX];
void bfs(int input_x, int input_y) {
  queue<pair<int, int>> q;
  mark[input_x][input_y] = 1;
  mat[input_x][input_y] = cur;
  q.push(make_pair(input_x, input_y));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int u = x + row[i];
      int v = y + colm[i];
      if (0 <= u && u < r && 0 <= v && v < c) {
        if (mark[u][v] == 0 && s[u][v] == '.') {
          mark[u][v] = 1;
          q.push(make_pair(u, v));
          mat[u][v] = cur;
        }
      }
    }
  }
  return;
}
int V[MAX], D[MAX];
void bfs2(int src) {
  V[src] = 1;
  queue<int> q;
  q.push(src);
  while (!q.empty()) {
    int parent = q.front();
    q.pop();
    for (int i = 0; i < graph[parent].size(); i++) {
      int child = graph[parent][i];
      if (V[child] == 0) {
        V[child] = 1;
        D[child] = D[parent] + 1;
        q.push(child);
      }
    }
  }
}
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int a,
      b, p, q;
  cin >> r >> c;
  cin >> a >> b >> p >> q;
  for (int i = 0; i < r; i++)
    cin >> s[i];
  a--;
  b--;
  p--;
  q--;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (mark[i][j] == 0 && s[i][j] == '.') {
        // cout<<i<<" "<<j<<endl;
        cur++;
        bfs(i, j);
      }
    }
  }
  map<pair<int, int>, int> mp;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (s[i][j] == '.') {
        for (int x = i - 2; x <= i + 2; x++) {
          for (int y = j - 2; y <= j + 2; y++) {
            if (0 <= x && x < r && 0 <= y && y < c) {
              int u = mat[i][j];
              int v = mat[x][y];
              if (u != v && mp[{u, v}] == 0 && u && v) {
                graph[u].push_back(v);
                graph[v].push_back(u);
                mp[{u, v}] = 1;
                mp[{v, u}] = 1;
              }
            }
          }
        }
      }
    }
  }
  int root = mat[a][b];
  bfs2(root);
  if (V[mat[p][q]] == 0)
    cout << "-1" << endl;
  else
    cout << D[mat[p][q]] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int mat[1200][1200];
string s[1200];
int mark[1200][1200];
int r, c;
int row[] = {1, 0, -1, 0, 1, -1, 1, -1};
int colm[] = {0, 1, 0, -1, 1, -1, -1, 1};
int cur;
vector<int> graph[MAX];
void bfs(int input_x, int input_y) {
  queue<pair<int, int>> q;
  mark[input_x][input_y] = 1;
  mat[input_x][input_y] = cur;
  q.push(make_pair(input_x, input_y));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int u = x + row[i];
      int v = y + colm[i];
      if (0 <= u && u < r && 0 <= v && v < c) {
        if (mark[u][v] == 0 && s[u][v] == '.') {

          mark[u][v] = 1;
          q.push(make_pair(u, v));
          mat[u][v] = cur;
        }
      }
    }
  }
  return;
}
int V[1200], D[1200];
void bfs2(int src) {
  V[src] = 1;
  queue<int> q;
  q.push(src);
  while (!q.empty()) {
    int parent = q.front();
    q.pop();
    for (int i = 0; i < graph[parent].size(); i++) {
      int child = graph[parent][i];
      if (V[child] == 0) {
        V[child] = 1;
        D[child] = D[parent] + 1;
        q.push(child);
      }
    }
  }
}
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int a,
      b, p, q;
  cin >> r >> c;
  cin >> a >> b >> p >> q;
  for (int i = 0; i < r; i++)
    cin >> s[i];
  a--;
  b--;
  p--;
  q--;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (mark[i][j] == 0 && s[i][j] == '.') {
        // cout<<i<<" "<<j<<endl;
        cur++;
        bfs(i, j);
      }
    }
  }
  map<pair<int, int>, int> mp;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (s[i][j] == '.') {
        for (int x = i - 2; x < i + 2; x++) {
          for (int y = j - 2; y < j + 2; y++) {
            if (0 <= x && x < r && 0 <= y && y < c) {
              int u = mat[i][j];
              int v = mat[x][y];
              if (u != v && mp[{u, v}] == 0 && u && v) {
                graph[u].push_back(v);
                graph[v].push_back(u);
                mp[{u, v}] = 1;
                mp[{v, u}] = 1;
              }
            }
          }
        }
      }
    }
  }
  int root = mat[a][b];
  bfs2(root);
  if (V[mat[p][q]] == 0)
    cout << "-1" << endl;
  else
    cout << D[mat[p][q]] << endl;

  return 0;
}