Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

ll H, W, h1, w1, h2, w2, ans = -1, tmp = 0;
string S;
ll dy[] = {1, 0, -1, 0};
ll dx[] = {0, 1, 0, -1};
vector<ll> visited;
vector<vector<ll>> color, G;
vector<vector<char>> A;

ll dfs(ll h, ll w, ll id) {
  for (int i = 0; i < 4; i++) {
    int ny = h + dy[i], nx = w + dx[i];
    if (!(0 <= ny && ny < H && 0 <= nx && nx < W))
      continue;
    if (A[ny][nx] == '.' && color[ny][nx] == -1) {
      color[ny][nx] = id;
      dfs(ny, nx, id);
    }
  }
}

void bfs(ll bd1, ll bd2) {
  queue<pair<ll, ll>> Q;
  Q.push(make_pair(bd1, 0));
  while (!Q.empty()) {
    pair<ll, ll> p = Q.front();
    Q.pop();
    if (visited[p.first] != -1)
      continue;
    visited[p.first] = p.second;
    if (p.first == bd2) {
      ans = p.second;
      break;
    }
    for (int i = 0; i < G[p.first].size(); i++) {
      if (visited[G[p.first][i]] == -1)
        Q.push(make_pair(G[p.first][i], p.second + 1));
    }
  }
}

signed main() {
  cin >> H >> W >> h1 >> w1 >> h2 >> w2;
  h1--;
  w1--;
  h2--;
  w2--;
  A.resize(H);
  color.resize(H);
  for (int i = 0; i < H; i++) {
    color[i].resize(W, -1);
    for (int j = 0; j < W; j++) {
      char a;
      cin >> a;
      A[i].push_back(a);
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (A[i][j] == '.' && color[i][j] == -1) {
        color[i][j] = tmp;
        dfs(i, j, tmp);
        tmp++;
      }
    }
  }
  G.resize(tmp);
  visited.resize(tmp, -1);
  // cout << "OK" << endl;
  /*
  for(int i=0;i<H;i++) {
      for(int j=0;j<W;j++) {
          cout << color[i][j] << " ";
      }
      cout << endl;
  }
  */
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int l = i - 2; l < i + 3; l++) {
        if (l >= 0 && l < H) {
          for (int k = j - 2; k < j + 3; k++) {
            if (k >= 0 && k < W) {
              if (color[l][k] != -1 && color[i][j] != -1 &&
                  color[l][k] != color[i][j]) {
                G[color[i][j]].push_back(color[l][k]);
                G[color[l][k]].push_back(color[i][j]);
              }
            }
          }
        }
      }
    }
  }
  // cout << "OK" << endl;
  if (color[h1][w1] != -1 && color[h2][w2] != -1)
    bfs(color[h1][w1], color[h2][w2]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

ll H, W, h1, w1, h2, w2, ans = -1, tmp = 0;
string S;
ll dy[] = {1, 0, -1, 0};
ll dx[] = {0, 1, 0, -1};
vector<ll> visited;
vector<vector<ll>> color, G;
vector<vector<char>> A;

void dfs(ll h, ll w, ll id) {
  for (int i = 0; i < 4; i++) {
    int ny = h + dy[i], nx = w + dx[i];
    if (!(0 <= ny && ny < H && 0 <= nx && nx < W))
      continue;
    if (A[ny][nx] == '.' && color[ny][nx] == -1) {
      color[ny][nx] = id;
      dfs(ny, nx, id);
    }
  }
}

void bfs(ll bd1, ll bd2) {
  queue<pair<ll, ll>> Q;
  Q.push(make_pair(bd1, 0));
  while (!Q.empty()) {
    pair<ll, ll> p = Q.front();
    Q.pop();
    if (visited[p.first] != -1)
      continue;
    visited[p.first] = p.second;
    if (p.first == bd2) {
      ans = p.second;
      break;
    }
    for (int i = 0; i < G[p.first].size(); i++) {
      if (visited[G[p.first][i]] == -1)
        Q.push(make_pair(G[p.first][i], p.second + 1));
    }
  }
}

signed main() {
  cin >> H >> W >> h1 >> w1 >> h2 >> w2;
  h1--;
  w1--;
  h2--;
  w2--;
  A.resize(H);
  color.resize(H);
  for (int i = 0; i < H; i++) {
    color[i].resize(W, -1);
    for (int j = 0; j < W; j++) {
      char a;
      cin >> a;
      A[i].push_back(a);
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (A[i][j] == '.' && color[i][j] == -1) {
        color[i][j] = tmp;
        dfs(i, j, tmp);
        tmp++;
      }
    }
  }
  G.resize(tmp);
  visited.resize(tmp, -1);
  // cout << "OK" << endl;
  /*
  for(int i=0;i<H;i++) {
      for(int j=0;j<W;j++) {
          cout << color[i][j] << " ";
      }
      cout << endl;
  }
  */
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int l = i - 2; l < i + 3; l++) {
        if (l >= 0 && l < H) {
          for (int k = j - 2; k < j + 3; k++) {
            if (k >= 0 && k < W) {
              if (color[l][k] != -1 && color[i][j] != -1 &&
                  color[l][k] != color[i][j]) {
                G[color[i][j]].push_back(color[l][k]);
                G[color[l][k]].push_back(color[i][j]);
              }
            }
          }
        }
      }
    }
  }
  // cout << "OK" << endl;
  if (color[h1][w1] != -1 && color[h2][w2] != -1)
    bfs(color[h1][w1], color[h2][w2]);
  cout << ans << endl;
  return 0;
}