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

#include <bits/stdc++.h>
using namespace std;
char S[1001][1001];
int inf = 1001001001;
void check(int H, int W, int X_count) {
  cout << "The root is..." << endl;
  for (int j = 0; j < H; j++) {
    for (int k = 0; k < W; k++)
      cout << S[j][k];
    cout << endl;
  }
  cout << "X_count = " << X_count << endl;
}
int main() {
  int H, W, Ch, Cw, Dh, Dw, min_count = inf, count_warp = 0;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> S[i][j];
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

  queue<tuple<int, int, int>> que_walk, que_warp;

  tuple<int, int, int> X;
  X = make_tuple(Ch, Cw, 0);
  S[Cw][Ch] = '#';
  que_walk.push(X);
  que_warp.push(X);

  while (1) {
    // 徒歩での移動
    while (!que_walk.empty()) {
      X = que_walk.front();
      que_walk.pop();
      que_warp.push(X);
      int Xx = get<0>(X);
      int Xy = get<1>(X);
      int X_count = get<2>(X);
      for (int i = 0; i < 4; i++) {
        int new_x = Xx + dx[i], new_y = Xy + dy[i];
        if (0 <= new_x && new_x <= H - 1 && 0 <= new_y && new_y <= W - 1 &&
            S[new_x][new_y] != '#') {
          //(new_x,new_y)の位置は訪れたことがない
          S[new_x][new_y] = '#';
          if (new_x == Dh && new_y == Dw)
            min_count = min(min_count, X_count);
          X = make_tuple(new_x, new_y, X_count);
          que_walk.push(X);
          que_warp.push(X);
          /*
          S[new_x][new_y] = 'o';
          cout <<"Now, walking." << endl;
          check(H,W,X_count);
          S[new_x][new_y] = '#';
          */
        }
      }
    }
    // 徒歩で移動できる地点が列挙されたので、それらからワープで移動できる地点を列挙する
    // warpでの移動
    count_warp++;
    while (!que_warp.empty()) {
      X = que_warp.front();
      que_warp.pop();
      int Xx = get<0>(X);
      int Xy = get<1>(X);
      int X_count = get<2>(X);
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int new_x = Xx + i;
          int new_y = Xy + j;
          if (0 <= new_x && new_x <= H - 1 && 0 <= new_y && new_y <= W - 1 &&
              S[new_x][new_y] != '#') {
            //(new_x,new_y)の位置は訪れたことがない
            S[new_x][new_y] = '#';
            if (new_x == Dh && new_y == Dw)
              min_count = min(min_count, X_count + 1);
            X = make_tuple(new_x, new_y, X_count + 1);
            que_walk.push(
                X); // warpで移動したところからまた徒歩出移動出来る場所を列挙するので、queueに入れとく
            /*
            S[new_x][new_y] = 'o';
            cout << "Now, warping : " << count_warp << endl;
            check(H,W,X_count+1);
            S[new_x][new_y] = '#';
            */
          }
        }
      }
    }
    if (que_walk.empty() && que_warp.empty()) {
      if (min_count != inf)
        cout << min_count << endl;
      else
        cout << -1 << endl;
      return 0;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
char S[104][104];
int main() {
  int H, W, Ch, Cw, Dh, Dw, Xx, Xy, X_count, min_X_count = 1001001001, ans;
  bool arrivegoal = false;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Cw += 1;
  Ch += 1;
  Dh += 1;
  Dw += 1;
  for (int i = 0; i < 104; i++) {
    for (int j = 0; j < 104; j++) {
      S[i][j] = '#';
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> S[i + 2][j + 2];
    }
  }

  tuple<int, int, int> X; // x座標,y座標,ワープした回数;
  queue<tuple<int, int, int>> que;
  X = make_tuple(Ch, Cw, 0);
  que.push(X);

  // S[Dh][Dw] = 'G';

  while (1) {
    if (que.empty()) {
      if (arrivegoal)
        ans = min_X_count;
      else
        ans = -1;
      cout << ans << endl;
      return 0;
    }

    X = que.front();
    que.pop();
    Xx = get<0>(X);
    Xy = get<1>(X);
    X_count = get<2>(X);

    if (Xx == Dh && Xy == Dw) {
      arrivegoal = true;
      min_X_count = min(min_X_count, X_count);
    }

    if (S[Xx][Xy] != '#') {
      /*
      S[Xx][Xy] = 'o';
      cout << "The root is..." << endl;
      for(int m=0;m<H;m++){
          for(int n=0;n<W;n++){
              cout << S[m+2][n+2];
          }
          cout << endl;
      }
      cout << "count = " << X_count << endl;
      */

      S[Xx][Xy] = '#';

      // ワープor徒歩の処理
      for (int m = -2; m <= 2; m++) {
        for (int n = -2; n <= 2; n++) {
          if (m == 0 && n == 0)
            continue;
          int countplus = 1;
          if (m * n == 0 && abs(m + n) < 2)
            countplus = 0;
          X = make_tuple(Xx + n, Xy + m, X_count + countplus);
          que.push(X);
        }
      }
    }
  }
}