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

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

pair<int, int> operator+(const pair<int, int> &x, const pair<int, int> &y) {
  return make_pair(x.first + y.first, x.second + y.second);
}
pair<int, int> plusHW(const pair<int, int> &x, const pair<int, int> &y,
                      const int H, const int W) {
  return make_pair(min(max(1, x.first + y.first), H),
                   min(max(1, x.second + y.second), W));
}

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw;
  cin >> Ch >> Cw;
  int Dh, Dw;
  cin >> Dh >> Dw;
  vector<vector<string>> S(H + 1, vector<string>(W + 1));
  vector<vector<bool>> wallOrVisited(H + 1, vector<bool>(W + 1));
  for (int h = 1; h <= H; ++h) {
    string str;
    cin >> str;
    for (int w = 1; w <= W; ++w) {
      S[h][w] = str[w - 1];
      wallOrVisited[h][w] = (S[h][w] == "#");
    }
  }
  vector<vector<int>> A(H + 1, vector<int>(W + 1, -1));
  queue<pair<int, int>> I;
  queue<pair<int, int>> R;
  vector<pair<int, int>> oneStep{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  vector<pair<int, int>> jump;
  for (int i = -2; i <= 2; ++i) {
    for (int j = -2; j <= 2; ++j) {
      jump.push_back(make_pair(i, j));
    }
  }
  jump.erase(find(jump.begin(), jump.end(), make_pair(0, 0)));
  for (auto x : oneStep) {
    jump.erase(find(jump.begin(), jump.end(), x));
  }

  wallOrVisited[Ch][Cw] = true;
  I.push(make_pair(Ch, Cw));
  int stage = 0;
  while (!I.empty()) {
    while (!I.empty()) {
      auto x = I.front();
      I.pop();
      R.push(x);
      for (auto d : oneStep) {
        auto y = plusHW(x, d, H, W);
        if (!wallOrVisited[y.first][y.second]) {
          A[y.first][y.second] = stage;
          I.push(y);
          wallOrVisited[y.first][y.second] = true;
        }
      }
    }
    ++stage;
    while (!R.empty()) {
      auto r = R.front();
      R.pop();
      for (auto d : jump) {
        auto x = plusHW(r, d, H, W);
        if (!wallOrVisited[x.first][x.second]) {
          A[x.first][x.second] = stage;
          I.push(x);
          wallOrVisited[x.first][x.second] = true;
        }
      }
    }
  }
  cout << A[Dh][Dw] << endl;
}

Error: -11

Fixed submission:

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

int main() {
  const ll INF = 1e10;
  int H, W;
  cin >> H >> W;
  int Ch, Dh;
  cin >> Ch >> Dh;
  int Cw, Dw;
  cin >> Cw >> Dw;
  vector<vector<string>> S(H, vector<string>(W));
  for (int h = 0; h < H; ++h) {
    for (int w = 0; w < W; ++w) {
      cin >> S[h][w];
    }
  }

  vector<vector<ll>> dp(H, vector<ll>(W, INF));
  for (int h = Ch - 1; h < Dh; ++h) {
    for (int w = Cw - 1; w < Dw; ++w) {
      // h+
      if (h + 2 < H) {
        if (S[h + 1][w] == ".") {
          dp[h + 1][w] = min(dp[h + 1][w], dp[h][w]);
        } else if (S[h + 2][w] == ".") {
          dp[h + 2][w] = min(dp[h + 2][w], dp[h][w] + 1);
        }
      } else if (h + 1 < H) {
        if (S[h + 1][w] == ".") {
          dp[h + 1][w] = min(dp[h + 1][w], dp[h][w]);
        }
      }
      // h-
      if (h - 2 >= 0) {
        if (S[h - 1][w] == ".") {
          dp[h - 1][w] = min(dp[h - 1][w], dp[h][w]);
        } else if (S[h - 2][w] == ".") {
          dp[h - 2][w] = min(dp[h - 2][w], dp[h][w] + 1);
        }
      } else if (h - 1 >= 0) {
        if (S[h - 1][w] == ".") {
          dp[h - 1][w] = min(dp[h - 1][w], dp[h][w]);
        }
      }
      // w+
      if (w + 2 < H) {
        if (S[h][w + 1] == ".") {
          dp[h][w + 1] = min(dp[h][w + 1], dp[h][w]);
        } else if (S[h + 2][w] == ".") {
          dp[h][w + 2] = min(dp[w + 2][w], dp[h][w] + 1);
        }
      } else if (h + 1 < H) {
        if (S[h][w + 1] == ".") {
          dp[h][w + 1] = min(dp[h][w + 1], dp[h][w]);
        }
      }

      // w-
      if (w - 2 >= 0) {
        if (S[h][w - 1] == ".") {
          dp[h][w - 1] = min(dp[h][w - 1], dp[h][w]);
        } else if (S[h][w - 2] == ".") {
          dp[h][w - 2] = min(dp[h][w - 2], dp[h][w] + 1);
        }
      } else if (w - 1 >= 0) {
        if (S[h][w - 1] == ".") {
          dp[h][w - 1] = min(dp[h][w - 1], dp[h][w]);
        }
      }
    }
  }
  cout << dp[Ch][Cw] << endl;
}