Problem p01271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<int, int> Pi;

bool used[50][50][50][50];
char masL[50][50], masR[50][50];
int W, H;
Pi RinS, LenS; // スタート
const int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1};

int bfs() {
  queue<Pi> Rin, Len;
  Rin.push(RinS);
  Len.push(LenS);
  used[RinS.first][RinS.second][LenS.first][LenS.second] = true;
  while (!Rin.empty()) {
    Pi r = Rin.front(), l = Len.front();
    Rin.pop(), Len.pop();
    if (masL[l.first][l.second] == '%' && masR[r.first][r.second] == '%') {
      return true;
    }
    if (masL[l.first][l.second] != '%' && masR[r.first][r.second] != '%') {
      for (int i = 0; i < 4; i++) {
        int rny = min(H - 1, max(0, r.first + dy[i]));
        int rnx = min(W - 1, max(0, r.second + dx[i]));
        int lny = min(H - 1, max(0, l.first + dy[i]));
        int lnx = min(W - 1, max(0, l.second - dx[i]));
        // めんどい
        if (masL[lny][lnx] == '#') { // 元の位置に戻す！！
          lny = l.first, lnx = l.second;
        }
        if (masR[rny][rnx] == '#') {
          rny = r.first, rnx = r.second;
        }

        if (!used[rny][rnx][lny][lnx]) {
          used[rny][rnx][lny][lnx] = true;
          Rin.push(Pi(rny, rnx)), Len.push(Pi(lny, lnx));
        }
      }
    }
  }
  return false; // いけない!!
}
int main() {
  while (cin >> W >> H, W) {
    memset(used, false, sizeof(used));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> masL[i][j];
        if (masL[i][j] == 'L') {
          LenS = Pi(i, j);
          masL[i][j] = '.';
        }
      }
      for (int j = 0; j < W; j++) {
        cin >> masR[i][j];
        if (masR[i][j] == 'R') {
          RinS = Pi(i, j);
          masR[i][j] = '.';
        }
      }
    }
    bool judge = bfs();
    if (judge)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> Pi;

bool used[50][50][50][50];
char masL[50][50], masR[50][50];
int W, H;
Pi RinS, LenS; // スタート
const int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1};

bool isover(int y, int x) { return y < 0 || y >= H || x < 0 || x >= W; }

int bfs() {
  queue<Pi> Rin, Len;
  Rin.push(RinS);
  Len.push(LenS);
  used[RinS.first][RinS.second][LenS.first][LenS.second] = true;
  while (!Rin.empty()) {
    Pi r = Rin.front(), l = Len.front();
    if (masL[r.first][r.second] == '%' && masR[l.first][l.second] == '%') {
      return true;
    }
    for (int i = 0; i < 4; i++) {
      int rny = min(H - 1, max(0, r.first + dy[i]));
      int rnx = min(W - 1, max(0, r.second + dx[i]));
      int lny = min(H - 1, max(0, l.first + dy[i]));
      int lnx = min(W - 1, max(0, r.second - dx[i]));
      // めんどい
      if (masR[lny][lnx] == '#') { // 元の位置に戻す！！
        rny = r.first, rnx = r.second;
      }
      if (masL[rny][rnx] == '#') {
        lny = l.first, lnx = l.second;
      }

      if (!used[rny][rnx][lny][lnx]) {
        used[rny][rnx][lny][lnx] = true;
        Rin.push(Pi(rny, rnx)), Len.push(Pi(lny, lnx));
      }
    }
  }
  return false; // いけない!!
}
int main() {
  while (cin >> W >> H, W) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> masL[i][j];
        if (masL[i][j] == 'R') {
          LenS = Pi(i, j);
          masL[i][j] = '.';
        }
      }
      for (int j = 0; j < W; j++) {
        cin >> masR[i][j];
        if (masR[i][j] == 'L') {
          RinS = Pi(i, j);
          masR[i][j] = '.';
        }
      }
    }
    bool judge = bfs();
    if (judge)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}