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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}

const int maxn = 1e6 + 7;

int h, w;
int sx, sy;
int dx, dy;
vector<string> gridog;
vector<vector<int>> grid;
int cnt;
vector<pair<int, int>> moves = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
map<int, set<int>> g;

void bfs(int xx, int yy) {
  queue<pair<int, int>> q;
  q.push({xx, yy});
  grid[xx][yy] = cnt;

  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (auto p : moves) {
      int xx = x + p.first;
      int yy = y + p.second;
      if (xx < 0 || xx >= h)
        continue;
      if (yy < 0 || yy >= w)
        continue;
      if (grid[xx][yy] != 0)
        continue;
      grid[xx][yy] = cnt;
      q.push({xx, yy});
    }
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> h >> w;

  cin >> sx >> sy;
  sx--;
  sy--;

  cin >> dx >> dy;
  dx--;
  dy--;

  gridog.resize(h);
  grid.assign(h, vector<int>(w, 0));
  for (int i = 0; i < h; i++) {
    cin >> gridog[i];
    for (int j = 0; j < w; j++) {
      if (gridog[i][j] == '#')
        grid[i][j] = -1;
    }
  }

  cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == 0) {
        cnt++;
        bfs(i, j);
      }
    }
  }

  /*
  cout << "printing grid.\\n";
  for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
          cout << grid[i][j] << " ";
      }
      cout << "\\n";
  }
  cout << "done printing grid.\\n";
  */

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == -1)
        continue;
      assert(grid[i][j] != 0);
      int xx = i - 2;
      int yy = j - 2;
      for (int x = xx; x < xx + 5; x++) {
        for (int y = yy; y < yy + 5; y++) {
          if (x < 0 || x >= h)
            continue;
          if (y < 0 || y >= w)
            continue;
          if (grid[x][y] == -1)
            continue;
          if (grid[x][y] == grid[i][j])
            continue;
          g[grid[x][y]].insert(grid[i][j]);
          g[grid[i][j]].insert(grid[x][y]);
        }
      }
    }
  }

  queue<pair<int, int>> q;
  q.push({grid[sx][sy], 0});
  set<int> seen;

  int dnode = grid[dx][dy];

  while (!q.empty()) {
    int node = q.front().first;
    int dist = q.front().second;
    q.pop();
    seen.insert(node);
    if (node == dnode)
      out(dist);
    for (auto v : g[node]) {
      if (seen.count(v))
        continue;
      seen.insert(v);
      q.push({v, dist + 1});
    }
  }

  out(-1);

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}

const int maxn = 1e6 + 7;

int h, w;
int sx, sy;
int dx, dy;
vector<string> grid;
int cnt;
vector<pair<int, int>> moves = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
map<int, set<int>> g;

void bfs(int xx, int yy) {
  queue<pair<int, int>> q;
  q.push({xx, yy});
  grid[xx][yy] = (char)(cnt + '0');

  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (auto p : moves) {
      int xx = x + p.first;
      int yy = y + p.second;
      if (xx < 0 || xx >= h)
        continue;
      if (yy < 0 || yy >= w)
        continue;
      if (grid[xx][yy] != '.')
        continue;
      grid[xx][yy] = (char)(cnt + '0');
      q.push({xx, yy});
    }
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> h >> w;

  cin >> sx >> sy;
  sx--;
  sy--;

  cin >> dx >> dy;
  dx--;
  dy--;

  grid.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }

  cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == '.') {
        cnt++;
        bfs(i, j);
      }
    }
  }

  /*
  cout << "printing grid.\\n";
  for (int i = 0; i < h; i++) {
      cout << grid[i] << "\\n";
  }
  cout << "done printing grid.\\n";
  */

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == '#')
        continue;
      assert(grid[i][j] != '.');
      int xx = i - 2;
      int yy = j - 2;
      for (int x = xx; x < xx + 5; x++) {
        for (int y = yy; y < yy + 5; y++) {
          if (x < 0 || x >= h)
            continue;
          if (y < 0 || y >= w)
            continue;
          if (grid[x][y] == '#')
            continue;
          if (grid[x][y] == grid[i][j])
            continue;
          g[grid[x][y] - '0'].insert(grid[i][j] - '0');
          g[grid[i][j] - '0'].insert(grid[x][y] - '0');
        }
      }
    }
  }

  queue<pair<int, int>> q;
  q.push({grid[sx][sy] - '0', 0});
  set<int> seen;

  int dnode = (grid[dx][dy] - '0');

  while (!q.empty()) {
    int node = q.front().first;
    int dist = q.front().second;
    q.pop();
    seen.insert(node);
    if (node == dnode)
      out(dist);
    for (auto v : g[node]) {
      if (seen.count(v))
        continue;
      seen.insert(v);
      q.push({v, dist + 1});
    }
  }

  out(-1);

  return 0;
}