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

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<pair<int, int>, pair<int, int>> pii;
#define REP(i, n) for (int i = 0; i < n; ++i)

bool state[50][50][50][50];

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

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    vector<string> left(h), right(h);
    REP(i, h) cin >> left[i] >> right[i];

    int lx, ly, rx, ry;
    int glx, gly, grx, gry;
    REP(i, h) REP(j, w) {
      if (left[i][j] == 'L')
        lx = j, ly = i, left[i][j] = '.';
      if (left[i][j] == '%')
        glx = j, gly = i, left[i][j] = '.';
    }
    REP(i, h) REP(j, w) {
      if (right[i][j] == 'R')
        rx = j, ry = i, right[i][j] = '.';
      if (right[i][j] == '%')
        grx = j, gry = i, right[i][j] = '.';
    }

    memset(state, false, sizeof(state));
    queue<pii> Q;
    Q.push(make_pair(make_pair(lx, ly), make_pair(rx, ry)));
    while (!Q.empty()) {
      pii now = Q.front();
      Q.pop();
      int lx = now.first.first, ly = now.first.second;
      int rx = now.second.first, ry = now.second.second;
      if (lx == glx && ly == gly && rx == grx && ry == gry) {
        goto SUCCESS;
      }
      if (lx == glx && ly == gly)
        continue;
      if (rx == grx && ry == gry)
        continue;
      if (state[lx][ly][rx][ry])
        continue;
      state[lx][ly][rx][ry] = true;
      for (int i = 0; i < 4; ++i) {
        int nlx = lx + dx[i], nly = ly + dy[i];
        int nrx = rx + dx[(i + 2) % 4], nry = ry + dy[i];
        nlx = min(w - 1, max(0, nlx));
        nrx = min(w - 1, max(0, nrx));
        nly = min(h - 1, max(0, nly));
        nry = min(h - 1, max(0, nry));
        if (nlx < 0 || nlx >= w || nrx < 0 || nrx >= w)
          continue;
        if (nry < 0 || nry >= h || nly < 0 || nly >= h)
          continue;
        if (left[nly][nlx] == '#') {
          nly = ly;
          nlx = lx;
        }
        if (right[nry][nrx] == '#') {
          nry = ry;
          nrx = rx;
        }
        Q.push(make_pair(make_pair(nlx, nly), make_pair(nrx, nry)));
      }
    }

    goto BAD;
  SUCCESS:
    cout << "Yes" << endl;
    continue;
  BAD:
    cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<pair<int, int>, pair<int, int>> pii;
#define REP(i, n) for (int i = 0; i < n; ++i)

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

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    vector<string> left(h), right(h);
    REP(i, h) cin >> left[i] >> right[i];

    int lx, ly, rx, ry;
    int glx, gly, grx, gry;
    REP(i, h) REP(j, w) {
      if (left[i][j] == 'L')
        lx = j, ly = i, left[i][j] = '.';
      if (left[i][j] == '%')
        glx = j, gly = i, left[i][j] = '.';
    }
    REP(i, h) REP(j, w) {
      if (right[i][j] == 'R')
        rx = j, ry = i, right[i][j] = '.';
      if (right[i][j] == '%')
        grx = j, gry = i, right[i][j] = '.';
    }

    set<pii> state;
    queue<pii> Q;
    Q.push(make_pair(make_pair(lx, ly), make_pair(rx, ry)));
    while (!Q.empty()) {
      pii now = Q.front();
      Q.pop();
      if (now.first.first == glx && now.first.second == gly &&
          now.second.first == grx && now.second.second == gry) {
        goto SUCCESS;
      }
      if (state.find(now) != state.end())
        continue;
      state.insert(now);
      int lx = now.first.first, ly = now.first.second;
      int rx = now.second.first, ry = now.second.second;
      for (int i = 0; i < 4; ++i) {
        int nlx = lx + dx[i], nly = ly + dy[i];
        int nrx = rx + dx[(i + 2) % 4], nry = ry + dy[i];
        if (nlx < 0 || nlx >= w || nrx < 0 || nrx >= w)
          continue;
        if (nry < 0 || nry >= h || nly < 0 || nly >= h)
          continue;
        if (left[nly][nlx] == '#') {
          nly = ly;
          nlx = lx;
        }
        if (right[nry][nrx] == '#') {
          nry = ry;
          nrx = rx;
        }
        Q.push(make_pair(make_pair(nlx, nly), make_pair(nrx, nry)));
      }
    }

    goto BAD;
  SUCCESS:
    cout << "Yes" << endl;
    continue;
  BAD:
    cout << "No" << endl;
  }
}