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

#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

int w, h;
bool vis[64][64][64][64];
pii ls, lg, rs, rg;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};

inline bool border(pii p) {
  return 0 <= p.first && p.first < w && 0 <= p.second && p.second < h;
}

string solve(vector<string> left, vector<string> right) {

  queue<pii> lqu, rqu;
  lqu.push(ls);
  rqu.push(rs);

  memset(vis, 0, sizeof(vis));
  vis[ls.first][ls.second][rs.first][rs.second] = true;

  while (!lqu.empty() && !rqu.empty()) {

    pii lnx, rnx, lnw, rnw;

    lnw = lqu.front();
    rnw = rqu.front();

    lqu.pop();
    rqu.pop();

    for (int i = 0; i < 4; ++i) {

      lnx = make_pair(lnw.first + dx[i], lnw.second + dy[i]);
      rnx = make_pair(rnw.first - dx[i], rnw.second + dy[i]);

      if (!border(lnx) || left[lnx.second][lnx.first] == '#')
        lnx = lnw;

      if (!border(rnx) || right[rnx.second][rnx.first] == '#')
        rnx = rnw;

      if (lnx == lg && rnx == rg)
        return "Yes";

      if (lnx == lg ^ rnx == rg)
        continue;

      if (!vis[lnx.first][lnx.second][rnx.first][rnx.second]) {

        vis[lnx.first][lnx.second][rnx.first][rnx.second] = true;
        lqu.push(lnx);
        rqu.push(rnx);
      }
    }
  }

  return "No";
}

int main() {

  while (cin >> w >> h, (w || h)) {

    vector<string> left, right;

    for (int i = 0; i < h; ++i) {

      string l, r;
      cin >> l >> r;

      left.push_back(l);
      right.push_back(r);
    }

    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {

        if (left[y][x] == 'L')
          ls = make_pair(x, y);
        if (right[y][x] == 'R')
          rs = make_pair(x, y);
        if (left[y][x] == '%')
          lg = make_pair(x, y);
        if (right[y][x] == '%')
          rg = make_pair(x, y);
      }
    }

    cout << solve(left, right) << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;
typedef pair<pii, pii> stat;

int w, h;
pii ls, lg, rs, rg;
stat start, goal;

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

bool border(pii p) {

  return 0 <= p.first && p.first < w && 0 <= p.second && p.second < h;
}

string solve(const vector<string> &left, const vector<string> &right) {

  start = make_pair(ls, rs);
  goal = make_pair(lg, rg);

  set<stat> vis;
  queue<stat> qu;

  vis.insert(start);
  qu.push(start);

  while (!qu.empty()) {

    pii lnx, rnx;
    stat now = qu.front();
    qu.pop();

    for (int i = 0; i < 4; ++i) {

      lnx = make_pair(now.first.first + dx[i], now.first.second + dy[i]);

      rnx = make_pair(now.second.first - dx[i], now.second.second + dy[i]);

      if (!border(lnx) || left[lnx.second][lnx.first] == '#')
        lnx = now.first;

      if (!border(rnx) || right[rnx.second][rnx.first] == '#')
        rnx = now.second;

      stat nx = make_pair(lnx, rnx);

      if (nx == goal)
        return "Yes";

      if (!vis.count(nx)) {

        vis.insert(nx);
        qu.push(nx);
      }
    }
  }

  return "No";
}

int main() {

  while (cin >> w >> h, (w || h)) {

    vector<string> left, right;

    for (int i = 0; i < h; ++i) {

      string l, r;
      cin >> l >> r;

      left.push_back(l);
      right.push_back(r);
    }

    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {

        if (left[y][x] == 'L')
          ls = make_pair(x, y);
        if (left[y][x] == '%')
          lg = make_pair(x, y);
        if (right[y][x] == 'R')
          rs = make_pair(x, y);
        if (right[y][x] == '%')
          rg = make_pair(x, y);
      }
    }

    cout << solve(left, right) << endl;
  }
}