Problem p01271 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstring>
#include <iostream>
#include <memory.h>
#include <queue>
#include <vector>

using namespace std;

int maxw, maxh;
int lsx, lsy;
int rsx, rsy;

const int dxl[] = {1, 0, -1, 0};
const int dxr[] = {-1, 0, 1, 0};
const int dyl[] = {0, 1, 0, -1};
const int dyr[] = {0, 1, 0, -1};

typedef pair<int, int> PP;
typedef pair<PP, PP> P;

bool visited[55][55][55][55] = {};

int main(void) {
  while (1) {
    char fieldl[55][55];
    char fieldr[55][55];
    memset(visited, 0, sizeof(visited));
    memset(fieldl, 0, sizeof(fieldl));
    memset(fieldr, 0, sizeof(fieldr));
    cin >> maxw >> maxh;
    if (maxw == 0 && maxh == 0)
      return 0;
    for (int y = 1; y <= maxh; ++y) {
      for (int x = 1; x <= maxw; ++x) {
        cin >> fieldl[y][x];
        if (fieldl[y][x] == 'L') {
          lsy = y;
          lsx = x;
          fieldl[y][x] = '.';
        }
        if (fieldl[y][x] == 'R') {
          rsy = y;
          rsx = x;
          fieldl[y][x] = '.';
        }
      }
      for (int x = 1; x <= maxw; ++x) {
        cin >> fieldr[y][x];
        if (fieldr[y][x] == 'L') {
          lsy = y;
          lsx = x;
          fieldr[y][x] = '.';
        }
        if (fieldr[y][x] == 'R') {
          rsy = y;
          rsx = x;
          fieldr[y][x] = '.';
        }
      }
    }

    queue<P> q;
    bool isGoal = false;
    q.push(P(PP(lsy, lsx), PP(rsy, rsx)));
    visited[lsy][lsx][rsy][rsx] = true;
    while (!q.empty()) {
      P p = q.front();
      q.pop();
      for (int i = 0; i < 4; ++i) {
        bool f = false;
        PP tol = PP(p.first.first + dyl[i], p.first.second + dxl[i]);   // y,x
        PP tor = PP(p.second.first + dyr[i], p.second.second + dxr[i]); // y,x
        if (visited[tol.first][tol.second][tor.first][tor.second])
          continue;
        if (fieldl[tol.first][tol.second] == '%' &&
            fieldr[tor.first][tor.second] == '%') {
          isGoal = true;
          break;
        } else if (fieldl[tol.first][tol.second] == '%' ||
                   fieldr[tor.first][tor.second] == '%')
          continue;
        PP l = PP(p.first.first, p.first.second);
        PP r = PP(p.second.first, p.second.second);
        if (fieldl[tol.first][tol.second] == '.') {
          l = tol;
          f = true;
        }
        if (fieldr[tor.first][tor.second] == '.') {
          r = tor;
          f = true;
        }
        if (!f)
          continue;
        visited[l.first][l.second][r.first][r.second] = true;
        q.push(P(l, r));
      }
      if (isGoal)
        break;
    }
    (isGoal ? cout << "Yes" << endl : cout << "No" << endl);
  }
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <memory.h>
#include <queue>
#include <vector>

using namespace std;

int maxw, maxh;
int lsx, lsy;
int rsx, rsy;

const int dxl[] = {1, 0, -1, 0};
const int dxr[] = {-1, 0, 1, 0};
const int dyl[] = {0, 1, 0, -1};
const int dyr[] = {0, 1, 0, -1};

typedef pair<int, int> PP;
typedef pair<PP, PP> P;

bool visited[55][55][55][55] = {};

int main(void) {
  while (1) {
    char fieldl[55][55];
    char fieldr[55][55];
    memset(visited, 0, sizeof(visited));
    memset(fieldl, 0, sizeof(fieldl));
    memset(fieldr, 0, sizeof(fieldr));
    cin >> maxw >> maxh;
    if (maxw == 0 && maxh == 0)
      return 0;
    for (int y = 1; y <= maxh; ++y) {
      for (int x = 1; x <= maxw; ++x) {
        cin >> fieldl[y][x];
        if (fieldl[y][x] == 'L') {
          lsy = y;
          lsx = x;
          fieldl[y][x] = '.';
        }
        if (fieldl[y][x] == 'R') {
          rsy = y;
          rsx = x;
          fieldl[y][x] = '.';
        }
      }
      for (int x = 1; x <= maxw; ++x) {
        cin >> fieldr[y][x];
        if (fieldr[y][x] == 'L') {
          lsy = y;
          lsx = x;
          fieldr[y][x] = '.';
        }
        if (fieldr[y][x] == 'R') {
          rsy = y;
          rsx = x;
          fieldr[y][x] = '.';
        }
      }
    }

    queue<P> q;
    bool isGoal = false;
    q.push(P(PP(lsy, lsx), PP(rsy, rsx)));
    visited[lsy][lsx][rsy][rsx] = true;
    while (!q.empty()) {
      P p = q.front();
      q.pop();
      for (int i = 0; i < 4; ++i) {
        bool f = false;
        PP tol = PP(p.first.first + dyl[i], p.first.second + dxl[i]);   // y,x
        PP tor = PP(p.second.first + dyr[i], p.second.second + dxr[i]); // y,x
        if (visited[tol.first][tol.second][tor.first][tor.second])
          continue;
        if (fieldl[tol.first][tol.second] == '%' &&
            fieldr[tor.first][tor.second] == '%') {
          isGoal = true;
          break;
        } else if (fieldl[tol.first][tol.second] == '%' ||
                   fieldr[tor.first][tor.second] == '%')
          continue;
        PP l = PP(p.first.first, p.first.second);
        PP r = PP(p.second.first, p.second.second);
        if (fieldl[tol.first][tol.second] == '.') {
          l = tol;
          f = true;
        }
        if (fieldr[tor.first][tor.second] == '.') {
          r = tor;
          f = true;
        }
        if (!f)
          continue;
        if (visited[l.first][l.second][r.first][r.second])
          continue;
        visited[l.first][l.second][r.first][r.second] = true;
        q.push(P(l, r));
      }
      if (isGoal)
        break;
    }
    (isGoal ? cout << "Yes" << endl : cout << "No" << endl);
  }
}