Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using LL = long long;

bool is_visited(vector<vector<int>> &num, vector<vector<int>> &S, int ig,
                int jg, int is, int js, int H, int W) {
  if (ig <= 0 || ig > H || jg <= 0 || jg > W) {
    return false;
  }
  if (num[ig][jg] == -1 && S[ig][jg] == 1) {
    return true;
  } else if ((num[ig][jg] >= 0 && num[ig][jg] > num[is][js]) &&
             S[ig][jg] == 1) {
    return true;
  }
  return false;
}

bool is_visited2(vector<vector<int>> &num, vector<vector<int>> &S, int ig,
                 int jg, int is, int js, int H, int W) {
  if (ig <= 0 || ig > H || jg <= 0 || jg > W) {
    return false;
  }
  if (num[ig][jg] == -1 && S[ig][jg] == 1) {
    return true;
  } else if ((num[ig][jg] >= 0 && num[ig][jg] > num[is][js] + 1) &&
             S[ig][jg] == 1) {
    return true;
  }
  return false;
}

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Dh, Cw, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  vector<vector<int>> num(H + 2, vector<int>(W + 2, -1));
  vector<vector<int>> S(H + 2, vector<int>(W + 2, -1));

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char a;
      cin >> a;
      if (a == '.') {
        S[i][j] = 1;
      }
    }
  }
  vector<queue<pair<int, int>>> v_que(H * W);
  queue<pair<int, int>> que;
  que.push(make_pair(Ch, Cw));
  v_que[0].push(make_pair(Ch, Cw));
  int qi = 0;
  num[Ch][Cw] = 0;
  while (qi < H * W) {
    pair<int, int> p = v_que[qi].front();
    v_que[qi].pop();
    int i = p.first;
    int j = p.second;
    if (is_visited(num, S, i + 1, j, i, j, H, W)) {
      num[i + 1][j] = num[i][j];
      v_que[qi].push(make_pair(i + 1, j));
    }
    if (is_visited(num, S, i, j + 1, i, j, H, W)) {
      num[i][j + 1] = num[i][j];
      v_que[qi].push(make_pair(i, j + 1));
    }
    if (is_visited(num, S, i - 1, j, i, j, H, W)) {
      num[i - 1][j] = num[i][j];
      v_que[qi].push(make_pair(i - 1, j));
    }
    if (is_visited(num, S, i, j - 1, i, j, H, W)) {
      num[i][j - 1] = num[i][j];
      v_que[qi].push(make_pair(i, j - 1));
    }

    if (is_visited2(num, S, i + 1, j + 1, i, j, H, W)) {
      num[i + 1][j + 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j + 1));
    }
    if (is_visited2(num, S, i + 1, j - 1, i, j, H, W)) {
      num[i + 1][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j - 1));
    }
    if (is_visited2(num, S, i - 1, j - 1, i, j, H, W)) {
      num[i - 1][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 1, j - 1));
    }
    if (is_visited2(num, S, i + 1, j - 1, i, j, H, W)) {
      num[i + 1][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j - 1));
    }

    if (is_visited2(num, S, i - 2, j - 2, i, j, H, W)) {
      num[i - 2][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j - 2));
    }
    if (is_visited2(num, S, i - 2, j - 1, i, j, H, W)) {
      num[i - 2][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j - 1));
    }
    if (is_visited2(num, S, i - 2, j, i, j, H, W)) {
      num[i - 2][j] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j));
    }
    if (is_visited2(num, S, i - 2, j + 1, i, j, H, W)) {
      num[i - 2][j + 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j + 1));
    }
    if (is_visited2(num, S, i - 2, j + 2, i, j, H, W)) {
      num[i - 2][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j + 2));
    }
    if (is_visited2(num, S, i - 1, j + 2, i, j, H, W)) {
      num[i - 1][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 1, j + 2));
    }
    if (is_visited2(num, S, i, j + 2, i, j, H, W)) {
      num[i][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i, j + 2));
    }
    if (is_visited2(num, S, i + 1, j + 2, i, j, H, W)) {
      num[i + 1][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j + 2));
    }
    if (is_visited2(num, S, i + 2, j + 2, i, j, H, W)) {
      num[i + 2][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j + 2));
    }
    if (is_visited2(num, S, i + 2, j + 1, i, j, H, W)) {
      num[i + 2][j + 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j + 1));
    }
    if (is_visited2(num, S, i + 2, j, i, j, H, W)) {
      num[i + 2][j] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j));
    }
    if (is_visited2(num, S, i + 2, j - 1, i, j, H, W)) {
      num[i + 2][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j - 1));
    }
    if (is_visited2(num, S, i + 2, j - 2, i, j, H, W)) {
      num[i + 2][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j - 2));
    }
    if (is_visited2(num, S, i + 1, j - 2, i, j, H, W)) {
      num[i + 1][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j - 2));
    }
    if (is_visited2(num, S, i, j - 2, i, j, H, W)) {
      num[i][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i, j - 2));
    }
    if (is_visited2(num, S, i - 1, j - 2, i, j, H, W)) {
      num[i - 1][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 1, j - 2));
    }
    if (v_que[qi].empty()) {
      qi++;
    }
  }

  cout << num[Dh][Dw] << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using LL = long long;

