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

#include <iostream>
#include <map>
#include <queue>
#include <set>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> node;
int ldx[4] = {1, 0, -1, 0};
int rdx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

char l[52][52];
char r[52][52];
bool sets[52][52][52][52];
int main(void) {
  int w, h;
  while (cin >> w >> h, w | h) {
    for (int x1 = 0; x1 <= w + 1; x1++)
      for (int y1 = 0; y1 <= h + 1; y1++)
        for (int x2 = 0; x2 <= w + 1; x2++)
          for (int y2 = 0; y2 <= h + 1; y2++)
            sets[x1][y1][x2][y2] = false;
    int plx, ply, prx, pry;
    int slx, sly, srx, sry;
    for (int x = 0; x <= w + 1; x++) {
      l[x][0] = r[x][0] = '#';
      l[x][h + 1] = r[x][h + 1] = '#';
    }
    for (int y = 0; y <= h + 1; y++) {
      l[0][y] = r[0][y] = '#';
      l[w + 1][y] = r[w + 1][y] = '#';
    }
    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        cin >> l[x][y];
        if (l[x][y] == '%')
          plx = x, ply = y;
        if (l[x][y] == 'L')
          slx = x, sly = y;
      }
      for (int x = 1; x <= w; x++) {
        cin >> r[x][y];
        if (r[x][y] == '%')
          prx = x, pry = y;
        if (r[x][y] == 'R')
          srx = x, sry = y;
      }
    }
    /*for(int y=0;y<=h+1;y++){
            for(int x=0;x<=w+1;x++){
                    cout << l[x][y];
            }
            cout <<" " ;
            for(int x=0;x<=w+1;x++){
                    cout<< r[x][y];
            }
            cout << endl;
    }*/
    queue<node> q;
    node s, g;
    s = node(P(slx, sly), P(srx, sry));
    g = node(P(plx, ply), P(prx, pry));
    q.push(s);
    sets[slx][sly][srx][sry] = true;
    while (!q.empty()) {
      node cur = q.front();
      q.pop();
      //	cout << cur.F.F << " "<< cur.F.S << " "<< cur.S.F << " "<<
      //cur.S.S << endl;
      if (cur == g)
        break;
      if ((cur.F.F == plx && cur.F.S == ply) ||
          (cur.S.F == prx && cur.S.S == pry))
        continue;
      for (int i = 0; i < 4; i++) {
        int lnx = cur.F.F + ldx[i], lny = cur.F.S + dy[i],
            rnx = cur.S.F + rdx[i], rny = cur.S.S + dy[i];
        if (l[lnx][lny] == '#' && r[rnx][rny] == '#')
          continue;
        if (l[lnx][lny] == '#')
          lnx = cur.F.F, lny = cur.F.S;
        if (r[rnx][rny] == '#')
          rnx = cur.S.F, rny = cur.S.S;
        bool lok = false, rok = false;
        if (l[lnx][lny] == '%')
          lok = true;
        if (r[rnx][rny] == '%')
          rok = true;
        //	cout << lok << rok << (lok^rok) << endl;
        node c = node(P(lnx, lny), P(rnx, rny));
        //	if(i==1|| i==3)
        //	cout << lnx << " " << lny << " " << rnx << " " << rny << endl;
        if (!sets[lnx][lny][rnx][rny] && !(lok ^ rok))
          sets[lnx][lny][rnx][rny] = true, q.push(c);
      }
    }
    if (sets[plx][ply][prx][pry])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <set>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> node;
int ldx[4] = {1, 0, -1, 0};
int rdx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

char l[52][52];
char r[52][52];

int main(void) {
  int w, h;
  while (cin >> w >> h, w | h) {
    set<node> sets;
    int plx, ply, prx, pry;
    int slx, sly, srx, sry;
    for (int x = 0; x <= w + 1; x++) {
      l[x][0] = r[x][0] = '#';
      l[x][h + 1] = r[x][h + 1] = '#';
    }
    for (int y = 0; y <= h + 1; y++) {
      l[0][y] = r[0][y] = '#';
      l[w + 1][y] = r[w + 1][y] = '#';
    }
    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        cin >> l[x][y];
        if (l[x][y] == '%')
          plx = x, ply = y;
        if (l[x][y] == 'L')
          slx = x, sly = y;
      }
      for (int x = 1; x <= w; x++) {
        cin >> r[x][y];
        if (r[x][y] == '%')
          prx = x, pry = y;
        if (r[x][y] == 'R')
          srx = x, sry = y;
      }
    }
    /*for(int y=0;y<=h+1;y++){
            for(int x=0;x<=w+1;x++){
                    cout << l[x][y];
            }
            cout <<" " ;
            for(int x=0;x<=w+1;x++){
                    cout<< r[x][y];
            }
            cout << endl;
    }*/
    queue<node> q;
    node s, g;
    s = node(P(slx, sly), P(srx, sry));
    g = node(P(plx, ply), P(prx, pry));
    q.push(s);
    sets.insert(s);
    while (!q.empty()) {
      node cur = q.front();
      q.pop();
      // cout << cur.F.F << " "<< cur.F.S << " "<< cur.S.F << " "<< cur.S.S <<
      // endl;
      if (cur == g)
        break;
      if ((cur.F.F == plx && cur.F.S == ply) ||
          (cur.S.F == prx && cur.F.S == pry))
        continue;
      for (int i = 0; i < 4; i++) {
        int lnx = cur.F.F + ldx[i], lny = cur.F.S + dy[i],
            rnx = cur.S.F + rdx[i], rny = cur.S.S + dy[i];
        if (l[lnx][lny] == '#' && r[rnx][rny] == '#')
          continue;
        if (l[lnx][lny] == '#')
          lnx = cur.F.F, lny = cur.F.S;
        if (r[rnx][rny] == '#')
          rnx = cur.S.F, rny = cur.S.S;
        node c = node(P(lnx, lny), P(rnx, rny));
        if (sets.find(c) == sets.end())
          sets.insert(c), q.push(c);
      }
    }
    if (sets.find(g) != sets.end())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}