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

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>

#define ll long long int

using namespace std;

const int INF = (1 << 30) - 1;

// lh, lw, rh, rw
typedef tuple<int, int, int, int> State;

int dh[] = {0, 1, 0, -1};
int dw[] = {1, 0, -1, 0};

int dp[50][50][50][50];

bool solve(vector<string> &L, vector<string> &R) {
  int H = L.size();
  int W = L[0].size();

  memset(dp, 0, sizeof(dp));
  queue<State> que;

  int lh, lw, rh, rw;
  int glh, glw, grh, grw;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      if (L[h][w] == 'L') {
        lh = h;
        lw = w;
      } else if (L[h][w] == '%') {
        glh = h;
        glw = w;
      }
    }
  }
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      if (R[h][w] == 'R') {
        rh = h;
        rw = w;
      } else if (R[h][w] == '%') {
        grh = h;
        grw = w;
      }
    }
  }
  auto start = State(lh, lw, rh, rw);
  dp[lh][lw][rh][rw] = true;
  que.push(start);

  while (!que.empty()) {
    tie(lh, lw, rh, rw) = que.front();
    que.pop();

    for (int i = 0; i < 4; i++) {
      int nlh = lh + dh[i], nlw = lw + dw[i];
      int nrh = rh + dh[i], nrw = rw + dw[i] * -1;

      if (nlh < 0 || H <= nlh || nlw < 0 || W <= nlw || L[nlh][nlw] == '#') {
        nlh = lh;
        nlw = lw;
      }
      if (nrh < 0 || H <= nrh || nrw < 0 || W <= nrw || R[nrh][nrw] == '#') {
        nrh = rh;
        nrw = rw;
      }

      State next = State(nlh, nlw, nrh, nrw);

      if (nlh == glh && nlw == glw && nrh == grh && nrw == grw)
        return true;

      if (!dp[nlh][nlw][nrh][nrw] && (nlh != glh || nlw != glw) &&
          (nrh != grh || nrw != grw)) {
        dp[nlh][nlw][nrh][nrw] = true;
        que.push(next);
      }
    }
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W;
  while (cin >> W >> H, H | W) {
    vector<string> L(H), R(H);
    for (int i = 0; i < H; i++)
      cin >> L[i] >> R[i];

    cout << (solve(L, R) ? "Yes" : "No") << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>

#define ll long long int

using namespace std;

const int INF = (1 << 30) - 1;

// lh, lw, rh, rw
typedef tuple<int, int, int, int> State;

int dh[] = {0, 1, 0, -1};
int dw[] = {1, 0, -1, 0};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W;
  while (cin >> W >> H, H | W) {
    vector<string> L(H), R(H);
    for (int i = 0; i < H; i++)
      cin >> L[i] >> R[i];

    set<State> dp;
    queue<State> que;

    int lh, lw, rh, rw;
    int glh, glw, grh, grw;
    for (int h = 0; h < H; h++)
      for (int w = 0; w < W; w++)
        if (L[h][w] == 'L') {
          lh = h;
          lw = w;
        } else if (L[h][w] == '%') {
          glh = h;
          glw = w;
        }
    for (int h = 0; h < H; h++)
      for (int w = 0; w < W; w++)
        if (R[h][w] == 'R') {
          rh = h;
          rw = w;
        } else if (R[h][w] == '%') {
          grh = h;
          grw = w;
        }

    auto start = State(lh, lw, rh, rw);
    dp.insert(start);
    que.push(start);

    while (!que.empty()) {
      tie(lh, lw, rh, rw) = que.front();
      que.pop();

      for (int i = 0; i < 4; i++) {
        int nlh = lh + dh[i], nlw = lw + dw[i];
        int nrh = rh + dh[i], nrw = rw + dw[i] * -1;

        if (nlh < 0 || H <= nlh || nlw < 0 || W <= nlw || L[nlh][nlw] == '#') {
          nlh = lh;
          nlw = lw;
        }
        if (nrh < 0 || H <= nrh || nrw < 0 || W <= nrw || R[nrh][nrw] == '#') {
          nrh = rh;
          nrw = rw;
        }

        State next = State(nlh, nlw, nrh, nrw);
        if (dp.count(next) == 0) {
          dp.insert(next);
          que.push(next);
        }
      }
    }
    cout << (dp.count(State(glh, glw, grh, grw)) ? "YES" : "NO") << endl;
  }

  return 0;
}