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) begin(a), end(a)
#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;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
typedef double weight;
struct edge {
  int to;
  weight cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
typedef vector<vector<edge>> Graph;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
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); }
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; }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

bool used[51][51][51][51];

int main() {
  int h, w;
  while (cin >> w >> h, w) {
    vs fld1(h), fld2(h);
    REP(i, h) cin >> fld1[i] >> fld2[i];
    pii sa, sb, ga, gb;
    REP(i, h) REP(j, w) {
      if (fld1[i][j] == 'L')
        sa = pii(i, j);
      if (fld2[i][j] == 'R')
        sb = pii(i, j);
      if (fld1[i][j] == '%')
        ga = pii(i, j);
      if (fld2[i][j] == '%')
        gb = pii(i, j);
    }
    typedef pair<pii, pii> data;
    //		set<data> st;
    queue<data> que;
    que.push(data(sa, sb));
    data goal = data(ga, gb);
    MS(used, false);
    while (que.size()) {
      data tmp = que.front();
      que.pop();
      int lx = tmp.first.first, ly = tmp.first.second;
      int rx = tmp.second.first, ry = tmp.second.second;
      REP(i, 4) {
        int lnx = lx + dx[i], lny = ly + dy[i];
        int rnx = rx + dx[i], rny = ry - dy[i];
        if (!valid(lnx, lny, h, w) || fld1[lnx][lny] == '#') {
          lnx = lx, lny = ly;
        }
        if (!valid(rnx, rny, h, w) || fld2[rnx][rny] == '#') {
          rnx = rx, rny = ry;
        }
        pii nl(lnx, lny), nr(rnx, rny);
        data next = data(nl, nr);
        if (next != goal && (nl == ga || nr == gb))
          continue;
        //				if (st.find(next) != st.end()) continue;
        //				st.insert(next);
        if (used[lnx][lny][rnx][rny])
          continue;
        used[lnx][lny][rnx][rny] = true;
        if (next == goal)
          goto end;
        que.push(next);
      }
    }
  end:;
    //		if (st.find(goal) == st.end()) puts("No");
    if (used[ga.first][ga.second][gb.first][gb.second])
      puts("Yes");
    else
      puts("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) begin(a), end(a)
#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;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
typedef double weight;
struct edge {
  int to;
  weight cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
typedef vector<vector<edge>> Graph;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
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); }
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; }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main() {
  int h, w;
  while (cin >> w >> h, w) {
    vs fld1(h), fld2(h);
    REP(i, h) cin >> fld1[i] >> fld2[i];
    pii sa, sb, ga, gb;
    REP(i, h) REP(j, w) {
      if (fld1[i][j] == 'L')
        sa = pii(i, j);
      if (fld2[i][j] == 'R')
        sb = pii(i, j);
      if (fld1[i][j] == '%')
        ga = pii(i, j);
      if (fld2[i][j] == '%')
        gb = pii(i, j);
    }
    typedef pair<pii, pii> data;
    set<data> st;
    queue<data> que;
    que.push(data(sa, sb));
    data goal = data(ga, gb);
    while (que.size()) {
      data tmp = que.front();
      que.pop();
      int lx = tmp.first.first, ly = tmp.first.second;
      int rx = tmp.second.first, ry = tmp.second.second;
      REP(i, 4) {
        int lnx = lx + dx[i], lny = ly + dy[i];
        int rnx = rx + dx[i], rny = ry - dy[i];
        if (!valid(lnx, lny, h, w) || fld1[lnx][lny] == '#') {
          lnx = lx, lny = ly;
        }
        if (!valid(rnx, rny, h, w) || fld2[rnx][rny] == '#') {
          rnx = rx, rny = ry;
        }
        pii nl(lnx, lny), nr(rnx, rny);
        data next = data(nl, nr);
        if (next != goal && (nl == ga || nr == gb))
          continue;
        if (st.find(next) != st.end())
          continue;
        st.insert(next);
        if (next == goal)
          break;
        que.push(next);
      }
    }
    if (st.find(goal) == st.end())
      puts("No");
    else
      puts("Yes");
  }
  return 0;
}