bool is_visited(vector<vector<int>> &num, vector<vector<int>> &S, int ig,
                int jg, int is, int js, int H, int W) {
  if (ig <= 0 || ig > H || jg <= 0 || jg > W) {
    return false;
  }
  if (num[ig][jg] == -1 && S[ig][jg] == 1) {
    return true;
  } else if ((num[ig][jg] >= 0 && num[ig][jg] > num[is][js]) &&
             S[ig][jg] == 1) {
    return true;
  }
  return false;
}

bool is_visited2(vector<vector<int>> &num, vector<vector<int>> &S, int ig,
                 int jg, int is, int js, int H, int W) {
  if (ig <= 0 || ig > H || jg <= 0 || jg > W) {
    return false;
  }
  if (num[ig][jg] == -1 && S[ig][jg] == 1) {
    return true;
  } else if ((num[ig][jg] >= 0 && num[ig][jg] > num[is][js] + 1) &&
             S[ig][jg] == 1) {
    return true;
  }
  return false;
}

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Dh, Cw, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  vector<vector<int>> num(H + 2, vector<int>(W + 2, -1));
  vector<vector<int>> S(H + 2, vector<int>(W + 2, -1));

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char a;
      cin >> a;
      if (a == '.') {
        S[i][j] = 1;
      }
    }
  }
  vector<queue<pair<int, int>>> v_que(H * W);
  queue<pair<int, int>> que;
  que.push(make_pair(Ch, Cw));
  v_que[0].push(make_pair(Ch, Cw));
  int qi = 0;
  num[Ch][Cw] = 0;
  while (qi < H * W) {
    pair<int, int> p = v_que[qi].front();
    v_que[qi].pop();
    int i = p.first;
    int j = p.second;
    if (is_visited(num, S, i + 1, j, i, j, H, W)) {
      num[i + 1][j] = num[i][j];
      v_que[qi].push(make_pair(i + 1, j));
    }
    if (is_visited(num, S, i, j + 1, i, j, H, W)) {
      num[i][j + 1] = num[i][j];
      v_que[qi].push(make_pair(i, j + 1));
    }
    if (is_visited(num, S, i - 1, j, i, j, H, W)) {
      num[i - 1][j] = num[i][j];
      v_que[qi].push(make_pair(i - 1, j));
    }
    if (is_visited(num, S, i, j - 1, i, j, H, W)) {
      num[i][j - 1] = num[i][j];
      v_que[qi].push(make_pair(i, j - 1));
    }

    if (is_visited2(num, S, i + 1, j + 1, i, j, H, W)) {
      num[i + 1][j + 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j + 1));
    }
    if (is_visited2(num, S, i + 1, j - 1, i, j, H, W)) {
      num[i + 1][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j - 1));
    }
    if (is_visited2(num, S, i - 1, j - 1, i, j, H, W)) {
      num[i - 1][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 1, j - 1));
    }
    if (is_visited2(num, S, i + 1, j - 1, i, j, H, W)) {
      num[i + 1][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j - 1));
    }

    if (is_visited2(num, S, i - 2, j - 2, i, j, H, W)) {
      num[i - 2][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j - 2));
    }
    if (is_visited2(num, S, i - 2, j - 1, i, j, H, W)) {
      num[i - 2][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j - 1));
    }
    if (is_visited2(num, S, i - 2, j, i, j, H, W)) {
      num[i - 2][j] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j));
    }
    if (is_visited2(num, S, i - 2, j + 1, i, j, H, W)) {
      num[i - 2][j + 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j + 1));
    }
    if (is_visited2(num, S, i - 2, j + 2, i, j, H, W)) {
      num[i - 2][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 2, j + 2));
    }
    if (is_visited2(num, S, i - 1, j + 2, i, j, H, W)) {
      num[i - 1][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 1, j + 2));
    }
    if (is_visited2(num, S, i, j + 2, i, j, H, W)) {
      num[i][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i, j + 2));
    }
    if (is_visited2(num, S, i + 1, j + 2, i, j, H, W)) {
      num[i + 1][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j + 2));
    }
    if (is_visited2(num, S, i + 2, j + 2, i, j, H, W)) {
      num[i + 2][j + 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j + 2));
    }
    if (is_visited2(num, S, i + 2, j + 1, i, j, H, W)) {
      num[i + 2][j + 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j + 1));
    }
    if (is_visited2(num, S, i + 2, j, i, j, H, W)) {
      num[i + 2][j] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j));
    }
    if (is_visited2(num, S, i + 2, j - 1, i, j, H, W)) {
      num[i + 2][j - 1] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j - 1));
    }
    if (is_visited2(num, S, i + 2, j - 2, i, j, H, W)) {
      num[i + 2][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 2, j - 2));
    }
    if (is_visited2(num, S, i + 1, j - 2, i, j, H, W)) {
      num[i + 1][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i + 1, j - 2));
    }
    if (is_visited2(num, S, i, j - 2, i, j, H, W)) {
      num[i][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i, j - 2));
    }
    if (is_visited2(num, S, i - 1, j - 2, i, j, H, W)) {
      num[i - 1][j - 2] = num[i][j] + 1;
      v_que[qi + 1].push(make_pair(i - 1, j - 2));
    }
    if (v_que[qi].empty()) {
      qi++;
      if (v_que[qi].empty()) {
        break;
      }
    }
  }

  cout << num[Dh][Dw] << endl;

  return 0;
}