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

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

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int H, W;
  cin >> H >> W;
  pair<int, int> start, goal, start2;
  cin >> start.first >> start.second;
  cin >> goal.first >> goal.second;
  start.first--;
  start.second--;
  goal.first--;
  goal.second--;
  vector<string> S(H);
  for (int i = 0; i < H; ++i)
    cin >> S[i];
  // 二度塗りでどうだ
  // 最初に歩きパターンを見てから、次にワープパターンを見る
  vector<vector<int>> Graph(H, vector<int>(W, -1));
  vector<vector<bool>> isSeen(H, vector<bool>(W, false));
  int count = 0;
  queue<pair<int, int>> que, que2;
  Graph[start.first][start.second] = count;
  que.push(start);
  que2.push(start);
  isSeen[start.first][start.second] = true;
  while (!que.empty() || !que2.empty()) {
    int nh, nw;
    while (!que.empty()) {
      auto v = que.front();
      que.pop();
      que2.push(v);
      for (int dir = 0; dir < 4; ++dir) {
        nh = v.first + dx[dir];
        nw = v.second + dy[dir];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W)
          continue;
        if (S[nh][nw] == '#')
          continue;
        if (!isSeen[nh][nw]) {
          isSeen[nh][nw] = true;
          Graph[nh][nw] = count;
          que.push({nh, nw});
        }
      }
    }
    count++;
    while (!que2.empty()) {
      auto v = que2.front();
      que2.pop();
      for (int i = -2; i <= 2; ++i) {
        for (int j = -2; j <= 2; ++j) {
          nh = v.first + i;
          nw = v.second + j;
          if ((abs(i) <= 1 && j == 0) || (abs(j) <= 1 && i == 0))
            continue;
          else {
            if (nh < 0 || nh >= H || nw < 0 || nw >= W)
              continue;
            if (S[nh][nw] == '#')
              continue;
            if (!isSeen[nh][nw]) {
              Graph[nh][nw] = count;
              isSeen[nh][nw] = true;
              que.push({nh, nw});
              start2 = {nh, nw};
            }
          }
        }
      }
    }
  }
  cout << Graph[goal.first][goal.second] << endl;
}


Error: -11

Fixed submission:

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

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int H, W;
  cin >> H >> W;
  pair<int, int> start, goal;
  cin >> start.first >> start.second;
  cin >> goal.first >> goal.second;
  start.first--;
  start.second--;
  goal.first--;
  goal.second--;
  vector<string> S(H);
  for (int i = 0; i < H; ++i)
    cin >> S[i];
  // 二度塗りでどうだ
  // 最初に歩きパターンを見てから、次にワープパターンを見る
  vector<vector<int>> Graph(H, vector<int>(W, -1));
  vector<vector<bool>> isSeen(H, vector<bool>(W, false));
  int count = 0;
  queue<pair<int, int>> que, que2;
  Graph[start.first][start.second] = count;
  que.push(start);
  que2.push(start);
  isSeen[start.first][start.second] = true;
  while (1) {
    int nh, nw;
    bool isContinue = false;
    while (!que.empty()) {
      auto v = que.front();
      que.pop();
      for (int dir = 0; dir < 4; ++dir) {
        nh = v.first + dx[dir];
        nw = v.second + dy[dir];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W)
          continue;
        if (S[nh][nw] == '#')
          continue;
        if (!isSeen[nh][nw]) {
          isSeen[nh][nw] = true;
          Graph[nh][nw] = count;
          que.push({nh, nw});
          que2.push({nh, nw});
        }
      }
    }
    count++;
    while (!que2.empty()) {
      auto v = que2.front();
      que2.pop();
      for (int i = -2; i <= 2; ++i) {
        for (int j = -2; j <= 2; ++j) {
          nh = v.first + i;
          nw = v.second + j;
          if ((abs(i) <= 1 && j == 0) || (abs(j) <= 1 && i == 0))
            continue;
          else {
            if (nh < 0 || nh >= H || nw < 0 || nw >= W)
              continue;
            if (S[nh][nw] == '#')
              continue;
            if (!isSeen[nh][nw]) {
              Graph[nh][nw] = count;
              isContinue = true;
            }
          }
        }
      }
    }
    if (!isContinue)
      break;
    else {
      que.push({nh, nw});
      que2.push({nh, nw});
      isSeen[nh][nw] = true;
    }
  }
  cout << Graph[goal.first][goal.second] << endl;
}