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

#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

vector<string> field;
int W, H;
unsigned char memo[52][52][52][52];

struct State {
  unsigned char rx, ry, lx, ly;
  unsigned char index;
};

int main() {
  while (true) {
    cin >> W >> H;
    if (W == 0 && H == 0) {
      break;
    }
    memset(memo, 0, sizeof(memo));
    field.resize(H + 2);
    field[0] = field[H + 1] = string(W * 2 + 3, '#');
    State init = {0};
    for (int i = 1; i <= H; ++i) {
      string l1, l2;
      cin >> l1 >> l2;
      string line = "#" + l1 + "#" + l2 + "#";
      for (int j = 0; j < line.size(); ++j) {
        if (line[j] == 'R') {
          init.rx = j;
          init.ry = i;
        } else if (line[j] == 'L') {
          init.lx = j;
          init.ly = i;
        }
      }
      field[i] = line;
    }
    stack<State> stk;
    stk.push(init);
    bool ok = false;
    while (!stk.empty()) {
      State cur = stk.top();
      stk.pop();
      int rx = cur.rx, ry = cur.ry, lx = cur.lx, ly = cur.ly;
      if (cur.index == 0) {
        if (memo[rx - W - 1][ry][lx][ly]) {
          continue;
        }
        memo[rx - W - 1][ry][lx][ly] = 1;
        if (field[ry][rx] == '%' && field[ly][lx] == '%') {
          ok = true;
          break;
        } else if (field[ry][rx] == '%' || field[ly][lx] == '%') {
          continue;
        }
      }
      int nrx = rx + dx[cur.index], nry = ry + dy[cur.index];
      int nlx = lx - dx[cur.index], nly = ly + dy[cur.index];
      if (field[nry][nrx] == '#') {
        nrx -= dx[cur.index];
        nry -= dy[cur.index];
      }
      if (field[nly][nlx] == '#') {
        nlx += dx[cur.index];
        nly -= dy[cur.index];
      }
      if (cur.index != 3) {
        ++cur.index;
        stk.push(cur);
      }
      State next = {nrx, nry, nlx, nly, 0};
      stk.push(next);
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

vector<string> field;
int W, H;
unsigned char memo[52][52][52][52];

struct State {
  int rx, ry, lx, ly;
  int index;
};

int main() {
  while (true) {
    cin >> W >> H;
    if (W == 0 && H == 0) {
      break;
    }
    memset(memo, 0, sizeof(memo));
    field.resize(H + 2);
    field[0] = field[H + 1] = string(W * 2 + 3, '#');
    State init = {0};
    for (int i = 1; i <= H; ++i) {
      string l1, l2;
      cin >> l1 >> l2;
      string line = "#" + l1 + "#" + l2 + "#";
      for (int j = 0; j < line.size(); ++j) {
        if (line[j] == 'R') {
          init.rx = j;
          init.ry = i;
        } else if (line[j] == 'L') {
          init.lx = j;
          init.ly = i;
        }
      }
      field[i] = line;
    }
    stack<State> stk;
    stk.push(init);
    bool ok = false;
    while (!stk.empty()) {
      State cur = stk.top();
      stk.pop();
      int rx = cur.rx, ry = cur.ry, lx = cur.lx, ly = cur.ly;
      if (cur.index == 0) {
        if (memo[rx - W - 1][ry][lx][ly]) {
          continue;
        }
        memo[rx - W - 1][ry][lx][ly] = 1;
        if (field[ry][rx] == '%' && field[ly][lx] == '%') {
          ok = true;
          break;
        } else if (field[ry][rx] == '%' || field[ly][lx] == '%') {
          continue;
        }
      }
      int nrx = rx + dx[cur.index], nry = ry + dy[cur.index];
      int nlx = lx - dx[cur.index], nly = ly + dy[cur.index];
      if (field[nry][nrx] == '#') {
        nrx -= dx[cur.index];
        nry -= dy[cur.index];
      }
      if (field[nly][nlx] == '#') {
        nlx += dx[cur.index];
        nly -= dy[cur.index];
      }
      if (cur.index != 3) {
        ++cur.index;
        stk.push(cur);
      }
      State next = {nrx, nry, nlx, nly, 0};
      stk.push(next);
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
  return 0;
}