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

#include <iostream>
#include <queue>
#include <set>
#define N 52
#define y1 first.first
#define x1 first.second
#define y2 second.first
#define x2 second.second
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> P1;

int bfs();

int w, h, sy1, sx1, sy2, sx2, gy1, gx1, gy2, gx2;
string r1[N], r2[N];
bool visited[N][N][N][N];

int main() {
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h; i++)
      cin >> r1[i] >> r2[i];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        for (int k = 0; k < h; k++)
          for (int l = 0; l < w; l++)
            visited[i][j][k][l] = false;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (r1[i][j] == 'L')
          sy1 = i, sx1 = j;
        if (r1[i][j] == '%')
          gy1 = i, gx1 = j;
        if (r2[i][j] == 'R')
          sy2 = i, sx2 = j;
        if (r2[i][j] == '%')
          gy2 = i, gx2 = j;
      }
    if (bfs())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

int bfs() {
  int dy[4] = {0, -1, 0, 1}, dx[4] = {-1, 0, 1, 0};
  queue<P1> q;
  P1 k = make_pair(P(sy1, sx1), P(sy2, sx2));
  q.push(k);
  visited[sy1][sx1][sy2][sx2] = true;
  while (!q.empty()) {
    P1 t = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ny1 = t.y1 + dy[i], nx1 = t.x1 + dx[i];
      int ny2 = t.y2 + dy[i], nx2 = t.x2 - dx[i];
      if (ny1 < 0)
        ny1 = 0;
      if (nx1 < 0)
        nx1 = 0;
      if (h <= ny1)
        ny1 = h - 1;
      if (w <= nx1)
        nx1 = w - 1;
      if (ny2 < 0)
        ny2 = 0;
      if (nx2 < 0)
        nx2 = 0;
      if (h <= ny2)
        ny2 = h - 1;
      if (w <= nx2)
        nx2 = w - 1;
      if (r1[ny1][nx1] == '#')
        ny1 = t.y1, nx1 = t.x1;
      if (r2[ny2][nx2] == '#')
        ny2 = t.y2, nx2 = t.x2;
      P1 u = make_pair(P(ny1, nx1), P(ny2, nx2));
      if (ny1 == gy1 && nx1 == gx1 && ny2 == gy2 && nx2 == gx2)
        return 1;
      if (visited[ny1][nx1][ny2][nx2] || (ny1 == gy1 && nx1 == gx1) ||
          (ny2 == gy2 && nx2 == gx2))
        continue;
      visited[ny1][nx1][ny2][nx2] = true;
      q.push(u);
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#define N 52
#define y1 first.first
#define x1 first.second
#define y2 second.first
#define x2 second.second
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> P1;

void bfs();

int w, h, sy1, sx1, sy2, sx2, gy1, gx1, gy2, gx2, ans;
string r1[N], r2[N];
set<P1> visited;

int main() {
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h; i++)
      cin >> r1[i] >> r2[i];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (r1[i][j] == 'L')
          sy1 = i, sx1 = j;
        if (r1[i][j] == '%')
          gy1 = i, gx1 = j;
        if (r2[i][j] == 'R')
          sy2 = i, sx2 = j;
        if (r2[i][j] == '%')
          gy2 = i, gx2 = j;
      }
    ans = 0;
    bfs();
    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
    visited.clear();
  }
  return 0;
}

void bfs() {
  int dy[4] = {0, -1, 0, 1}, dx[4] = {-1, 0, 1, 0};
  queue<P1> q;
  P1 k = make_pair(P(sy1, sx1), P(sy2, sx2));
  q.push(k);
  visited.insert(k);
  while (!q.empty()) {
    P1 t = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ny1 = t.y1 + dy[i], nx1 = t.x1 + dx[i];
      int ny2 = t.y2 + dy[i], nx2 = t.x2 - dx[i];
      if (ny1 < 0)
        ny1 = 0;
      if (nx1 < 0)
        nx1 = 0;
      if (h <= ny1)
        ny1 = h - 1;
      if (w <= nx1)
        nx1 = w - 1;
      if (ny2 < 0)
        ny2 = 0;
      if (nx2 < 0)
        nx2 = 0;
      if (h <= ny2)
        ny2 = h - 1;
      if (w <= nx2)
        nx2 = w - 1;
      if (r1[ny1][nx1] == '#')
        ny1 = t.y1, nx1 = t.x1;
      if (r2[ny2][nx2] == '#')
        ny2 = t.y2, nx2 = t.x2;
      P1 u = make_pair(P(ny1, nx1), P(ny2, nx2));
      if (visited.count(u))
        continue;
      visited.insert(u);
      q.push(u);
      if (ny1 == gy1 && nx1 == gx1 && ny2 == gy2 && nx2 == gx2) {
        ans = 1;
        return;
      }
    }
  }
}