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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __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(ALL(c))
#define RSORT(c) sort(RALL(c))

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;

int W, H;
char RoomL[52][52];
char RoomR[52][52];

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

struct P {
  int lx, ly, rx, ry;
};

bool is_reachable(int x, int y, char room[52][52]) {
  bool G[52][52] = {};
  G[y][x] = true;
  queue<pii> que;
  que.push({x, y});
  while (!que.empty()) {
    pii p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    REP(i, 4) {
      int sx = x + dx[i], sy = y + dy[i];
      switch (room[sy][sx]) {
      case '%':
        return true;
      case '#':
        break;
      default:
        if (!G[sy][sx]) {
          G[sy][sx] = true;
          que.push({sx, sy});
        }
      }
    }
  }
  return false;
}

int main() {
  while (cin >> W >> H, W | H) {
    fill_n((char *)RoomL, 52 * 52, '#');
    fill_n((char *)RoomR, 52 * 52, '#');

    int lx, ly, rx, ry;
    int lgx, lgy, rgx, rgy;
    FOR(y, 1, H + 1) {
      FOR(x, 1, W + 1) {
        cin >> RoomL[y][x];
        switch (RoomL[y][x]) {
        case 'L':
          lx = x;
          ly = y;
          RoomL[y][x] = '.';
          break;
        case '%':
          lgx = x;
          lgy = y;
          break;
        }
      }
      FOR(x, 1, W + 1) {
        cin >> RoomR[y][x];
        switch (RoomR[y][x]) {
        case 'R':
          rx = x;
          ry = y;
          RoomR[y][x] = '.';
          break;
        case '%':
          rgx = x;
          rgy = y;
          break;
        }
      }
    }

    if (!(is_reachable(lx, ly, RoomL) && is_reachable(rx, ry, RoomR))) {
      cout << "No" << endl;
      continue;
    }

    bool G[52][52][52][52] = {};
    G[lx][ly][rx][ry] = true;
    queue<P> que;
    que.push({lx, ly, rx, ry});
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      if (p.lx == lgx && p.ly == lgy && p.rx == rgx && p.ry == rgy) {
        cout << "Yes" << endl;
        goto END;
      }

      REP(i, 4) {
        int lsx = p.lx - dx[i], lsy = p.ly + dy[i];
        int rsx = p.rx + dx[i], rsy = p.ry + dy[i];
        if (RoomL[lsy][lsx] == '#') {
          lsx = p.lx;
          lsy = p.ly;
        }
        if (RoomR[rsy][rsx] == '#') {
          rsx = p.rx;
          rsy = p.ry;
        }
        if (RoomR[rsy][rsx] == '%' || RoomL[lsy][lsx] == '%') {
          if (RoomR[rsy][rsx] == '%' && RoomL[lsy][lsx] == '%') {
          } else {
            continue;
          }
        }
        if (!G[lsx][lsy][rsx][rsy]) {
          //                    LOG("-----\\n%d %d # %d %d\\n-----\\n", lsx, lsy,
          //                    rsx, rsy); REP(y, H+2) {
          //                        REP(x, W+2) {
          //                            if (lsx == x && lsy == y) {
          //                                printf("L");
          //                            } else {
          //                                printf("%c", RoomL[y][x]);
          //                            }
          //                        }
          //                        printf("\\n");
          //                    }
          //                    REP(y, H+2) {
          //                        REP(x, W+2) {
          //                            if (rsx == x && rsy == y) {
          //                                printf("R");
          //                            } else {
          //                                printf("%c", RoomR[y][x]);
          //                            }
          //                        }
          //                        printf("\\n");
          //                    }
          G[lsx][lsy][rsx][rsy] = true;
          que.push({lsx, lsy, rsx, rsy});
        }
      }
    }
    cout << "No" << endl;
  END : {}
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __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(ALL(c))
#define RSORT(c) sort(RALL(c))

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;

int W, H;
char RoomL[50][50];
char RoomR[50][50];

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

struct P {
  int lx, ly, rx, ry;
};

bool is_reachable(int x, int y, char room[50][50]) {
  bool G[50][50] = {};
  queue<pii> que;
  que.push({x, y});
  while (!que.empty()) {
    pii p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    REP(i, 4) {
      int sx = x + dx[i], sy = y + dy[i];
      if (0 <= sx && sx < W && 0 <= sy && sy < H) {
        switch (room[sy][sx]) {
        case '%':
          return true;
        case '#':
          break;
        default:
          if (!G[sy][sx]) {
            G[sy][sx] = true;
            que.push({sx, sy});
          }
        }
      }
    }
  }
  return false;
}

int main() {
  while (cin >> W >> H, W | H) {
    fill_n((char *)RoomL, 50 * 50, '#');
    fill_n((char *)RoomR, 50 * 50, '#');

    int lx, ly, rx, ry;
    int lgx, lgy, rgx, rgy;
    REP(y, H) {
      REP(x, W) {
        cin >> RoomL[y][x];
        switch (RoomL[y][x]) {
        case 'L':
          lx = x;
          ly = y;
          RoomL[y][x] = '.';
          break;
        case '%':
          lgx = x;
          lgy = y;
          break;
        }
      }
      REP(x, W) {
        cin >> RoomR[y][x];
        switch (RoomR[y][x]) {
        case 'R':
          rx = x;
          ry = y;
          RoomR[y][x] = '.';
          break;
        case '%':
          rgx = x;
          rgy = y;
          break;
        }
      }
    }

    LOG("%d %d", is_reachable(lx, ly, RoomL), is_reachable(rx, ry, RoomR));
    if (!(is_reachable(lx, ly, RoomL) && is_reachable(rx, ry, RoomR))) {
      cout << "No" << endl;
      break;
    }

    set<int> G;

    queue<P> que;
    que.push({lx, ly, rx, ry});
    G.insert(lx + ly * 100 + rx * 10000 + ry * 1000000);
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      if (p.lx == lgx && p.ly == lgy && p.rx == rgx && p.ry == rgy) {
        cout << "Yes" << endl;
        goto END;
      }

      REP(i, 4) {
        int lsx = p.lx - dx[i], lsy = p.ly + dy[i];
        int rsx = p.rx + dx[i], rsy = p.ry + dy[i];
        if (!(0 <= lsx && lsx < W && 0 <= lsy && lsy < H) ||
            RoomL[lsy][lsx] == '#') {
          lsx = p.lx;
          lsy = p.lx;
        }
        if (!(0 <= rsx && rsx < W && 0 <= rsy && rsy < H) ||
            RoomR[rsy][rsx] == '#') {
          rsx = p.rx;
          rsy = p.rx;
        }
        if (RoomR[rsy][rsx] == '%' || RoomL[lsy][lsx] == '%') {
          if (RoomR[rsy][rsx] == '%' && RoomL[lsy][lsx] == '%') {
          } else {
            continue;
          }
        }
        if (G.insert(lsx + lsy * 100 + rsx * 10000 + rsy * 1000000).second) {
          //                    LOG("-----\\n%d %d # %d %d\\n-----\\n", lsx, lsy,
          //                    rsx, rsy); REP(y, H) {
          //                        REP(x, W) {
          //                            if (lsx == x && lsy == y) {
          //                                printf("L");
          //                            } else {
          //                                printf("%c", RoomL[y][x]);
          //                            }
          //                        }
          //                        printf("\\n");
          //                    }
          //                    REP(y, H) {
          //                        REP(x, W) {
          //                            if (rsx == x && rsy == y) {
          //                                printf("R");
          //                            } else {
          //                                printf("%c", RoomR[y][x]);
          //                            }
          //                        }
          //                        printf("\\n");
          //                    }

          que.push({lsx, lsy, rsx, rsy});
        }
      }
    }
    cout << "No" << endl;
  END : {}
  }
}