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

#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ck(a, b) (0 <= (a) && (a) < b)
using namespace std;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
struct S {
  int lx, ly, rx, ry;
  S(int a, int b, int c, int d) : lx(a), ly(b), rx(c), ry(d) {}
};
bool operator<(const S &a, const S &b) {
  if (a.lx != b.lx)
    return a.lx < b.lx;
  if (a.ly != b.ly)
    return a.ly < b.ly;
  return a.rx != b.rx ? a.rx < b.rx : a.ry < b.ry;
}
bool V[50][50][50][50];
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    int lx, ly, rx, ry;
    string cavel[h], caver[h];
    rep(i, h) cin >> cavel[i] >> caver[i];
    rep(i, h) rep(j, w) {
      if (cavel[i][j] == 'L')
        ly = i, lx = j, cavel[i][j] = '.';
      if (caver[i][j] == 'R')
        ry = i, rx = j, caver[i][j] = '.';
    }
    vector<S> F;
    rep(i, h) rep(j, w) rep(k, h) rep(l, w) V[i][j][k][l] = 0;
    F.push_back(S(lx, ly, rx, ry));
    V[ly][lx][ry][rx] = 1;
    int goal = 0;
    while (!F.empty() && !goal) {
      vector<S> nF;
      rep(i, F.size()) rep(d, 4) {
        int clx = F[i].lx, cly = F[i].ly, crx = F[i].rx, cry = F[i].ry;
        int nlx = clx + dx[d], nly = cly + dy[d], nrx = crx - dx[d],
            nry = cry + dy[d];
        if (!ck(nlx, w) || !ck(nly, h) || cavel[nly][nlx] == '#')
          nlx = clx, nly = cly;
        if (!ck(nrx, w) || !ck(nry, h) || caver[nry][nrx] == '#')
          nrx = crx, nry = cry;
        if (nlx == clx && nly == cly && nrx == crx && nry == cry)
          continue;
        if (cavel[nly][nlx] == '%' ^ caver[nry][nrx] == '%')
          continue;
        if (cavel[nly][nlx] == '%' && caver[nry][nrx] == '%') {
          goal = 1;
          goto END;
        }
        if (!V[nly][nlx][nry][nrx]) {
          nF.push_back(S(nlx, nly, nrx, nry));
          V[nly][nlx][nry][nrx] = 1;
        }
      }
      F = nF;
    }
  END:
    cout << (goal ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ck(a, b) (0 <= (a) && (a) < b)
using namespace std;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
struct S {
  int lx, ly, rx, ry;
  S(int a, int b, int c, int d) : lx(a), ly(b), rx(c), ry(d) {}
};
bool operator<(const S &a, const S &b) {
  if (a.lx != b.lx)
    return a.lx < b.lx;
  if (a.ly != b.ly)
    return a.ly < b.ly;
  return a.rx != b.rx ? a.rx < b.rx : a.ry < b.ry;
}
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    int lx, ly, rx, ry;
    string cavel[h], caver[h];
    rep(i, h) cin >> cavel[i] >> caver[i];
    rep(i, h) rep(j, w) {
      if (cavel[i][j] == 'L')
        ly = i, lx = j, cavel[i][j] = '.';
      if (caver[i][j] == 'R')
        ry = i, rx = j, caver[i][j] = '.';
    }
    vector<S> F;
    set<S> V;
    F.push_back(S(lx, ly, rx, ry));
    V.insert(S(lx, ly, rx, ry));
    int goal = 0;
    while (!F.empty() && !goal) {
      vector<S> nF;
      rep(i, F.size()) rep(d, 4) {
        int clx = F[i].lx, cly = F[i].ly, crx = F[i].rx, cry = F[i].ry;
        int nlx = clx + dx[d], nly = cly + dy[d], nrx = crx - dx[d],
            nry = cry + dy[d];
        if (!ck(nlx, w) || !ck(nly, h) || cavel[nly][nlx] == '#')
          nlx = clx, nly = cly;
        if (!ck(nrx, w) || !ck(nry, h) || caver[nry][nrx] == '#')
          nrx = crx, nry = cry;
        if (nlx == clx && nly == cly && nrx == crx && nry == cry)
          continue;
        if (cavel[nly][nlx] == '%' ^ caver[nry][nrx] == '%')
          continue;
        if (cavel[nly][nlx] == '%' && caver[nry][nrx] == '%') {
          goal = 1;
          goto END;
        }
        if (V.find(S(nlx, nly, nrx, nry)) == V.end()) {
          nF.push_back(S(nlx, nly, nrx, nry));
          V.insert(S(nlx, nly, nrx, nry));
        }
      }
      F = nF;
    }
  END:
    cout << (goal ? "Yes" : "No") << endl;
  }
  return 0;
}