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

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

#define ll long long int
#define All(a) (a).begin(), (a).end()

queue<pair<int, int>> q, v;
// Remember v.shrink_to_fit() when erasing
void solve() {
  ll h, w, ch, cw, dh, dw, x, y;
  int X[4] = {-1, 0, 0, 1};
  int Y[4] = {0, 1, -1, 0};
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  ll M[h][w];
  string s[h];
  for (ll i = 0; i < h; i++) {
    cin >> s[i];
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        M[i][j] = -1;
      else
        M[i][j] = 10000000000;
    }
  }

  ll cnt = 0;
  M[ch][cw] = 0;
  q.push(make_pair(ch, cw));
  while (!q.empty() || !v.empty()) {
    while (!q.empty()) {
      auto tmp = q.front();
      v.push(tmp);
      q.pop();
      if (tmp.first == dh && tmp.second == dw) {
        printf("%lld\\n", M[dh][dw]);
        return;
      }
      for (int i = 0; i < 4; i++) {
        x = tmp.first + X[i];
        y = tmp.second + Y[i];
        if (x >= 0 && x < h && y >= 0 && y < w &&
            M[x][y] > M[tmp.first][tmp.second]) {
          M[x][y] = cnt;
          q.push(make_pair(x, y));
        }
      }
    }
    while (!v.empty()) {
      auto tmp = v.front();
      v.pop();
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          x = tmp.first + i;
          y = tmp.second + j;
          if (x >= 0 && x < h && y >= 0 && y < w && M[x][y] > cnt + 1) {
            M[x][y] = cnt + 1;
            q.push(make_pair(x, y));
          }
        }
      }
    }
    cnt++;
  }
  printf("-1\\n");
  return;
}
int main() {
  solve();
  return 0;
}

Error: 0

Fixed submission:

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

#define ll long long int
#define All(a) (a).begin(), (a).end()

queue<pair<int, int>> q, v;
// Remember v.shrink_to_fit() when erasing
void solve() {
  ll h, w, ch, cw, dh, dw, x, y;
  int X[4] = {-1, 0, 0, 1};
  int Y[4] = {0, 1, -1, 0};
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string S[h];
  ll M[h][w];
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      cin >> S[i][j];
      if (S[i][j] == '#')
        M[i][j] = -1;
      else
        M[i][j] = 10000;
    }
  }
  ll cnt = 0;
  M[ch][cw] = 0;
  q.push(make_pair(ch, cw));
  while (!q.empty() || !v.empty()) {
    while (!q.empty()) {
      auto tmp = q.front();
      v.push(tmp);
      q.pop();
      if (tmp.first == dh && tmp.second == dw) {
        printf("%lld\\n", M[dh][dw]);
        return;
      }
      for (int i = 0; i < 4; i++) {
        x = tmp.first + X[i];
        y = tmp.second + Y[i];
        if (x >= 0 && x < h && y >= 0 && y < w &&
            M[x][y] > M[tmp.first][tmp.second]) {
          M[x][y] = cnt;
          q.push(make_pair(x, y));
        }
      }
    }
    while (!v.empty()) {
      auto tmp = v.front();
      v.pop();
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          x = tmp.first + i;
          y = tmp.second + j;
          if (x >= 0 && x < h && y >= 0 && y < w && M[x][y] == 10000) {
            M[x][y] = cnt + 1;
            q.push(make_pair(x, y));
          }
        }
      }
    }
    cnt++;
  }
  printf("-1\\n");
  return;
}
int main() {
  solve();
  return 0;
}