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

#include <bits/stdc++.h>
using namespace std;
#define MAX_W 50
#define MAX_H 50
// 深さ優先探索で行けるんじゃないか？

bool C[52][52][52][52];
int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};
vector<vector<bool>> Room_L(MAX_H + 2, vector<bool>(MAX_W + 2, false));
vector<vector<bool>> Room_R(MAX_H + 2, vector<bool>(MAX_W + 2, false));
int si_L, si_R, sj_L, sj_R, gi_L, gi_R, gj_L, gj_R; // スタートとゴールの座標

bool Err(int n) { return n < 1 || 50 < n; }

int main() {

  // cout << "ok" << endl;
  while (1) {

    // 入力
    // cout << "ol" << endl;
    int W, H;
    cin >> W >> H;
    // cout << H << " " << W << endl;
    if (W == 0)
      break;

    // cout << "u" << endl;

    for (int i = 0; i <= H + 1; i++) {
      for (int j = 0; j <= W + 1; j++) {
        Room_L[i][j] = Room_R[i][j] = false;
      }
    }

    // cout << "d" << endl;

    memset(C, 0, sizeof(C));

    // cout << "37" << endl;
    for (int i = 1; i <= H; i++) {
      // L
      for (int j = 1; j <= W; j++) {
        char input;
        cin >> input;
        if (input == '#')
          continue;
        else {
          Room_L[i][j] = true;

          if (input == '%') {
            gi_L = i;
            gj_L = j;
          }

          if (input == 'L') {
            si_L = i;
            sj_L = j;
          }
        }
      }

      // cout << "58" << endl;
      // R
      for (int j = 1; j <= W; j++) {
        char input;
        cin >> input;
        if (input == '#')
          continue;
        else {
          Room_R[i][j] = true;

          if (input == '%') {
            gi_R = i;
            gj_R = j;
          }

          if (input == 'R') {
            si_R = i;
            sj_R = j;
          }
        }
      }
    }

    // cout << "79" << endl;

    // 幅優先をする
    // set<pair<pair<int, int>, pair<int, int> > > s;

    queue<pair<pair<int, int>, pair<int, int>>> wait; // 左の座標　右の座標
    wait.push({{si_L, sj_L}, {si_R, sj_R}});
    // s.insert({ {si_L, sj_L}, {si_R, sj_R} });
    C[si_L][sj_L][si_R][sj_R] = true;

    bool can = false;
    int A = 0;

    // cout << H << " " << W << endl;
    while (!wait.empty()) { // キュー画空になるまで

      // if(W == 50 && H == 50 && A < 100000) cout << A << endl;
      // キューの先頭要素を取り出す
      int nowi_L = wait.front().first.first;
      int nowj_L = wait.front().first.second;
      int nowi_R = wait.front().second.first;
      int nowj_R = wait.front().second.second;
      if (Err(nowi_L) || Err(nowi_R) || Err(nowj_L) || Err(nowj_R))
        cout << nowi_L << " " << nowj_L << "  " << nowi_R << " " << nowj_R
             << endl;
      wait.pop();
      A++;
      // 四方向を探索
      for (int k = 0; k < 4; k++) {
        int nexti_L = nowi_L + di[k];
        int nextj_L = nowj_L + dj[k];
        int nexti_R = nowi_R + di[k];
        int nextj_R = nowj_R - dj[k];
        // cout << " " << nexti_L << " " << nextj_L << "  " << nexti_R << " " <<
        // nextj_R << endl;

        bool L = Room_L[nexti_L][nextj_L];
        bool R = Room_R[nexti_R][nextj_R];
        // cout << " " << L << " " << R << endl;

        // その方向に進めるなら
        if (nexti_L == gi_L && nextj_L == gj_L && nexti_R == gi_R &&
            nextj_R == gj_R) {
          can = true;
          break;
        } else if ((nexti_L == gi_L && nextj_L == gj_L) ||
                   (nexti_R == gi_R && nextj_R == gj_R)) {
          continue;
        } else if (L && R && !C[nexti_L][nextj_L][nexti_R][nextj_R]) {
          wait.push({{nexti_L, nextj_L}, {nexti_R, nextj_R}});
          C[nexti_L][nextj_L][nexti_R][nextj_R] = true;
        } else if (!L && R && !C[nowi_L][nowj_L][nexti_R][nextj_R]) {
          wait.push({{nowi_L, nowj_L}, {nexti_R, nextj_R}});
          C[nowi_L][nowj_L][nexti_R][nextj_R] = true;
        } else if (L && !R && !C[nexti_L][nextj_L][nowi_R][nowj_R]) {
          wait.push({{nexti_L, nextj_L}, {nowi_R, nowj_R}});
          C[nexti_L][nextj_L][nowi_R][nowj_R] = true;
        }
      }
    }

    if (can)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_W 10
#define MAX_H 10
// 深さ優先探索で行けるんじゃないか？

bool C[52][52][52][52];
int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};
vector<vector<bool>> Room_L(MAX_H + 2, vector<bool>(MAX_W + 2, false));
vector<vector<bool>> Room_R(MAX_H + 2, vector<bool>(MAX_W + 2, false));
int si_L, si_R, sj_L, sj_R, gi_L, gi_R, gj_L, gj_R; // スタートとゴールの座標

int main() {

  // cout << "ok" << endl;
  while (1) {

    // 入力
    // cout << "ol" << endl;
    int W, H;
    cin >> W >> H;
    if (W == 0)
      break;

    for (int i = 0; i <= H + 1; i++) {
      for (int j = 0; j <= W + 1; j++) {
        Room_L[i][j] = Room_R[i][j] = false;
      }
    }

    memset(C, 0, sizeof(C));

    for (int i = 1; i <= H; i++) {
      // L
      for (int j = 1; j <= W; j++) {
        char input;
        cin >> input;
        if (input == '#')
          continue;
        else {
          Room_L[i][j] = true;

          if (input == '%') {
            gi_L = i;
            gj_L = j;
          }

          if (input == 'L') {
            si_L = i;
            sj_L = j;
          }
        }
      }

      // R
      for (int j = 1; j <= W; j++) {
        char input;
        cin >> input;
        if (input == '#')
          continue;
        else {
          Room_R[i][j] = true;

          if (input == '%') {
            gi_R = i;
            gj_R = j;
          }

          if (input == 'R') {
            si_R = i;
            sj_R = j;
          }
        }
      }
    }

    // 幅優先をする
    // set<pair<pair<int, int>, pair<int, int> > > s;

    queue<pair<pair<int, int>, pair<int, int>>> wait; // 左の座標　右の座標
    wait.push({{si_L, sj_L}, {si_R, sj_R}});
    // s.insert({ {si_L, sj_L}, {si_R, sj_R} });
    C[si_L][sj_L][si_R][sj_R] = true;

    bool can = false;

    while (!wait.empty()) { // キュー画空になるまで

      // キューの先頭要素を取り出す
      int nowi_L = wait.front().first.first;
      int nowj_L = wait.front().first.second;
      int nowi_R = wait.front().second.first;
      int nowj_R = wait.front().second.second;
      // cout << nowi_L << " " << nowj_L << "  " << nowi_R << " " << nowj_R <<
      // endl;
      wait.pop();

      // 四方向を探索
      for (int k = 0; k < 4; k++) {
        int nexti_L = nowi_L + di[k];
        int nextj_L = nowj_L + dj[k];
        int nexti_R = nowi_R + di[k];
        int nextj_R = nowj_R - dj[k];
        // cout << " " << nexti_L << " " << nextj_L << "  " << nexti_R << " " <<
        // nextj_R << endl;

        bool L = Room_L[nexti_L][nextj_L];
        bool R = Room_R[nexti_R][nextj_R];
        // cout << " " << L << " " << R << endl;

        // その方向に進めるなら
        if (nexti_L == gi_L && nextj_L == gj_L && nexti_R == gi_R &&
            nextj_R == gj_R) {
          can = true;
          break;
        } else if ((nexti_L == gi_L && nextj_L == gj_L) ||
                   (nexti_R == gi_R && nextj_R == gj_R)) {
          continue;
        } else if (R && L && !C[nexti_L][nextj_L][nexti_R][nextj_R]) {
          wait.push({{nexti_L, nextj_L}, {nexti_R, nextj_R}});
          C[nexti_L][nextj_L][nexti_R][nextj_R] = true;
        } else if (R && !L && !C[nowi_L][nowj_L][nexti_R][nextj_R]) {
          wait.push({{nowi_L, nowj_L}, {nexti_R, nextj_R}});
          C[nowi_L][nowj_L][nexti_R][nextj_R] = true;
        } else if (!R && L && !C[nexti_L][nextj_L][nowi_R][nowj_R]) {
          wait.push({{nexti_L, nextj_L}, {nowi_R, nowj_R}});
          C[nexti_L][nextj_L][nowi_R][nowj_R] = true;
        }
      }
    }

    if (can)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}