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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int toHash(int y1, int x1, int y2, int x2) {
  return ((y1 * 52 + x1) * 52 + y2) * 52 + x2;
}

void fromHash(int hash, int &y1, int &x1, int &y2, int &x2) {
  x2 = hash % 52;
  hash /= 52;
  y2 = hash % 52;
  hash /= 52;
  x1 = hash % 52;
  hash /= 52;
  y1 = hash % 52;
}

int main() {
  for (;;) {
    int h, w;
    cin >> w >> h;
    if (h == 0)
      return 0;

    int sy1, sx1, sy2, sx2;
    vector<string> grid1(h + 2, string(w + 2, '#'));
    vector<string> grid2(h + 2, string(w + 2, '#'));
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        cin >> grid1[i][j];
        if (grid1[i][j] == 'L') {
          sy1 = i;
          sx1 = j;
        }
      }
      for (int j = 1; j <= w; ++j) {
        cin >> grid2[i][j];
        if (grid2[i][j] == 'R') {
          sy2 = i;
          sx2 = j;
        }
      }
    }

    queue<int> q;
    vector<bool> history(52 * 52 * 52 * 52, false);
    q.push(toHash(sy1, sx1, sy2, sx2));
    history[toHash(sy1, sx1, sy2, sx2)] = true;

    bool ret = false;
    while (!q.empty()) {
      int y1, x1, y2, x2;
      fromHash(q.front(), y1, x1, y2, x2);
      q.pop();
      if (grid1[y1][x1] == '%' && grid2[y2][x2] == '%') {
        ret = true;
        break;
      }
      if (grid1[y1][x1] == '%' || grid2[y2][x2] == '%')
        continue;

      for (int i = 0; i < 4; ++i) {
        int y3 = y1 + dy[i];
        int x3 = x1 + dx[i];
        int y4 = y2 + dy[i];
        int x4 = x2 + dx[i ^ 1];

        if (grid1[y3][x3] == '#') {
          y3 -= dy[i];
          x3 -= dx[i];
        }
        if (grid2[y4][x4] == '#') {
          y4 -= dy[i];
          x4 -= dx[i ^ 1];
        }

        int hash = toHash(y3, x3, y4, x4);
        if (!history[hash]) {
          q.push(hash);
          history[hash] = true;
        }
      }
    }

    if (ret)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int toHash(int y1, int x1, int y2, int x2) {
  return y1 * 1000000 + x1 * 10000 + y2 * 100 + x2;
}

void fromHash(int hash, int &y1, int &x1, int &y2, int &x2) {
  y1 = hash / 1000000 % 100;
  x1 = hash / 10000 % 100;
  y2 = hash / 100 % 100;
  x2 = hash / 1 % 100;
}

int main() {
  for (;;) {
    int h, w;
    cin >> w >> h;
    if (h == 0)
      return 0;

    int sy1, sx1, sy2, sx2;
    vector<string> grid1(h + 2, string(w + 2, '#'));
    vector<string> grid2(h + 2, string(w + 2, '#'));
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        cin >> grid1[i][j];
        if (grid1[i][j] == 'L') {
          sy1 = i;
          sx1 = j;
        }
      }
      for (int j = 1; j <= w; ++j) {
        cin >> grid2[i][j];
        if (grid2[i][j] == 'R') {
          sy2 = i;
          sx2 = j;
        }
      }
    }

    queue<int> q;
    set<int> history;
    q.push(toHash(sy1, sx1, sy2, sx2));
    history.insert(toHash(sy1, sx1, sy2, sx2));

    bool ret = false;
    while (!q.empty()) {
      int y1, x1, y2, x2;
      fromHash(q.front(), y1, x1, y2, x2);
      q.pop();
      if (grid1[y1][x1] == '%' && grid2[y2][x2] == '%') {
        ret = true;
        break;
      }
      if (grid1[y1][x1] == '%' || grid2[y2][x2] == '%')
        continue;

      for (int i = 0; i < 4; ++i) {
        int y3 = y1 + dy[i];
        int x3 = x1 + dx[i];
        int y4 = y2 + dy[i];
        int x4 = x2 + dx[i ^ 1];

        if (grid1[y3][x3] == '#') {
          y3 -= dy[i];
          x3 -= dx[i];
        }
        if (grid2[y4][x4] == '#') {
          y4 -= dy[i];
          x4 -= dx[i ^ 1];
        }

        int hash = toHash(y3, x3, y4, x4);
        if (history.find(hash) == history.end()) {
          q.push(hash);
          history.insert(hash);
        }
      }
    }

    if (ret)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}