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

#include <bits/stdc++.h>

using namespace std;

const int OfLen = 0;
const int OfRin = 1;

struct Point {
  int x, y;
  Point() {}
  Point(int x, int y) : x(x), y(y) {}
};

struct TPoint {
  Point Len, Rin;
  TPoint() {}
  TPoint(Point Len, Point Rin) : Len(Len), Rin(Rin) {}
};

int W, H;
string room[2][55];

bool isInRoom(int whose, Point p) {
  if (0 <= p.x && p.x < W && 0 <= p.y && p.y < H &&
      room[whose][p.y][p.x] != '#')
    return true;
  return false;
}

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

bool used[55][55][55][55];

bool isOpenGate(TPoint twinspos) {
  memset(used, false, sizeof(used));

  queue<TPoint> que;
  que.push(twinspos);

  while (!que.empty()) {
    TPoint tp = que.front();
    que.pop();
    Point len = tp.Len, rin = tp.Rin;

    if (used[len.x][len.y][rin.x][rin.y])
      continue;

    used[len.x][len.y][rin.x][rin.y] = true;

    if (room[OfLen][len.y][len.x] == '%' && room[OfRin][rin.y][rin.x] == '%')
      return true;
    if (room[OfLen][len.y][len.x] == '%' || room[OfRin][rin.y][rin.x] == '%')
      continue;

    for (int i = 0; i < 4; i++) {
      Point nextLen = len, nextRin = rin;

      Point IfLenMove = Point(len.x + dx[i], len.y + dy[i]);
      if (isInRoom(OfLen, IfLenMove))
        nextLen = IfLenMove;

      Point IfRinMove = Point(rin.x - dx[i], rin.y + dy[i]);
      if (isInRoom(OfRin, IfRinMove))
        nextRin = IfRinMove;

      if (used[nextLen.x][nextLen.y][nextRin.x][nextRin.y])
        continue;

      que.push(TPoint(nextLen, nextRin));
    }
  }
  return false;
}

int main() {
  while (cin >> W >> H, W || H) {
    TPoint twinspos;
    for (int i = 0; i < H; i++) {
      cin >> room[OfLen][i] >> room[OfRin][i];
      for (int j = 0; j < W; j++) {
        if (room[OfLen][i][j] == 'L')
          twinspos.Len = Point(j, i);
        if (room[OfRin][i][j] == 'R')
          twinspos.Rin = Point(j, i);
      }
    }
    cout << (isOpenGate(twinspos) ? "Yes" : "No") << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int OfLen = 0;
const int OfRin = 1;

struct Point {
  int x, y;
  Point() {}
  Point(int x, int y) : x(x), y(y) {}
};

struct TPoint {
  Point Len, Rin;
  TPoint() {}
  TPoint(Point Len, Point Rin) : Len(Len), Rin(Rin) {}
};

bool isInRoom(Point p, string room[55], int w, int h) {
  if (0 <= p.x && p.x < w && 0 <= p.y && p.y < h && room[p.y][p.x] != '#')
    return true;
  return false;
}

bool isOpenGate(int w, int h, string room[2][55], TPoint twinspos) {
  static int dx[] = {0, 1, 0, -1};
  static int dy[] = {1, 0, -1, 0};

  bool route[2][55][55] = {{{false}}};
  route[OfLen][twinspos.Len.y][twinspos.Len.x] = true;
  route[OfRin][twinspos.Rin.y][twinspos.Rin.x] = true;

  queue<TPoint> que;
  que.push(twinspos);

  while (!que.empty()) {
    TPoint tp = que.front();
    que.pop();
    Point len = tp.Len, rin = tp.Rin;

    route[OfLen][len.y][len.x] = true;
    route[OfRin][rin.y][rin.x] = true;

    if (room[OfLen][len.y][len.x] == '%' && room[OfRin][rin.y][rin.x] == '%')
      return true;

    for (int i = 0; i < 4; i++) {
      Point nextLen = len, nextRin = rin;

      Point lenIfMove = Point(len.x + dx[i], len.y + dy[i]);
      if (isInRoom(lenIfMove, room[OfLen], w, h))
        nextLen = lenIfMove;

      Point rinIfMove = Point(rin.x - dx[i], rin.y + dy[i]);
      if (isInRoom(rinIfMove, room[OfRin], w, h))
        nextRin = rinIfMove;

      if (route[OfLen][nextLen.y][nextLen.x] &&
          route[OfRin][nextRin.y][nextRin.x])
        continue;

      que.push(TPoint(nextLen, nextRin));
    }
  }
  return false;
}

int main() {
  int W, H;
  while (cin >> W >> H, W || H) {
    string room[2][55];
    TPoint twinspos;
    for (int i = 0; i < H; i++) {
      cin >> room[OfLen][i] >> room[OfRin][i];
      for (int j = 0; j < W; j++) {
        if (room[OfLen][i][j] == 'L')
          twinspos.Len = Point(j, i);
        if (room[OfRin][i][j] == 'R')
          twinspos.Rin = Point(j, i);
      }
    }
    cout << (isOpenGate(W, H, room, twinspos) ? "Yes" : "No") << endl;
  }
}