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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
//[int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int dy[] = { 0, -1, 1, -1, 1, 0,
//-1, 1 };
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
int place(int x, int y, int w) { return w * x + y; }

/*--------------------template--------------------*/
int n;
int hs(int x, int y) { return x * n + y; }
vector<bool> st;
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vs fldl(h), fldr(h);
    REP(i, h) cin >> fldl[i] >> fldr[i];
    n = w * h;
    st.clear();
    st.resize(n * n);
    int rx, ry, lx, ly;
    REP(i, h) REP(j, w) {
      if (fldl[i][j] == 'L') {
        fldl[i][j] = '.';
        lx = i, ly = j;
      }
      if (fldr[i][j] == 'R') {
        fldr[i][j] = '.';
        rx = i, ry = j;
      }
    }
    queue<int> que;
    st[hs(place(lx, ly, w), place(rx, ry, w))] = true;
    que.push(hs(place(lx, ly, w), place(rx, ry, w)));
    bool ans = false;
    while (que.size()) {
      int tmp = que.front();
      que.pop();
      int tl = tmp / n, tr = tmp % n;
      int lx = tl / w, ly = tl % w;
      int rx = tr / w, ry = tr % w;
      if (fldl[lx][ly] == '%' || fldr[rx][ry] == '%') {
        if (fldl[lx][ly] == '%' && fldr[rx][ry] == '%') {
          ans = true;
          break;
        } else
          continue;
      }
      REP(i, 4) {
        int lnx, lny, rnx, rny;
        if (valid(lx + dx[i], ly + dy[i], h, w) &&
            fldl[lx + dx[i]][ly + dy[i]] != '#') {
          lnx = lx + dx[i], lny = ly + dy[i];
        } else {
          lnx = lx, lny = ly;
        }
        if (valid(rx + dx[i], ry - dy[i], h, w) &&
            fldr[rx + dx[i]][ry - dy[i]] != '#') {
          rnx = rx + dx[i], rny = ry - dy[i];
        } else {
          rnx = rx, rny = ry;
        }
        int next = hs(place(lnx, lny, w), place(rnx, rny, w));
        if (!st[next]) {
          st[next] = true;
          que.push(next);
        }
      }
    }
    puts(ans ? "Yes" : "No");
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
//[int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int dy[] = { 0, -1, 1, -1, 1, 0,
//-1, 1 };
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
int place(int x, int y, int w) { return w * x + y; }

/*--------------------template--------------------*/
int n;
int hs(int x, int y) { return x * n + y; }
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vs fldl(h), fldr(h);
    REP(i, h) cin >> fldl[i] >> fldr[i];
    n = w * h;
    int rx, ry, lx, ly;
    REP(i, h) REP(j, w) {
      if (fldl[i][j] == 'L') {
        fldl[i][j] = '.';
        lx = i, ly = j;
      }
      if (fldr[i][j] == 'R') {
        fldr[i][j] = '.';
        rx = i, ry = j;
      }
    }
    set<int> st;
    queue<int> que;
    st.insert(hs(place(lx, ly, w), place(rx, ry, w)));
    que.push(hs(place(lx, ly, w), place(rx, ry, w)));
    bool ans = false;
    while (que.size()) {
      int tmp = que.front();
      que.pop();
      int tl = tmp / n, tr = tmp % n;
      int lx = tl / w, ly = tl % w;
      int rx = tr / w, ry = tr % w;
      if (fldl[lx][ly] == '%' && fldr[rx][ry] == '%') {
        ans = true;
        break;
      }
      REP(i, 4) {
        int lnx, lny, rnx, rny;
        if (valid(lx + dx[i], ly + dy[i], h, w) &&
            fldl[lx + dx[i]][ly + dy[i]] != '#') {
          lnx = lx + dx[i], lny = ly + dy[i];
        } else {
          lnx = lx, lny = ly;
        }
        if (valid(rx + dx[i], ry - dy[i], h, w) &&
            fldr[rx + dx[i]][ry - dy[i]] != '#') {
          rnx = rx + dx[i], rny = ry - dy[i];
        } else {
          rnx = rx, rny = ry;
        }
        int next = hs(place(lnx, lny, w), place(rnx, rny, w));
        if (st.find(next) == st.end()) {
          st.insert(next);
          que.push(next);
        }
      }
    }
    puts(ans ? "Yes" : "No");
  }
  return 0;
}