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

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};

// int dp[50][50][50][50] = {};//ly,lx,ry,rx

int main(void) {

  while (1) {
    bool flag = false;
    int w, h;
    int lx = -1, ly = -1, rx = -1, ry = -1;
    int lgx = -1, lgy = -1, rgx = -1, rgy = -1;

    vector<vector<vector<vector<int>>>> dp(
        50, vector<vector<vector<int>>>(
                50, vector<vector<int>>(50, vector<int>(50, 0))));

    pair<pair<int, int>, pair<int, int>> Pair;

    queue<pair<pair<int, int>, pair<int, int>>> que;

    cin >> w >> h;

    if (w == 0) {
      break;
    }

    vector<string> left(h), right(h);

    FOR(i, 0, h - 1) { cin >> left[i] >> right[i]; }
    FOR(i, 0, h - 1) {
      FOR(j, 0, w - 1) {
        if (left[i][j] == 'L') {
          lx = j;
          ly = i;
          break;
        }
      }
      if (lx != -1) {
        break;
      }
    }

    FOR(i, 0, h - 1) {
      FOR(j, 0, w - 1) {
        if (right[i][j] == 'R') {
          rx = j;
          ry = i;
          break;
        }
      }
      if (rx != -1) {
        break;
      }
    }

    FOR(i, 0, h - 1) {
      FOR(j, 0, w - 1) {
        if (left[i][j] == '%') {
          lgx = j;
          lgy = i;
          break;
        }
      }
      if (lgx != -1) {
        break;
      }
    }

    FOR(i, 0, h - 1) {
      FOR(j, 0, w - 1) {
        if (right[i][j] == '%') {
          rgx = j;
          rgy = i;
          break;
        }
      }
      if (rgx != -1) {
        break;
      }
    }

    dp[ly][lx][ry][rx] = 1;

    // cout << lgy << " " << lgx << " " << rgy << " " << rgx << endl;
    // cout << endl;

    que.push(make_pair(make_pair(ly, lx), make_pair(ry, rx)));

    bool leftwall = false;
    bool rightwall = false;

    while (!que.empty()) {
      Pair = que.front();

      que.pop();
      ly = Pair.first.first;
      lx = Pair.first.second;
      ry = Pair.second.first;
      rx = Pair.second.second;
      // cout <<"-"<< ly << " " << lx << " " << ry << " " << rx << endl;

      FOR(i, 0, 3) {
        leftwall = false;
        rightwall = false;

        if (ly + dy[i] < 0 || ly + dy[i] > h - 1 || lx - dx[i] < 0 ||
            lx - dx[i] > w - 1) {
          leftwall = true;
        } else {
          if (left[ly + dy[i]][lx - dx[i]] == '#') {
            leftwall = true;
          }
        }

        if (ry + dy[i] < 0 || ry + dy[i] > h - 1 || rx + dx[i] < 0 ||
            rx + dx[i] > w - 1) {
          rightwall = true;
        } else {
          if (right[ry + dy[i]][rx + dx[i]] == '#') {
            rightwall = true;
          }
        }

        if (leftwall == true) {
          if (rightwall == true) {
            // no
          } else {
            if (dp[ly][lx][ry + dy[i]][rx + dx[i]] != 1) {
              dp[ly][lx][ry + dy[i]][rx + dx[i]] = 1;
              if (!(ry + dy[i] == rgy && rx + dx[i] == rgx)) {

                que.push(make_pair(make_pair(ly, lx),
                                   make_pair(ry + dy[i], rx + dx[i])));

                // cout << ly << " " << lx << " " << ry + dy[i] << " " << rx +
                // dx[i] << endl;
              }
            }
          }
        } else {
          if (rightwall == true) {
            if (dp[ly + dy[i]][lx - dx[i]][ry][rx] != 1) {
              dp[ly + dy[i]][lx - dx[i]][ry][rx] = 1;
              if (!(ly + dy[i] == lgy && lx - dx[i] == lgx)) {
                que.push(make_pair(make_pair(ly + dy[i], lx - dx[i]),
                                   make_pair(ry, rx)));
                // cout << ly + dy[i] << " " << lx - dx[i] << " " << ry << " "
                // << rx << endl;
              }
            }
          } else {
            if (dp[ly + dy[i]][lx - dx[i]][ry + dy[i]][rx + dx[i]] != 1) {
              dp[ly + dy[i]][lx - dx[i]][ry + dy[i]][rx + dx[i]] = 1;
              if (!(ly + dy[i] == lgy && lx - dx[i] == lgx) &&
                  !(ry + dy[i] == rgy && rx + dx[i] == rgx)) {
                que.push(make_pair(make_pair(ly + dy[i], lx - dx[i]),
                                   make_pair(ry + dy[i], rx + dx[i])));
                // cout << ly + dy[i] << " " << lx - dx[i] << " " << ry + dy[i]
                // << " " << rx + dx[i] << endl;
              }
            }
          }
        }
      }

      // cout << endl;

      if (dp[lgy][lgx][rgy][rgx] == 1) {
        flag = true;
        // cout << "a" << endl;
        break;
      }
    }

    if (dp[lgy][lgx][rgy][rgx] == 1 || flag == true) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}


Error: MLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};

