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

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()

bool is_valid_warp(vector<string> &S, vector<int> &cost, int ci, int cj,
                   vector<int> &to) {
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (i == 0 && j == 0)
        continue;
      int H = S.size();
      int W = S[0].size();
      int ni = ci + i, nj = cj + j;
      if (ni < 0 || ni > H - 1 || nj < 0 || nj > W - 1)
        continue;
      if (S[ni][nj] == '#')
        continue;

      int id = ni * W + nj;
      if (cost[id] == -1) {
        to.push_back(id);
      }
    }
  }
  if (!to.empty())
    return true;
  else
    return false;
}

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  Graph G(H * W);
  vector<vector<int>> gid(H, vector<int>(W, -1));
  map<int, pair<int, int>> tocood;
  rep(i, H) rep(j, W) {
    int id = i * W + j;
    gid[i][j] = id;
    tocood[id] = make_pair(i, j);
    if (S[i][j] == '#')
      continue;
    if (count(ALL(G[id]), id - W) == 0 && i - 1 > 0 && S[i - 1][j] == '.') {
      G[id].push_back(id - W);
      G[id - W].push_back(id);
    }
    if (count(ALL(G[id]), id + 1) == 0 && j + 1 < W && S[i][j + 1] == '.') {
      G[id].push_back(id + 1);
      G[id + 1].push_back(id);
    }
    if (count(ALL(G[id]), id + W) == 0 && i + 1 < H && S[i + 1][j] == '.') {
      G[id].push_back(id + W);
      G[id + W].push_back(id);
    }
    if (count(ALL(G[id]), id - 1) == 0 && j - 1 > 0 && S[i][j - 1] == '.') {
      G[id].push_back(id - 1);
      G[id - 1].push_back(id);
    }
  }

  vector<int> cost(H * W, -1);
  vector<int> visited;
  queue<int> que;

  int cid = gid[Ch][Cw];
  int did = gid[Dh][Dw];
  cost[cid] = 0;
  que.push(cid);

  while (!que.empty()) {
    // Move A
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      visited.push_back(v);
      for (auto nv : G[v]) {
        if (cost[nv] != -1)
          continue;
        cost[nv] = cost[v];
        que.push(nv);
      }
    }
    if (cost[did] != -1)
      break;

    // Move B
    for (auto v : visited) {
      int i, j;
      vector<int> to;
      tie(i, j) = tocood[v];
      if (is_valid_warp(S, cost, i, j, to)) {
        for (auto nv : to) {
          if (cost[nv] != -1)
            continue;
          cost[nv] = cost[v] + 1;
          que.push(nv);
        }
      }
    }
    visited.clear();
  }

  cout << cost[did] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()

bool is_valid_warp(vector<string> &S, vector<int> &cost, int ci, int cj,
                   vector<int> &to) {
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (i == 0 && j == 0)
        continue;
      int H = S.size();
      int W = S[0].size();
      int ni = ci + i, nj = cj + j;
      if (ni < 0 || ni > H - 1 || nj < 0 || nj > W - 1)
        continue;
      if (S[ni][nj] == '#')
        continue;

      int id = ni * W + nj;
      if (cost[id] == -1) {
        to.push_back(id);
      }
    }
  }
  if (!to.empty())
    return true;
  else
    return false;
}

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  Graph G(H * W);
  vector<vector<int>> gid(H, vector<int>(W, -1));
  map<int, pair<int, int>> tocood;
  rep(i, H) rep(j, W) {
    int id = i * W + j;
    gid[i][j] = id;
    tocood[id] = make_pair(i, j);
    if (S[i][j] == '#')
      continue;
    if (count(ALL(G[id]), id - H) == 0 && i - 1 > 0 && S[i - 1][j] == '.') {
      G[id].push_back(id - H);
      G[id - H].push_back(id);
    }
    if (count(ALL(G[id]), id + 1) == 0 && j + 1 < W && S[i][j + 1] == '.') {
      G[id].push_back(id + 1);
      G[id + 1].push_back(id);
    }
    if (count(ALL(G[id]), id + H) == 0 && i + 1 < H && S[i + 1][j] == '.') {
      G[id].push_back(id + H);
      G[id + H].push_back(id);
    }
    if (count(ALL(G[id]), id - 1) == 0 && j - 1 > 0 && S[i][j - 1] == '.') {
      G[id].push_back(id - 1);
      G[id - 1].push_back(id);
    }
  }

  vector<int> cost(H * W, -1);
  vector<int> visited;
  queue<int> que;

  int cid = gid[Ch][Cw];
  int did = gid[Dh][Dw];
  cost[cid] = 0;
  que.push(cid);

  while (!que.empty()) {
    // Move A
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      visited.push_back(v);
      for (auto nv : G[v]) {
        if (cost[nv] != -1)
          continue;
        cost[nv] = cost[v];
        que.push(nv);
      }
    }
    if (cost[did] != -1)
      break;

    // Move B
    for (auto v : visited) {
      int i, j;
      vector<int> to;
      tie(i, j) = tocood[v];
      if (is_valid_warp(S, cost, i, j, to)) {
        for (auto nv : to) {
          if (cost[nv] != -1)
            continue;
          cost[nv] = cost[v] + 1;
          que.push(nv);
        }
      }
    }
    visited.clear();
  }

  cout << cost[did] << endl;

  return 0;
}