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

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

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S.at(i);
  }

  vector<vector<pair<int, int>>> vvp(H * W);
  vector<vector<bool>> flag(H, vector<bool>(W, false));
  vector<pair<int, int>> temp;
  vector<int> vx = {1, 0, -1, 0};
  vector<int> vy = {0, 1, 0, -1};
  stack<pair<int, int>> sp;
  int x, y, nx, ny;

  vvp.at(0).emplace_back(Ch, Cw);
  flag.at(Ch).at(Cw) = true;

  for (int i = 0; i < H * W; i++) {
    temp = vvp.at(i);
    for (auto &p : temp) {
      sp.push(p);
      while (!sp.empty()) {
        tie(x, y) = sp.top();
        sp.pop();
        for (int j = 0; j < 4; j++) {
          nx = x + vx.at(j);
          ny = y + vy.at(j);
          if (nx < 0 || nx >= H || ny < 0 || ny >= W) {
            continue;
          }
          if (!flag.at(nx).at(ny) && S.at(nx).at(ny) == '.') {
            if (nx == Dh && ny == Dw) {
              cout << i << endl;
              return 0;
            }
            vvp.at(i).emplace_back(nx, ny);
            sp.push(make_pair(nx, ny));
            flag.at(nx).at(ny) = true;
          }
        }
      }
    }
    for (auto &p : vvp.at(i)) {
      tie(x, y) = p;
      for (int j = -2; j <= 2; j++) {
        for (int k = -2; k <= 2; k++) {
          nx = x + j;
          ny = y + k;
          if (nx < 0 || nx >= H || ny < 0 || ny >= W) {
            continue;
          }
          if (!flag.at(nx).at(ny) && S.at(nx).at(ny) == '.') {
            if (nx == Dh && ny == Dw) {
              cout << i + 1 << endl;
              return 0;
            }
            vvp.at(i + 1).emplace_back(nx, ny);
            flag.at(nx).at(ny) = true;
          }
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int H, W, Ch, Cw, Dh, Dw;
vector<string> S;
vector<vector<vector<pair<int, int>>>> v;
vector<vector<bool>> flag;
int64_t ans = 1e10;
set<pair<int, int>> s;

void dfs(int x, int y, int64_t count) {
  if (ans == 0) {
    return;
  }
  int nx, ny;
  int diff = 0;
  for (auto &p : v.at(x).at(y)) {
    tie(nx, ny) = p;
    if (!flag.at(nx).at(ny)) {
      if (s.count(make_pair(nx, ny))) {
        if (!((nx == x && abs(ny - y) <= 1) || (abs(nx - x) <= 1 && ny == y))) {
          continue;
        }
      }
      flag.at(nx).at(ny) = true;
      if (!((nx == x && abs(ny - y) <= 1) || (abs(nx - x) <= 1 && ny == y))) {
        diff = 1;
      }
      if (nx == Dh && ny == Dw) {
        ans = min(ans, count + diff);
        flag.at(nx).at(ny) = false;
        return;
      }
      dfs(nx, ny, count + diff);
    }
  }
}

int main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  S.resize(H);
  v.resize(H, vector<vector<pair<int, int>>>(W));
  flag.resize(H, vector<bool>(W, false));
  vector<int> vx = {1, 0, -1, 0};
  vector<int> vy = {0, 1, 0, -1};
  for (int i = 0; i < H; i++) {
    cin >> S.at(i);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S.at(i).at(j) == '#') {
        continue;
      }
      for (int k = 0; k < 4; k++) {
        if (i + vx.at(k) < 0 || i + vx.at(k) >= H || j + vy.at(k) < 0 ||
            j + vy.at(k) >= W) {
          continue;
        }
        if (S.at(i + vx.at(k)).at(j + vy.at(k)) == '.') {
          v.at(i).at(j).emplace_back(i + vx.at(k), j + vy.at(k));
          s.insert(make_pair(i + vx.at(i), j + vy.at(k)));
        }
      }
      for (int k = -2; k <= 2; k++) {
        if ((i + k < 0) || (i + k >= H)) {
          continue;
        }
        for (int l = -2; l <= 2; l++) {
          if ((j + l < 0) || (j + l >= W)) {
            continue;
          }
          if ((k == 0 && abs(l) <= 1) || (abs(k) <= 1 && l == 0)) {
            continue;
          }
          if (S.at(i + k).at(j + l) == '.') {
            v.at(i).at(j).emplace_back(i + k, j + l);
          }
        }
      }
    }
  }
  flag.at(Ch).at(Cw) = true;
  dfs(Ch, Cw, 0);
  if (ans == 1e10) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}