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

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

int W, H;
int dxy[4] = {1, -1, 0, 0};
vector<string> len, rin;
/*
bool solve(vector<vector<vector<vector<int> > > > &used, int ly, int lx, int ry,
int rx){ if(used[ly][lx][ry][rx]) return false; used[ly][lx][ry][rx] = true;

  // cout<<ly<<" "<<lx<<" "<<ry<<" "<<rx<<endl;
  for(int i = 0; i < 4; i++){
      int nly = ly + dxy[i], nlx = lx + dxy[3-i];
      int nry = ry + dxy[i], nrx = rx + dxy[3-i]*-1;
      if(nly < 0 || H <= nly || nlx < 0 || W <= nlx || len[nly][nlx] == '#'){
        nly -= dxy[i], nlx -= dxy[3-i];
      }

      if(nry < 0 || H <= nry || nrx < 0 || W <= nrx || rin[nry][nrx] == '#'){
        nry -= dxy[i], nrx -= dxy[3-i]*-1;
      }

      if(len[nly][nlx] == '%' &&  rin[nry][nrx] == '%') return true;

      if(solve(used,nly,nlx,nry,nrx)) return true;
  }

  return false;
}
*/

bool solve(vector<vector<vector<vector<int>>>> &used, int ly, int lx, int ry,
           int rx) {
  queue<pair<pair<int, int>, pair<int, int>>> Q;

  Q.push(make_pair(make_pair(ly, lx), make_pair(ry, rx)));

  while (!Q.empty()) {
    pair<pair<int, int>, pair<int, int>> P = Q.front();
    Q.pop();
    int ly = P.first.first, lx = P.first.second;
    int ry = P.second.first, rx = P.second.second;
    used[ly][lx][ry][rx] = true;

    for (int i = 0; i < 4; i++) {
      int nly = ly + dxy[i], nlx = lx + dxy[3 - i];
      int nry = ry + dxy[i], nrx = rx + dxy[3 - i] * -1;
      if (nly < 0 || H <= nly || nlx < 0 || W <= nlx || len[nly][nlx] == '#') {
        nly -= dxy[i], nlx -= dxy[3 - i];
      }

      if (nry < 0 || H <= nry || nrx < 0 || W <= nrx || rin[nry][nrx] == '#') {
        nry -= dxy[i], nrx -= dxy[3 - i] * -1;
      }

      if (len[nly][nlx] == '%' && rin[nry][nrx] == '%')
        return true;
      if (len[nly][nlx] == '%' || rin[nry][nrx] == '%')
        continue;

      if (used[nly][nlx][nry][nrx])
        continue;

      used[nly][nlx][nry][nrx] = true;

      Q.push(make_pair(make_pair(nly, nlx), make_pair(nry, nrx)));
    }
  }

  return false;
}
signed main() {

  while (true) {

    vector<vector<vector<vector<int>>>> used;
    int lsx, lsy, rsx, rsy;
    cin >> W >> H;

    if (!W && !H)
      break;

    len.clear();
    rin.clear();
    len.resize(H);
    rin.resize(H);
    used.resize(H, vector<vector<vector<int>>>(
                       W, vector<vector<int>>(H, vector<int>(W, 0))));

    for (int i = 0; i < H; i++) {
      cin >> len[i];
      cin >> rin[i];
      for (int j = 0; j < W; j++) {
        if (len[i][j] == 'L') {
          lsx = j;
          lsy = i;
        }
        if (rin[i][j] == 'R') {
          rsx = j;
          rsy = i;
        }
      }
    }

    cout << (solve(used, lsy, lsx, rsy, rsx) ? "Yes" : "No") << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

#define LX zahyou[0][0]
#define LY zahyou[0][1]
#define RX zahyou[1][0]
#define RY zahyou[1][1]
#define RROOM room[0][LY][LX]
#define LROOM room[1][RY][RX]
#define DINU                                                                   \\
  for (int i = 0; i < 2; i++) {                                                \\
    temp[i][0] = zahyou[i][0];                                                 \\
    temp[i][1] = zahyou[i][1];                                                 \\
  }

int W, H;

int foo(char room[2][60][60], int zahyou[2][2]) {
  int temp[2][2];
  if ((LX < 1 || LX > W) || (RX < 1 || RX > W) || (LY < 1 || LY > H) ||
      (LY < 0 || LY > H))
    return 0;
  if (RROOM == 'X' && LROOM == 'X')
    return 0;
  if (RROOM == '%' && LROOM == '%')
    return 1;
  if (RROOM == '%' || LROOM == '%')
    return 0;
  RROOM = 'X';
  LROOM = 'X';

  DINU

      if (room[0][LY + 1][LX] != '#') temp[0][1] = LY + 1;
  if (room[1][RY + 1][RX] != '#')
    temp[1][1] = RY + 1;
  if (foo(room, temp))
    return 1;

  DINU

      if (room[0][LY - 1][LX] != '#') temp[0][1] = LY - 1;
  if (room[1][RY - 1][RX] != '#')
    temp[1][1] = RY - 1;
  if (foo(room, temp))
    return 1;

  DINU

      if (room[0][LY][LX + 1] != '#') temp[0][0] = LX + 1;
  if (room[1][RY][RX - 1] != '#')
    temp[1][0] = RX - 1;
  if (foo(room, temp))
    return 1;

  DINU

      if (room[0][LY][LX - 1] != '#') temp[0][0] = LX - 1;
  if (room[1][RY][RX + 1] != '#')
    temp[1][0] = RX + 1;
  if (foo(room, temp))
    return 1;

  return 0;
}

int main() {
  string temp1, temp2;

  int zahyou[2][2];

  while (1) {
    char room[2][60][60] = {};
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;

    for (int i = 1; i <= H; i++) {
      cin >> temp1 >> temp2;
      for (int j = 1; j <= W; j++) {
        room[0][i][j] = temp1[j - 1];
        if (room[0][i][j] == 'L') {
          LX = j;
          LY = i;
        }
      }
      for (int j = 1; j <= W; j++) {
        room[1][i][j] = temp2[j - 1];
        if (room[1][i][j] == 'R') {
          RX = j;
          RY = i;
        }
      }
    }

    if (foo(room, zahyou))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}