// int dp[50][50][50][50] = {};//ly,lx,ry,rx

int main(void) {

  while (1) {
    bool flag = false;
    int w, h;
    int lx = -1, ly = -1, rx = -1, ry = -1;
    int lgx = -1, lgy = -1, rgx = -1, rgy = -1;

    vector<vector<vector<vector<int>>>> dp(
        50, vector<vector<vector<int>>>(
                50, vector<vector<int>>(50, vector<int>(50, 0))));

    pair<pair<int, int>, pair<int, int>> Pair;

    queue<pair<pair<int, int>, pair<int, int>>> que;

    cin >> w >> h;

    if (w == 0) {
      break;
    }

    vector<string> left(h), right(h);

    FOR(i, 0, h - 1) { cin >> left[i] >> right[i]; }
    FOR(i, 0, h - 1) {
      FOR(j, 0, w - 1) {
        if (left[i][j] == 'L') {
          lx = j;
          ly = i;
          break;
        }
      }
      if (lx != -1) {
        break;
      }
    }

    FOR(i, 0, h - 1) {
      FOR(j, 0, w - 1) {
        if (right[i][j] == 'R') {
          rx = j;
          ry = i;
          break;
        }
      }
      if (rx != -1) {
        break;
      }
    }

    FOR(i, 0, h - 1) {
      FOR(j, 0, w - 1) {
        if (left[i][j] == '%') {
          lgx = j;
          lgy = i;
          break;
        }
      }
      if (lgx != -1) {
        break;
      }
    }

    FOR(i, 0, h - 1) {
      FOR(j, 0, w - 1) {
        if (right[i][j] == '%') {
          rgx = j;
          rgy = i;
          break;
        }
      }
      if (rgx != -1) {
        break;
      }
    }

    dp[ly][lx][ry][rx] = 1;

    que.push(make_pair(make_pair(ly, lx), make_pair(ry, rx)));

    while (!que.empty()) {
      Pair = que.front();

      que.pop();
      ly = Pair.first.first;
      lx = Pair.first.second;
      ry = Pair.second.first;
      rx = Pair.second.second;
      // cout << ly << " " << lx << " " << ry << " " << rx << endl;

      FOR(i, 0, 3) {

        if (ly + dy[i] >= 0 && lx - dx[i] >= 0 && ry + dy[i] <= h - 1 &&
            rx + dx[i] <= w - 1 && ly + dy[i] <= h - 1 && lx - dx[i] <= w - 1 &&
            ry + dy[i] >= 0 && rx + dx[i] >= 0) {
          if (dp[ly + dy[i]][lx - dx[i]][ry + dy[i]][rx + dx[i]] != 1) {
            if (left[ly + dy[i]][lx - dx[i]] == '#') {
              if (right[ry + dy[i]][rx + dx[i]] == '#') {
                // no
              } else {
                dp[ly][lx][ry + dy[i]][rx + dx[i]] = 1;
                que.push(make_pair(make_pair(ly, lx),
                                   make_pair(ry + dy[i], rx + dx[i])));
              }
            } else {
              if (right[ry + dy[i]][rx + dx[i]] == '#') {
                dp[ly + dy[i]][lx - dx[i]][ry][rx] = 1;
                que.push(make_pair(make_pair(ly + dy[i], lx - dx[i]),
                                   make_pair(ry, rx)));
              } else {
                dp[ly + dy[i]][lx - dx[i]][ry + dy[i]][rx + dx[i]] = 1;
                que.push(make_pair(make_pair(ly + dy[i], lx - dx[i]),
                                   make_pair(ry + dy[i], rx + dx[i])));
              }
            }
          }
        }
      }
      if (dp[lgy][lgx][rgy][rgx] == 1) {
        flag = true;
        break;
      }
    }

    if (dp[lgy][lgx][rgy][rgx] == 1 || flag == true) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}