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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
const int MAX_N = 60;
struct Info {
  P left, right;
};
string Rfield[MAX_N];
string Lfield[MAX_N];
bool used[MAX_N][MAX_N][MAX_N][MAX_N];
int h, w, lsy, lsx, rsy, rsx;
bool OutOfRange(int y, int x, string field[]) {
  if (y >= h || x >= w || x < 0 || y < 0 || field[y][x] == '#')
    return true;
  else
    return false;
}
bool bfs() {
  memset(used, false, sizeof(used));
  queue<Info> que;
  que.push({P(lsy, lsx), P(rsy, rsx)});
  while (!que.empty()) {
    Info p = que.front();
    que.pop();
    if (used[p.left.first][p.left.second][p.right.first][p.right.second])
      continue;
    used[p.left.first][p.left.second][p.right.first][p.right.second] = true;
    if (Lfield[p.left.first][p.left.second] == '%' &&
        Rfield[p.right.first][p.right.second] == '%')
      return true;
    if (Lfield[p.left.first][p.left.second] == '%' ||
        Rfield[p.right.first][p.right.second] == '%')
      continue;
    for (int i = 0; i < 4; i++) {
      int ly = p.left.first;
      int lx = p.left.second;
      int ry = p.right.first;
      int rx = p.right.second;
      if (!OutOfRange(ly + dy[i], lx + dx[i], Lfield))
        ly += dy[i], lx += dx[i];
      if (!OutOfRange(ry + dy[i], rx - dx[i], Rfield))
        ry += dy[i], rx -= dx[i];
      P np = {ly, lx}, nq = {ry, rx};
      if (!used[ly][lx][ry][rx])
        que.push({np, nq});
    }
  }
  return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> w >> h, w | h) {
    for (int i = 0; i < h; i++) {
      cin >> Lfield[i] >> Rfield[i];
      for (int j = 0; j < w; j++) {
        if (Lfield[i][j] == 'L')
          lsy = i, lsx = j;
        if (Rfield[i][j] == 'R')
          rsy = i, rsx = j;
      }
    }
    cout << (bfs() ? "Yes" : "No") << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
const int MAX_N = 60;
struct Info {
  P left, right;
};
string Rfield[MAX_N];
string Lfield[MAX_N];
int h, w, lsy, lsx, rsy, rsx;
bool range_check(int y, int x) {
  if (y >= h || x >= w || x < 0 || y < 0)
    return false;
  else
    return true;
}
bool bfs() {
  Lfield[lsy][lsx] = '$';
  Rfield[rsy][rsx] = '$';
  queue<Info> que;
  que.push({P(lsy, lsx), P(rsy, rsx)});
  while (!que.empty()) {
    Info p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int ly = p.left.first + dy[i];
      int lx = p.left.second + dx[i];
      int ry = p.right.first + dy[i];
      int rx = p.right.second + dx[(i + 2) % 4];
      if (!(range_check(ly, lx) || range_check(ry, rx)))
        continue;
      if (Lfield[ly][lx] == '$' || Rfield[ry][rx] == '$')
        continue;
      if (Lfield[ly][lx] == '#' && Rfield[ry][rx] == '#')
        continue;
      if (Lfield[ly][lx] == '%' && Rfield[ry][rx] == '%')
        return true;
      if (Lfield[ly][lx] == '%' || Rfield[ry][rx] == '%')
        continue;
      if (Lfield[ly][lx] == '.')
        Lfield[ly][lx] = '$';
      if (Rfield[ry][rx] == '.')
        Rfield[ry][rx] = '$';
      P np = {ly, lx}, nq = {ry, rx};
      if (Lfield[ly][lx] == '#')
        np = {p.left.first, p.left.second};
      if (Rfield[ry][rx] == '#')
        nq = {p.right.first, p.right.second};
      que.push({np, nq});
    }
    // for(int i = 0; i < h; i++) cout << Rfield[i] << endl;
  }
  return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> w >> h, w | h) {
    for (int i = 0; i < h; i++) {
      cin >> Lfield[i] >> Rfield[i];
      for (int j = 0; j < w; j++) {
        if (Lfield[i][j] == 'L')
          rsy = i, rsx = j;
        if (Rfield[i][j] == 'R')
          lsy = i, lsx = j;
      }
    }
    cout << (bfs() ? "Yes" : "No") << endl;
  }
  return 0;
}