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

#include <iostream>
#include <queue>
#include <string>
#include <tuple>
using namespace std;

#define MAX_N 63
#define INF 1 << 30

int dist[MAX_N][MAX_N][MAX_N][MAX_N];
int dy1[4] = {0, 1, 0, -1};
int dy2[4] = {0, 1, 0, -1};
int dx1[4] = {1, 0, -1, 0};
int dx2[4] = {-1, 0, 1, 0};
int H, W, map1[MAX_N][MAX_N], map2[MAX_N][MAX_N];
int Lsy, Lsx, Lgy, Lgx;
int Rsy, Rsx, Rgy, Rgx;
string S, T;
queue<tuple<int, int, int, int>> Q;

void _memset() {
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      for (int k = 0; k < MAX_N; k++) {
        for (int l = 0; l < MAX_N; l++) {
          dist[i][j][k][l] = INF;
        }
      }
    }
  }

  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      map1[i][j] = 1;
      map2[i][j] = 1;
    }
  }
}

int main() {
  while (true) {
    // memset.
    _memset();

    // cin.
    cin >> W >> H;
    if (H == 0 && W == 0) {
      break;
    }
    for (int i = 1; i <= H; i++) {
      cin >> S >> T;
      for (int j = 0; j < S.size(); j++) {
        if (S[j] == 'L') {
          Lsx = j + 1;
          Lsy = i;
          map1[i][j + 1] = 0;
        }
        if (S[j] == '#') {
          map1[i][j + 1] = 1;
        }
        if (S[j] == '.') {
          map1[i][j + 1] = 0;
        }
        if (S[j] == '%') {
          Lgy = i;
          Lgx = j + 1;
          map1[i][j + 1] = 0;
        }
      }
      for (int j = 0; j < T.size(); j++) {
        if (T[j] == 'R') {
          Rsx = j + 1;
          Rsy = i;
          map2[i][j + 1] = 0;
        }
        if (T[j] == '#') {
          map2[i][j + 1] = 1;
        }
        if (T[j] == '.') {
          map2[i][j + 1] = 0;
        }
        if (T[j] == '%') {
          Rgy = i;
          Rgx = j + 1;
          map2[i][j + 1] = 0;
        }
      }
    }

    // bfs.
    Q.push(make_tuple(Lsy, Lsx, Rsy, Rsx));
    dist[Lsy][Lsx][Rsy][Rsx] = 0;

    while (!Q.empty()) {
      tuple<int, int, int, int> tup = Q.front();
      Q.pop();
      bool Q1, Q2;
      int ay = get<0>(tup);
      int ax = get<1>(tup);
      int by = get<2>(tup);
      int bx = get<3>(tup);
      for (int i = 0; i < 4; i++) {
        int ey = ay + dy1[i], ex = ax + dx1[i];
        int fy = by + dy2[i], fx = bx + dx2[i];
        if (map1[ey][ex] == 1) {
          ey = ay;
          ex = ax;
        }
        if (map2[fy][fx] == 1) {
          fy = by;
          fx = bx;
        }
        if (ey == Lgy && ex == Lgx) {
          Q1 = true;
        } else {
          Q1 = false;
        }
        if (fy == Rgy && fx == Rgx) {
          Q2 = true;
        } else {
          Q2 = false;
        }
        if (Q1 == true && Q2 == false) {
          goto G;
        }
        if (Q1 == false && Q2 == true) {
          goto G;
        }
        if (dist[ey][ex][fy][fx] == INF) {
          dist[ey][ex][fy][fx] = dist[ay][ax][by][bx] + 1;
          Q.push(make_tuple(ey, ex, fy, fx));
        }
      G:;
      }
    }

    // hantei.
    if (dist[Lgy][Lgx][Rgy][Rgx] == INF) {
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <tuple>
using namespace std;

#define MAX_N 70
#define INF 1 << 30

int dist[MAX_N][MAX_N][MAX_N][MAX_N];
int dy1[4] = {0, 1, 0, -1};
int dy2[4] = {0, 1, 0, -1};
int dx1[4] = {1, 0, -1, 0};
int dx2[4] = {-1, 0, 1, 0};
int H, W, map1[MAX_N][MAX_N], map2[MAX_N][MAX_N];
int Lsy, Lsx, Lgy, Lgx;
int Rsy, Rsx, Rgy, Rgx;
string S, T;
queue<tuple<int, int, int, int>> Q;

void _memset() {
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      for (int k = 0; k < MAX_N; k++) {
        for (int l = 0; l < MAX_N; l++) {
          dist[i][j][k][l] = INF;
        }
      }
    }
  }

  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      map1[i][j] = 1;
      map2[i][j] = 1;
    }
  }
}

int main() {
  while (true) {
    // memset.
    _memset();

    // cin.
    cin >> W >> H;
    if (H == 0 && W == 0) {
      break;
    }
    for (int i = 1; i <= H; i++) {
      cin >> S >> T;
      for (int j = 0; j < S.size(); j++) {
        if (S[j] == 'L') {
          Lsx = j + 1;
          Lsy = i;
          map1[i][j + 1] = 0;
        }
        if (S[j] == '#') {
          map1[i][j + 1] = 1;
        }
        if (S[j] == '.') {
          map1[i][j + 1] = 0;
        }
        if (S[j] == '%') {
          Lgy = i;
          Lgx = j + 1;
          map1[i][j + 1] = 0;
        }
      }
      for (int j = 0; j < T.size(); j++) {
        if (T[j] == 'R') {
          Rsx = j + 1;
          Rsy = i;
          map2[i][j + 1] = 0;
        }
        if (T[j] == '#') {
          map2[i][j + 1] = 1;
        }
        if (T[j] == '.') {
          map2[i][j + 1] = 0;
        }
        if (T[j] == '%') {
          Rgy = i;
          Rgx = j + 1;
          map2[i][j + 1] = 0;
        }
      }
    }

    // bfs.
    Q.push(make_tuple(Lsy, Lsx, Rsy, Rsx));
    dist[Lsy][Lsx][Rsy][Rsx] = 0;

    while (!Q.empty()) {
      tuple<int, int, int, int> tup = Q.front();
      Q.pop();
      bool Q1, Q2;
      int ay = get<0>(tup);
      int ax = get<1>(tup);
      int by = get<2>(tup);
      int bx = get<3>(tup);
      for (int i = 0; i < 4; i++) {
        int ey = ay + dy1[i], ex = ax + dx1[i];
        int fy = by + dy2[i], fx = bx + dx2[i];
        if (map1[ey][ex] == 1) {
          ey = ay;
          ex = ax;
        }
        if (map2[fy][fx] == 1) {
          fy = by;
          fx = bx;
        }
        if (ey == Lgy && ex == Lgx) {
          Q1 = true;
        } else {
          Q1 = false;
        }
        if (fy == Rgy && fx == Rgx) {
          Q2 = true;
        } else {
          Q2 = false;
        }
        if (Q1 == true && Q2 == false) {
          goto G;
        }
        if (Q1 == false && Q2 == true) {
          goto G;
        }
        if (dist[ey][ex][fy][fx] == INF) {
          dist[ey][ex][fy][fx] = dist[ay][ax][by][bx] + 1;
          Q.push(make_tuple(ey, ex, fy, fx));
        }
      G:;
      }
    }

    // hantei.
    if (dist[Lgy][Lgx][Rgy][Rgx] == INF) {
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
    }
  }
  return 0;
}