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

#include <iostream>
#include <queue>
#include <tuple>
using namespace std;

typedef tuple<int, int, int, int> T;

bool isChecked[50][50][50][50];
bool mapL[50][50]; // true:????????? false:????????????
bool mapR[50][50];

int main() {
  int W, H;
  while (cin >> W >> H, W) {
    int SRx, SRy, SLx, SLy, GRx, GRy, GLx, GLy;
    fill(mapL[0], mapL[50], false);
    fill(mapR[0], mapR[50], false);
    for (int i = 0; i < H; i++) {
      string strL, strR;
      cin >> strL >> strR;
      for (int j = 0; j < W; j++) {
        char cL, cR;
        cL = strL[j];
        switch (cL) {
        case '.':
          mapL[i][j] = true;
          break;
        case '#':
          break;
        case '%':
          mapL[i][j] = true;
          GLy = i;
          GLx = j;
          break;
        case 'L':
          mapL[i][j] = true;
          SLy = i;
          SLx = j;
          break;
        }
        cR = strR[W - 1 - j];
        switch (cR) {
        case '.':
          mapR[i][j] = true;
          break;
        case '#':
          break;
        case '%':
          mapR[i][j] = true;
          GRy = i;
          GRx = j;
          break;
        case 'R':
          mapR[i][j] = true;
          SRy = i;
          SRx = j;
          break;
        }
      }
    }
    fill(isChecked[0][0][0], isChecked[50][0][0], false);
    isChecked[SLx][SLy][SRx][SRy] = true;
    queue<T> que;
    que.emplace(SLx, SLy, SRx, SRy);
    bool ans = false;
    while (!que.empty()) {
      T t = que.front();
      que.pop();
      int Lx = get<0>(t), Ly = get<1>(t), Rx = get<2>(t), Ry = get<3>(t);
      if (Lx == GLx && Ly == GLy && Rx == GRx && Ry == GRy) {
        ans = true;
        while (!que.empty())
          que.pop();
        break;
      } else if ((Lx == GLx && Ly == GLy) || (Rx == GRx && Ry == GRy)) {
        continue;
      }
      int d[5] = {-1, 0, 1, 0, -1};
      for (int i = 0; i < 4; i++) {
        int nLx, nLy, nRx, nRy;
        int dx = d[i], dy = d[i + 1];
        if (Lx + dx < 0 || Lx + dx >= W || Ly + dy < 0 || Ly + dy >= H) {
          nLx = Lx, nLy = Ly;
        } else if (mapL[Ly + dy][Lx + dx]) {
          nLx = Lx + dx, nLy = Ly + dy;
        } else {
          nLx = Lx, nLy = Ly;
        }
        if (Rx + dx < 0 || Rx + dx >= W || Ry + dy < 0 || Ry + dy >= H) {
          nRx = Rx, nRy = Ry;
        } else if (mapR[Ry + dy][Rx + dx]) {
          nRx = Rx + dx, nRy = Ry + dy;
        } else {
          nRx = Rx, nRy = Ry;
        }
        if (nLx == Lx && nLy == Ly && nRx == Rx && nRy == Ry) {
          continue;
        } else if (!isChecked[nLx][nLy][nRx][nRy]) {
          isChecked[nLx][nLy][nRx][nRy] = true;
          que.emplace(nLx, nLy, nRx, nRy);
        }
      }
    }

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

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <tuple>
using namespace std;

typedef tuple<int, int, int, int> T;

bool isChecked[50][50][50][50];
bool mapL[50][50]; // true:????????? false:????????????
bool mapR[50][50];

int main() {
  int W, H;
  while (cin >> W >> H, W) {
    int SRx, SRy, SLx, SLy, GRx, GRy, GLx, GLy;
    fill(mapL[0], mapL[50], false);
    fill(mapR[0], mapR[50], false);
    for (int i = 0; i < H; i++) {
      string strL, strR;
      cin >> strL >> strR;
      for (int j = 0; j < W; j++) {
        char cL, cR;
        cL = strL[j];
        switch (cL) {
        case '.':
          mapL[i][j] = true;
          break;
        case '#':
          break;
        case '%':
          mapL[i][j] = true;
          GLy = i;
          GLx = j;
          break;
        case 'L':
          mapL[i][j] = true;
          SLy = i;
          SLx = j;
          break;
        }
        cR = strR[W - 1 - j];
        switch (cR) {
        case '.':
          mapR[i][j] = true;
          break;
        case '#':
          break;
        case '%':
          mapR[i][j] = true;
          GRy = i;
          GRx = j;
          break;
        case 'R':
          mapL[i][j] = true;
          SRy = i;
          SRx = j;
          break;
        }
      }
    }

    fill(isChecked[0][0][0], isChecked[50][0][0], false);
    isChecked[SLx][SLy][SRx][SRy] = true;
    queue<T> que;
    que.emplace(SLx, SLy, SRx, SRy);
    bool ans = false;
    while (!que.empty()) {
      T t = que.front();
      que.pop();
      int Lx = get<0>(t), Ly = get<1>(t), Rx = get<2>(t), Ry = get<3>(t);
      isChecked[Lx][Ly][Rx][Ry] = true;
      if (Lx == GLx && Ly == GLy && Rx == GRx && Ry == GRy) {
        ans = true;
        break;
      } else if ((Lx == GLx && Ly == GLy) || (Rx == GRx && Ry == GRy)) {
        continue;
      }
      int d[5] = {-1, 0, 1, 0, -1};
      for (int i = 0; i < 4; i++) {
        int nLx, nLy, nRx, nRy;
        int dx = d[i], dy = d[i + 1];
        if (Lx < 0 || Lx > W || Ly < 0 || Ly > H)
          continue;
        if (mapL[Lx + dx][Ly + dy]) {
          nLx = Lx + dx, nLy = Ly + dy;
        } else {
          nLx = Lx, nLy = Ly;
        }
        if (mapR[Rx + dx][Ry + dy]) {
          nRx = Rx + dx, nRy = Ry + dy;
        } else {
          nRx = Rx, nRy = Ry;
        }
        if (nLx == Lx && nLy == Ly && nRx == Rx && nRy == Ry) {
          continue;
        } else if (!isChecked[nLx][nLy][nRx][nRy]) {
          que.emplace(nLx, nLy, nRx, nRy);
        }
      }
    }
    if (ans) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}