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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

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

bool vis[50][50][50][50];

void move(int &x, int &y, int dir, vector<string> &a) {
  int H = a.size(), W = a[0].length();
  x += dx[dir];
  y += dy[dir];
  if (x >= 0 && x < W && y >= 0 && y < H && a[y][x] != '#')
    return;
  x -= dx[dir];
  y -= dy[dir];
}

int main() {
  for (;;) {
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    vector<string> al(H), ar(H);
    int xsl, ysl, xtl, ytl, xsr, ysr, xtr, ytr;
    for (int y = 0; y < H; y++) {
      cin >> al[y] >> ar[y];
      reverse(ar[y].begin(), ar[y].end());
      for (int x = 0; x < W; x++) {
        if (al[y][x] == 'L') {
          xsl = x;
          ysl = y;
        }
        if (al[y][x] == '%') {
          xtl = x;
          ytl = y;
        }
        if (ar[y][x] == 'R') {
          xsr = x;
          ysr = y;
        }
        if (ar[y][x] == '%') {
          xtr = x;
          ytr = y;
        }
      }
    }
    for (int yl = 0; yl < H; yl++)
      for (int xl = 0; xl < W; xl++)
        for (int yr = 0; yr < H; yr++)
          for (int xr = 0; xr < W; xr++)
            vis[xl][yl][xr][yr] = false;
    vis[xsl][ysl][xsr][ysr] = true;
    queue<int> XL, YL, XR, YR;
    XL.push(xsl);
    YL.push(ysl);
    XR.push(xsr);
    YR.push(ysr);
    while (!XL.empty()) {
      int xl = XL.front();
      XL.pop();
      int yl = YL.front();
      YL.pop();
      int xr = XR.front();
      XR.pop();
      int yr = YR.front();
      YR.pop();
      for (int dir = 0; dir < 4; dir++) {
        int _xl = xl, _yl = yl, _xr = xr, _yr = yr;
        move(_xl, _yl, dir, al);
        move(_xr, _yr, dir, ar);
        if (vis[_xl][_yl][_xr][_yr])
          continue;
        vis[_xl][_yl][_xr][_yr] = true;
        if ((_xl == xtl && _yl == ytl) || (_xr == xtr && _yr == ytr))
          continue;
        XL.push(_xl);
        YL.push(_yl);
        XR.push(_xr);
        YR.push(_yr);
      }
    }
    cout << (vis[xtl][ytl][xtr][ytr] ? "Yes" : "No") << endl;
  }
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

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

bool vis[3000][3000];

int main() {
  for (;;) {
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    vector<vector<string>> a(2, vector<string>(H + 2, string(W + 2, '#')));
    for (int y = 1; y <= H; y++) {
      string s1, s2;
      cin >> s1 >> s2;
      a[0][y] = '#' + s1 + '#';
      a[1][y] = '#' + s2 + '#';
    }
    vector<char> xs(2), ys(2), xt(2), yt(2);
    for (int k = 0; k < 2; k++)
      for (int y = 1; y <= H; y++)
        for (int x = 1; x <= W; x++) {
          char &c = a[k][y][x];
          if (c == 'L' || c == 'R') {
            c = '.';
            xs[k] = x;
            ys[k] = y;
          } else if (c == '%') {
            c = '.';
            xt[k] = x;
            yt[k] = y;
          }
        }
    fill(vis[0], vis[0] + 9000000, false);
    stack<vector<char>> stx, sty;
    stx.push(xs);
    sty.push(ys);
    while (!stx.empty()) {
      vector<char> x = stx.top();
      stx.pop();
      vector<char> y = sty.top();
      sty.pop();
      vector<int> z(2);
      for (int k = 0; k < 2; k++)
        z[k] = y[k] * (W + 2) + x[k];
      vis[z[0]][z[1]] = true;
      if (x[0] == xt[0] && y[0] == yt[0])
        continue;
      if (x[1] == xt[1] && y[1] == yt[1])
        continue;
      for (int i = 0; i < 4; i++) {
        vector<char> _x = x, _y = y;
        vector<int> _z(2);
        for (int k = 0; k < 2; k++) {
          _x[k] += (k ? dx[i] : -dx[i]);
          _y[k] += dy[i];
          if (a[k][_y[k]][_x[k]] == '#') {
            _x[k] = x[k];
            _y[k] = y[k];
          }
          _z[k] = _y[k] * (W + 2) + _x[k];
        }
        if (!vis[_z[0]][_z[1]]) {
          vis[_z[0]][_z[1]] = true;
          stx.push(_x);
          sty.push(_y);
        }
      }
      cout << stx.size() << endl;
    }
    vector<int> zt(2);
    for (int k = 0; k < 2; k++)
      zt[k] = yt[k] * (W + 2) + xt[k];
    cout << (vis[zt[0]][zt[1]] ? "Yes" : "No") << endl;
  }
}