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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

const int MAX_W = 52;
const int MAX_H = 52;

char fieldL[MAX_H][MAX_W];
char fieldR[MAX_H][MAX_W];
bool G[52][52][52][52];

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    fill_n((char *)fieldL, MAX_W * MAX_H, '#');
    fill_n((char *)fieldR, MAX_W * MAX_H, '#');
    fill_n((bool *)G, MAX_W * MAX_H * MAX_W * MAX_H, false);

    pii ls, rs, lg, rg;
    REP(y, h) {
      char c;
      REP(x, w) {
        cin >> c;
        switch (c) {
        case '%':
          c = '.';
          lg.first = x + 1;
          lg.second = y + 1;
          break;
        case 'L':
          c = '.';
          ls.first = x + 1;
          ls.second = y + 1;
          break;
        }
        fieldL[y + 1][x + 1] = c;
      }
      REP(x, w) {
        cin >> c;
        switch (c) {
        case '%':
          c = '.';
          rg.first = x + 1;
          rg.second = y + 1;
          break;
        case 'R':
          c = '.';
          rs.first = x + 1;
          rs.second = y + 1;
          break;
        }
        fieldR[y + 1][x + 1] = c;
      }
    }

    //		LOG("%d %d ,, %d %d\\n", ls.first, ls.second , rs.first,
    //rs.second);

    queue<pair<pii, pii>> que;
    que.push({ls, rs});
    bool res = false;
    while (!que.empty()) {
      pii L = que.front().first;
      pii R = que.front().second;
      que.pop();

      /*
      LOG("%d %d |||| %d %d\\n", L.first, L.second , R.first, R.second);
      REP(y, h + 2){
              REP(x, w + 2){
                      if(L.first == x && L.second == y)
                              LOG("L");
                      else
                              LOG("%c", fieldL[y][x]);

              }
              LOG(" ");

              REP(x, w + 2){
                      if(R.first == x && R.second == y)
                              LOG("R");
                      else
                              LOG("%c", fieldR[y][x]);
              }
              LOG("\\n");
      }
      LOG("------\\n");
      */

      if (L == lg) {
        if (R == rg) {
          res = true;
          break;
        }
        continue;
      } else if (R == rg)
        continue;

      REP(i, 4) {
        pii nextL = {L.first + dx[i], L.second + dy[i]};
        pii nextR = {R.first - dx[i], R.second + dy[i]};
        if (fieldL[nextL.second][nextL.first] == '#') {
          nextL = L;
        }
        if (fieldR[nextR.second][nextR.first] == '#') {
          nextR = R;
        }

        //				LOG("%c %c   %d %d  <<<--- %d %d\\n",
        //fieldL[nextL.second][nextL.first],fieldR[nextR.second][nextR.first],
        //nextR.first, nextR.second, R.first, R.second);
        if (!G[nextL.first][nextL.second][nextR.first][nextR.second]) {
          G[nextL.first][nextL.second][nextR.first][nextR.second] = true;
          que.push({nextL, nextR});
        }
      }
    }

    cout << (res ? "Yes" : "No") << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

const int MAX_W = 52;
const int MAX_H = 52;

char fieldL[MAX_H][MAX_W];
char fieldR[MAX_H][MAX_W];

bool dfsG[MAX_H][MAX_W];

bool dfsL(pii p, pii &g) {
  if (p == g)
    return true;
  dfsG[p.second][p.first] = true;

  REP(i, 4) {
    pii next = {p.first + dx[i], p.second + dy[i]};
    if (dfsG[next.second][next.first])
      continue;
    if (fieldL[next.second][next.first] != '#') {
      if (dfsL(next, g))
        return true;
    }
  }

  return false;
}
bool dfsR(pii p, pii &g) {
  if (p == g)
    return true;
  dfsG[p.second][p.first] = true;

  REP(i, 4) {
    pii next = {p.first + dx[i], p.second + dy[i]};
    if (dfsG[next.second][next.first])
      continue;
    if (fieldR[next.second][next.first] != '#') {
      if (dfsR(next, g))
        return true;
    }
  }

  return false;
}

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    fill_n((char *)fieldL, MAX_W * MAX_H, '#');
    fill_n((char *)fieldR, MAX_W * MAX_H, '#');

    pii ls, rs, lg, rg;
    REP(y, h) {
      char c;
      REP(x, w) {
        cin >> c;
        switch (c) {
        case '%':
          c = '.';
          lg.first = x + 1;
          lg.second = y + 1;
          break;
        case 'L':
          c = '.';
          ls.first = x + 1;
          ls.second = x + 1;
          break;
        }
        fieldL[y + 1][x + 1] = c;
      }
      REP(x, w) {
        cin >> c;
        switch (c) {
        case '%':
          c = '.';
          rg.first = x + 1;
          rg.second = y + 1;
          break;
        case 'R':
          c = '.';
          rs.first = x + 1;
          rs.second = y + 1;
          break;
        }
        fieldR[y + 1][x + 1] = c;
      }
    }

    fill_n((bool *)dfsG, MAX_W * MAX_H, false);
    bool lres = dfsL(ls, lg);
    fill_n((bool *)dfsG, MAX_W * MAX_H, false);
    bool rres = dfsR(rs, rg);
    if (!lres || !rres) {
      cout << "No!!" << endl;
    }

    /*
    LOG("------\\n");
    REP(y, h + 2){
            REP(x, w + 2){
                    LOG("%c", fieldL[y][x]);
            }
            LOG("\\n");
    }
    LOG("------\\n");
    REP(y, h + 2){
            REP(x, w + 2){
                    LOG("%c", fieldR[y][x]);
            }
            LOG("\\n");
    }
    LOG("------\\n");
    */

    //		LOG("%d %d ,, %d %d\\n", ls.first, ls.second , rs.first,
    //rs.second);

    set<pair<pii, pii>> G;
    queue<pair<pii, pii>> que;
    que.push({ls, rs});
    bool res = false;
    while (!que.empty()) {
      pii L = que.front().first;
      pii R = que.front().second;
      que.pop();

      //			LOG("%d %d |||| %d %d\\n", L.first, L.second ,
      //R.first, R.second);

      if (L == lg) {
        if (R == rg) {
          res = true;
          break;
        }
        continue;
      } else if (R == rg)
        continue;

      REP(i, 4) {
        pii nextL = {L.first + dx[i], L.second + dy[i]};
        pii nextR = {R.first - dx[i], R.second + dy[i]};
        if (fieldL[nextL.second][nextL.first] == '#') {
          nextL = L;
        }
        if (fieldR[nextR.second][nextR.first] == '#') {
          nextR = R;
        }
        pair<pii, pii> next = {nextL, nextR};

        //				LOG("%c %c   %d %d  <<<--- %d %d\\n",
        //fieldL[nextL.second][nextL.first],fieldR[nextR.second][nextR.first],
        //nextR.first, nextR.second, R.first, R.second);
        if (G.insert(next).second)
          que.push({nextL, nextR});
      }
    }

    cout << (res ? "Yes" : "No") << endl;
  }
}