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

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

bool dis[52][52][52][52];

typedef pair<pair<int, int>, pair<int, int>> status;
#define LEFT first
#define RIGHT second
#define X first
#define Y second

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

int main() {
  int w, h;
  while (true) {
    vector<string> left, right;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    string block;
    rep(i, w + 2) block += '#';
    left.push_back(block);
    right.push_back(block);
    rep(i, h) {
      string str;
      cin >> str;
      left.push_back("#" + str + "#");
      cin >> str;
      right.push_back("#" + str + "#");
    }
    left.push_back(block);
    right.push_back(block);
    memset(dis, 0, sizeof(dis));
    queue<status> que;
    int lx, ly, rx, ry;
    rep(i, left.size()) rep(j, left[0].size()) {
      if (left[i][j] == 'L') {
        lx = j;
        ly = i;
      }
      if (right[i][j] == 'R') {
        rx = j;
        ry = i;
      }
    }
    status s;
    s.LEFT.X = lx;
    s.LEFT.Y = ly;
    s.RIGHT.X = rx;
    s.RIGHT.Y = ry;
    que.push(s);
    while (!que.empty()) {
      status now = que.front();
      que.pop();
      if (dis[now.LEFT.X][now.LEFT.Y][now.RIGHT.X][now.RIGHT.Y]) {
        continue;
      }
      dis[now.LEFT.X][now.LEFT.Y][now.RIGHT.X][now.RIGHT.Y] = true;
      rep(i, 4) {
        status next = now;
        next.LEFT.X += dx[i];
        next.LEFT.Y += dy[i];
        next.RIGHT.X -= dx[i];
        next.RIGHT.Y += dy[i];
        if (left[next.LEFT.Y][next.LEFT.X] == '#') {
          next.LEFT.X -= dx[i];
          next.LEFT.Y -= dy[i];
        }
        if (right[next.RIGHT.Y][next.RIGHT.X] == '#') {
          next.RIGHT.X += dx[i];
          next.RIGHT.Y -= dy[i];
        }
        if (left[next.LEFT.Y][next.LEFT.X] == '%' &&
            right[next.RIGHT.Y][next.RIGHT.X] == '%')
          goto aaa;
        if (left[next.LEFT.Y][next.LEFT.X] == '%')
          continue;
        if (right[next.RIGHT.Y][next.RIGHT.X] == '%')
          continue;
        que.push(next);
      }
    }
    cout << "No" << endl;
    continue;
  aaa:;
    cout << "Yes" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int dis[60][60][60][60];

typedef pair<pair<int, int>, pair<int, int>> status;
#define LEFT first
#define RIGHT second
#define X first
#define Y second

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

int main() {
  int w, h;
  while (true) {
    vector<string> left, right;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    string block;
    rep(i, w + 2) block += '#';
    left.push_back(block);
    right.push_back(block);
    rep(i, h) {
      string str;
      cin >> str;
      left.push_back("#" + str + "#");
      cin >> str;
      right.push_back("#" + str + "#");
    }
    left.push_back(block);
    right.push_back(block);
    memset(dis, -1, sizeof(dis));
    queue<status> que;
    int lx, ly, rx, ry;
    rep(i, left.size()) rep(j, left[0].size()) {
      if (left[i][j] == 'L') {
        lx = j;
        ly = i;
      }
      if (right[i][j] == 'R') {
        rx = j;
        ry = i;
      }
    }
    status s;
    s.LEFT.X = lx;
    s.LEFT.Y = ly;
    s.RIGHT.X = rx;
    s.RIGHT.Y = ry;
    que.push(s);
    while (!que.empty()) {
      status now = que.front();
      que.pop();
      if (dis[now.LEFT.X][now.LEFT.Y][now.RIGHT.X][now.RIGHT.Y] != -1) {
        continue;
      }
      dis[now.LEFT.X][now.LEFT.Y][now.RIGHT.X][now.RIGHT.Y] = 1;
      rep(i, 4) {
        status next = now;
        next.LEFT.X += dx[i];
        next.LEFT.Y += dy[i];
        next.RIGHT.X -= dx[i];
        next.RIGHT.Y += dy[i];
        if (left[next.LEFT.Y][next.LEFT.X] == '#') {
          next.LEFT.X -= dx[i];
          next.LEFT.Y -= dy[i];
        }
        if (right[next.RIGHT.Y][next.RIGHT.X] == '#') {
          next.RIGHT.X += dx[i];
          next.RIGHT.Y -= dy[i];
        }
        if (left[next.LEFT.Y][next.LEFT.X] == '%' &&
            right[next.RIGHT.Y][next.RIGHT.X] == '%')
          goto aaa;
        que.push(next);
      }
    }
    cout << "No" << endl;
    continue;
  aaa:;
    cout << "Yes" << endl;
  }
  return 0;
}