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

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define mk make_pair
using namespace std;
string s1[50], s2[50];
int dx1[] =
    {
        1,
        -1,
        0,
        0,
},
    dy1[] = {0, 0, 1, -1};
int dx2[] =
    {
        -1,
        1,
        0,
        0,
},
    dy2[] = {0, 0, 1, -1};
int w, h, gx1, gx2, gy1, gy2;
bool vis[51][51][51][51];
bool bfs(int x1, int y1, int x2, int y2) {
  queue<int> q;
  q.push(x1 + y1 * 100 + x2 * 10000 + y2 * 1000000);
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    x1 = p % 100;
    y1 = p % 10000 / 100;
    x2 = p % 1000000 / 10000;
    y2 = p / 1000000;
    for (int i = 0; i < 4; i++) {
      int x3 = x1 + dx1[i], y3 = y1 + dy1[i];
      int x4 = x2 + dx2[i], y4 = y2 + dy2[i];
      if (x3 < 0)
        x3 = x1;
      if (x4 < 0)
        x4 = x2;
      if (y3 < 0)
        y3 = y1;
      if (y4 < 0)
        y4 = y2;
      if (x3 >= w)
        x3 = x1;
      if (x4 >= w)
        x4 = x2;
      if (y3 >= h)
        y3 = y1;
      if (y4 >= h)
        y4 = y2;
      if (s1[y3][x3] == '#')
        y3 = y1, x3 = x1;
      if (s2[y4][x4] == '#')
        y4 = y2, x4 = x2;
      if (x1 == gx1 && y1 == gy1 && x2 == gx2 && y2 == gy2)
        return 1;
      if (!(x3 == gx1 && y3 == gy1 && (x4 != gx2 || y4 != gy2)))
        if (!((x3 != gx1 || y3 != gy1) && x4 == gx2 && y4 == gy2))
          if (!vis[x3][y3][x4][y4]) {
            vis[x3][y3][x4][y4] = 1;
            q.push(x3 + y3 * 100 + x4 * 10000 + y4 * 1000000);
          }
    }
  }
  return 0;
}
int main() {
  int x1, y1, x2, y2;
  while (cin >> w >> h, w) {
    r(i, h) cin >> s1[i] >> s2[i];
    r(i, h) r(j, w) {
      if (s1[i][j] == 'L')
        x1 = j, y1 = i;
      if (s2[i][j] == 'R')
        x2 = j, y2 = i;
      if (s1[i][j] == '%')
        gx1 = j, gy1 = i;
      if (s2[i][j] == '%')
        gx2 = j, gy2 = i;
    }
    r(i, 51) r(j, 51) r(k, 51) r(l, 51) vis[l][k][j][i] = 0;
    vis[x1][y1][x2][y2] = 1;
    if (bfs(x1, y1, x2, y2))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define fi first
#define se second
#define mk make_pair
using namespace std;
string s1[50], s2[50];
int dx1[] =
    {
        1,
        -1,
        0,
        0,
},
    dy1[] = {0, 0, 1, -1};
int dx2[] =
    {
        -1,
        1,
        0,
        0,
},
    dy2[] = {0, 0, 1, -1};
int w, h, gx1, gx2, gy1, gy2;
set<pair<pair<int, int>, pair<int, int>>> s;
queue<pair<pair<int, int>, pair<int, int>>> q;
bool bfs(int x1, int y1, int x2, int y2) {
  queue<int> q;
  q.push(x1 + y1 * 100 + x2 * 10000 + y2 * 1000000);
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    x1 = p % 100;
    y1 = p % 10000 / 100;
    x2 = p % 1000000 / 10000;
    y2 = p / 1000000;
    for (int i = 0; i < 4; i++) {
      int x3 = x1 + dx1[i], y3 = y1 + dy1[i];
      int x4 = x2 + dx2[i], y4 = y2 + dy2[i];
      if (x3 >= 0 && x4 >= 0 && y3 >= 0 && y4 >= 0)
        if (x3 < w && x4 < w && y3 < h && y4 < h) {
          if (s1[y3][x3] == '#')
            y3 = y1, x3 = x1;
          if (s2[y4][x4] == '#')
            y4 = y2, x4 = x2;
          if (x1 == gx1 && y1 == gy1 && x2 == gx2 && y2 == gy2)
            return 1;
          if (!(x3 == gx1 && y3 == gy1 && (x4 != gx2 || y4 != gy2)))
            if (!((x3 != gx1 || y3 != gy1) && x4 == gx2 && y4 == gy2))
              if (!s.count(mk(mk(x3, y3), mk(x4, y4)))) {
                s.insert(mk(mk(x3, y3), mk(x4, y4)));
                q.push(x3 + y3 * 100 + x4 * 10000 + y4 * 1000000);
              }
        }
    }
  }
  return 0;
}
bool dfs(int x1, int y1, int x2, int y2, int d) {
  // cout<<d<<endl;cout<<1;
  if (x1 == gx1 && y1 == gy1 && (x2 != gx2 || y2 != gy2))
    return 0;
  if ((x1 != gx1 || y1 != gy1) && x2 == gx2 && y2 == gy2)
    return 0;
  if (x1 == gx1 && y1 == gy1 && x2 == gx2 && y2 == gy2)
    return 1;
  if (d < 5000)
    for (int i = 0; i < 4; i++) {
      int x3 = x1 + dx1[i], y3 = y1 + dy1[i];
      int x4 = x2 + dx2[i], y4 = y2 + dy2[i];
      if (x3 < 0)
        x3 = x1;
      if (x4 < 0)
        x4 = x2;
      if (y3 < 0)
        y3 = y1;
      if (y4 < 0)
        y4 = y2;
      if (x3 >= w)
        x3 = x1;
      if (x4 >= w)
        x4 = x2;
      if (y3 >= h)
        y3 = y1;
      if (y4 >= h)
        y4 = y2;
      if (s1[y3][x3] == '#')
        y3 = y1, x3 = x1;
      if (s2[y4][x4] == '#')
        y4 = y2, x4 = x2;
      if (!s.count(mk(mk(x3, y3), mk(x4, y4)))) {
        s.insert(mk(mk(x3, y3), mk(x4, y4)));
        if (dfs(x3, y3, x4, y4, d + 1))
          return 1;
      }
    }
  else
    q.push(mk(mk(x1, y1), mk(x2, y2)));
  if (q.size()) {
    pair<pair<int, int>, pair<int, int>> pp = q.front();
    q.pop();
    if (bfs(pp.fi.fi, pp.fi.se, pp.se.fi, pp.se.se))
      return 1;
  }
  return 0;
}
int main() {
  int x1, y1, x2, y2;
  while (cin >> w >> h, w) {
    s.clear();
    r(i, h) cin >> s1[i] >> s2[i];
    r(i, h) r(j, w) {
      if (s1[i][j] == 'L')
        x1 = j, y1 = i;
      if (s2[i][j] == 'R')
        x2 = j, y2 = i;
      if (s1[i][j] == '%')
        gx1 = j, gy1 = i;
      if (s2[i][j] == '%')
        gx2 = j, gy2 = i;
    }
    s.insert(mk(mk(x1, y1), mk(x2, y2)));
    if (dfs(x1, y1, x2, y2, 0))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
    while (!q.empty())
      q.pop();
  }
}