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

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int n, m;
int a, b;
int c, d;
char e[1010][1010];
int need[1010][1010];
vector<int> vv[700010];
int bbfs[700010];
int now = 0;
void bdfs() {
  queue<int> q;
  bbfs[need[a][b]] = 0;
  q.push(need[a][b]);
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int i = 0; i < vv[p].size(); i++) {
      if (bbfs[p] + 1 < bbfs[vv[p][i]]) {
        bbfs[vv[p][i]] = bbfs[p] + 1;
        q.push(vv[p][i]);
      }
    }
  }
}
void bfs(int x, int y) {
  now++;
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  need[x][y] = now;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ax = dx[i] + p.first;
      int ay = dy[i] + p.second;
      if (ax >= 0 && ax < n && ay >= 0 && ay < m && e[ax][ay] != '#' &&
          !need[ax][ay]) {
        need[ax][ay] = now;
        q.push(make_pair(ax, ay));
      }
    }
  }
}
int main() {
  cin >> n >> m >> a >> b >> c >> d;
  a--, b--, c--, d--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> e[i][j];
      //			e[i][j]='.';
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (e[i][j] == '.' && !need[i][j]) {
        bfs(i, j);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (e[i][j] != '.')
        continue;
      for (int ii = -2; ii <= 2; ii++) {
        for (int jj = -2; jj <= 2; jj++) {
          int ax = i + ii, ay = j + jj;
          if (ax >= 0 && ax < n && ay >= 0 & ay < m && e[ax][ay] == '.' &&
              need[i][j] != need[ax][ay]) {
            vv[need[i][j]].push_back(need[ax][ay]);
            vv[need[ax][ay]].push_back(need[i][j]);
          }
        }
      }
    }
  }
  set<int> s;
  for (int i = 1; i <= now; i++) {
    s.clear();
    bbfs[i] = 1e9;
    for (int j = 0; j < vv[i].size(); j++)
      s.insert(vv[i][j]);
    vv[i].clear();
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
      vv[i].push_back(*it);
    /*
    cout<<i<<endl;
    for(int j=0;j<vv[i].size();j++){
            cout<<vv[i][j]<<" ";
    }
    cout<<endl;
    */
  }
  bdfs();
  if (bbfs[need[c][d]] == 1e9)
    cout << -1 << endl;
  else
    cout << bbfs[need[c][d]] << endl;
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int vis[1010][1010];
int n, m;
int a, b;
int c, d;
char e[1010][1010];
int need[1010][1010];
vector<int> vv[100010];
int bbfs[100010];
int now = 0;
void bdfs() {
  queue<int> q;
  bbfs[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int i = 0; i < vv[p].size(); i++) {
      if (bbfs[vv[p][i]] == 1e9 && vv[p][i] != 1) {
        bbfs[vv[p][i]] = bbfs[p] + 1;
        q.push(vv[p][i]);
      }
    }
  }
}
void bfs(int x, int y) {
  now++;
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    need[p.first][p.second] = now;
    for (int i = 0; i < 4; i++) {
      int ax = dx[i] + p.first;
      int ay = dy[i] + p.second;
      if (ax >= 0 && ax < n && ay >= 0 && ay < m && e[ax][ay] != '#' &&
          !need[ax][ay]) {
        q.push(make_pair(ax, ay));
      }
    }
  }
}
int main() {
  cin >> n >> m >> a >> b >> c >> d;
  a--, b--, c--, d--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> e[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (e[i][j] == '.' && !need[i][j]) {
        bfs(i, j);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (e[i][j] != '.')
        continue;
      for (int ii = -2; ii <= 2; ii++) {
        for (int jj = -2; jj <= 2; jj++) {
          int ax = i + ii, ay = j + jj;
          if (ax >= 0 && ax < n && ay >= 0 & ay < m && e[ax][ay] == '.' &&
              need[i][j] != need[ax][ay]) {
            vv[need[i][j]].push_back(need[ax][ay]);
            vv[need[ax][ay]].push_back(need[i][j]);
          }
        }
      }
    }
  }
  set<int> s;
  for (int i = 1; i <= now; i++) {
    s.clear();
    bbfs[i] = 1e9;
    for (int j = 0; j < vv[i].size(); j++)
      s.insert(vv[i][j]);
    vv[i].clear();
    for (set<int>::iterator it = s.begin(); it != s.end(); it++)
      vv[i].push_back(*it);
    /*
    cout<<i<<endl;
    for(int j=0;j<vv[i].size();j++){
            cout<<vv[i][j]<<" ";
    }
    cout<<endl;
    */
  }
  bdfs();
  if (bbfs[need[c][d]] == 1e9)
    cout << -1 << endl;
  else
    cout << bbfs[need[c][d]] << endl;
  return 0;
}