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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

const int dx[2][4] = {{1, 0, -1, 0}, {1, 0, -1, 0}};
const int dy[2][4] = {{0, 1, 0, -1}, {0, -1, 0, 1}};
const int MAX_W = 55;
const int MAX_H = 55;
char maze[2][MAX_H][MAX_W + 1];
int sx[2], sy[2];
int tx[2], ty[2];
int h, w;
bool used[MAX_H][MAX_W][MAX_H][MAX_W];

inline bool check(int hx, int hy) {
  return 0 <= hx && hx < h && 0 <= hy && hy < w;
}

int main() {
  while (cin >> w >> h, w) {
    rep(i, h) rep(j, w) rep(k, h) rep(l, w) used[i][j][k][l] = false;
    rep(i, h) {
      rep(j, w) cin >> maze[0][i][j];
      rep(j, w) cin >> maze[1][i][j];
    }

    rep(i, h) rep(j, w) if (maze[0][i][j] == 'L') sx[0] = i, sy[0] = j;
    rep(i, h) rep(j, w) if (maze[0][i][j] == '%') tx[0] = i, ty[0] = j;
    rep(i, h) rep(j, w) if (maze[1][i][j] == 'R') sx[1] = i, sy[1] = j;
    rep(i, h) rep(j, w) if (maze[1][i][j] == '%') tx[1] = i, ty[1] = j;

    queue<vector<int>> que;
    vector<int> tmp(4);
    tmp[0] = sx[0];
    tmp[1] = sy[0];
    tmp[2] = sx[1];
    tmp[3] = sy[1];
    que.push(tmp);
    //    map<vector<int>, bool> used;
    used[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = true;
    //    used[tmp] = true;
    bool ok = false;
    while (!que.empty()) {
      vector<int> h = que.front();
      que.pop();
      if (h[0] == tx[0] && h[1] == ty[0] && h[2] == tx[1] && h[3] == ty[1]) {
        ok = true;
        break;
      }
      rep(k, 4) {
        int nx[2] = {h[0] + dx[0][k], h[2] + dx[1][k]};
        int ny[2] = {h[1] + dy[0][k], h[3] + dy[1][k]};
        if (!check(nx[0], ny[0]) && !check(nx[1], ny[1]))
          continue;
        if ((nx[0] == tx[0] && ny[0] == ty[0]) &&
            (nx[1] != tx[1] || ny[1] != ty[1]))
          continue;
        if ((nx[0] != tx[0] || ny[0] != ty[0]) &&
            (nx[1] == tx[1] && ny[1] == ty[1]))
          continue;
        if (check(nx[0], ny[0]) && maze[0][nx[0]][ny[0]] != '#') {
          tmp[0] = nx[0];
          tmp[1] = ny[0];
        } else {
          tmp[0] = h[0];
          tmp[1] = h[1];
        }
        if (check(nx[1], ny[1]) && maze[1][nx[1]][ny[1]] != '#') {
          tmp[2] = nx[1];
          tmp[3] = ny[1];
        } else {
          tmp[2] = h[2];
          tmp[3] = h[3];
        }
        if (h != tmp && !used[tmp[0]][tmp[1]][tmp[2]][tmp[3]]) {
          used[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = true;
          //          used[tmp] = true;
          que.push(tmp);
        }
      }
    }

    puts(ok ? "Yes" : "No");
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

const int dx[2][4] = {{1, 0, -1, 0}, {1, 0, -1, 0}};
const int dy[2][4] = {{0, 1, 0, -1}, {0, -1, 0, 1}};
const int MAX_W = 55;
const int MAX_H = 55;
char maze[2][MAX_H][MAX_W + 1];
int sx[2], sy[2];
int tx[2], ty[2];
int h, w;

inline bool check(int hx, int hy) {
  return 0 <= hx && hx < h && 0 <= hy && hy < w;
}

int main() {
  while (cin >> w >> h, w) {
    rep(i, h) {
      rep(j, w) cin >> maze[0][i][j];
      rep(j, w) cin >> maze[1][i][j];
    }

    rep(i, h) rep(j, w) if (maze[0][i][j] == 'L') sx[0] = i, sy[0] = j;
    rep(i, h) rep(j, w) if (maze[0][i][j] == '%') tx[0] = i, ty[0] = j;
    rep(i, h) rep(j, w) if (maze[1][i][j] == 'R') sx[1] = i, sy[1] = j;
    rep(i, h) rep(j, w) if (maze[1][i][j] == '%') tx[1] = i, ty[1] = j;

    stack<vector<int>> que;
    vector<int> tmp(4);
    tmp[0] = sx[0];
    tmp[1] = sy[0];
    tmp[2] = sx[1];
    tmp[3] = sy[1];
    que.push(tmp);
    map<vector<int>, bool> used;
    used[tmp] = true;
    bool ok = false;
    while (!que.empty()) {
      vector<int> h = que.top();
      que.pop();
      if (h[0] == tx[0] && h[1] == ty[0] && h[2] == tx[1] && h[3] == ty[1]) {
        ok = true;
        break;
      }
      rep(k, 4) {
        int nx[2] = {h[0] + dx[0][k], h[2] + dx[1][k]};
        int ny[2] = {h[1] + dy[0][k], h[3] + dy[1][k]};
        if (!check(nx[0], ny[0]) && !check(nx[1], ny[1]))
          continue;
        if ((nx[0] == tx[0] && ny[0] == ty[0]) &&
            (nx[1] != tx[1] || ny[1] != ty[1]))
          continue;
        if ((nx[0] != tx[0] || ny[0] != ty[0]) &&
            (nx[1] == tx[1] && ny[1] == ty[1]))
          continue;
        if (check(nx[0], ny[0]) && maze[0][nx[0]][ny[0]] != '#') {
          tmp[0] = nx[0];
          tmp[1] = ny[0];
        } else {
          tmp[0] = h[0];
          tmp[1] = h[1];
        }
        if (check(nx[1], ny[1]) && maze[1][nx[1]][ny[1]] != '#') {
          tmp[2] = nx[1];
          tmp[3] = ny[1];
        } else {
          tmp[2] = h[2];
          tmp[3] = h[3];
        }
        if (h != tmp && !used[tmp]) {
          used[tmp] = true;
          que.push(tmp);
        }
      }
    }

    puts(ok ? "Yes" : "No");
  }

  return 0;
}