Problem p01271 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0 and H == 0)
      break;
    vector<vector<string>> field(2, vector<string>(H + 2, string(W + 2, '#')));

    for (int i = 1; i <= H; i++) {
      for (int j = 0; j < 2; j++) {
        cin >> field[j][i];
        field[j][i] = '#' + field[j][i] + '#';
      }
    }
    pair<pii, pii> start;
    pair<pii, pii> goal;
    for (int i = 0; i < 2; i++) {
      for (int j = 1; j <= H; j++) {
        for (int k = 1; k <= W; k++) {
          if (field[i][j][k] == 'L') {
            start.first = make_pair(j, k);
          } else if (field[i][j][k] == 'R') {
            start.second = make_pair(j, k);
          } else if (field[i][j][k] == '%') {
            if (i == 0) {
              goal.first = make_pair(j, k);
            } else {
              goal.second = make_pair(j, k);
            }
          }
        }
      }
    }
    stack<pair<pii, pii>> sta;
    sta.push(start);
    bool used[H + 2][W + 2][H + 2][W + 2];
    for (int i = 0; i < H + 2; i++) {
      for (int j = 0; j < W + 2; j++) {
        for (int k = 0; k < H + 2; k++) {
          for (int l = 0; l < W + 2; l++) {
            used[i][j][k][l] = false;
          }
        }
      }
    }
    bool ok = false;
    while (not sta.empty()) {
      pair<pii, pii> now = sta.top();
      sta.pop();
      if (used[now.first.first][now.first.second][now.second.first]
              [now.second.second]) {
        continue;
      }
      used[now.first.first][now.first.second][now.second.first]
          [now.second.second] = true;
      if (now == goal) {
        ok = true;
        break;
      }
      if (now.first == goal.first) {
        continue;
      }
      if (now.second == goal.second) {
        continue;
      }
      for (int i = 0; i < 4; i++) {
        pair<pii, pii> next = now;
        next.first.first += dy[i];
        next.first.second += dx[i];
        next.second.first += dy[i];
        next.second.second -= dx[i];
        if (field[0][next.first.first][next.first.second] == '#') {
          next.first = now.first;
        }
        if (field[1][next.second.first][next.second.second] == '#') {
          next.second = now.second;
        }
        sta.push(next);
      }
    }
    if (ok) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<char, char> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0 and H == 0)
      break;
    vector<vector<string>> field(2, vector<string>(H + 2, string(W + 2, '#')));

    for (int i = 1; i <= H; i++) {
      for (int j = 0; j < 2; j++) {
        cin >> field[j][i];
        field[j][i] = '#' + field[j][i] + '#';
      }
    }
    pair<pii, pii> start;
    pair<pii, pii> goal;
    for (int i = 0; i < 2; i++) {
      for (int j = 1; j <= H; j++) {
        for (int k = 1; k <= W; k++) {
          if (field[i][j][k] == 'L') {
            start.first = make_pair(j, k);
          } else if (field[i][j][k] == 'R') {
            start.second = make_pair(j, k);
          } else if (field[i][j][k] == '%') {
            if (i == 0) {
              goal.first = make_pair(j, k);
            } else {
              goal.second = make_pair(j, k);
            }
          }
        }
      }
    }
    stack<pair<pii, pii>> sta;
    sta.push(start);
    bool used[H + 2][W + 2][H + 2][W + 2];
    for (int i = 0; i < H + 2; i++) {
      for (int j = 0; j < W + 2; j++) {
        for (int k = 0; k < H + 2; k++) {
          for (int l = 0; l < W + 2; l++) {
            used[i][j][k][l] = false;
          }
        }
      }
    }
    bool ok = false;
    while (not sta.empty()) {
      pair<pii, pii> now = sta.top();
      sta.pop();
      if (used[now.first.first][now.first.second][now.second.first]
              [now.second.second]) {
        continue;
      }
      used[now.first.first][now.first.second][now.second.first]
          [now.second.second] = true;
      if (now == goal) {
        ok = true;
        break;
      }
      if (now.first == goal.first) {
        continue;
      }
      if (now.second == goal.second) {
        continue;
      }
      for (int i = 0; i < 4; i++) {
        pair<pii, pii> next = now;
        next.first.first += dy[i];
        next.first.second += dx[i];
        next.second.first += dy[i];
        next.second.second -= dx[i];
        if (field[0][next.first.first][next.first.second] == '#') {
          next.first = now.first;
        }
        if (field[1][next.second.first][next.second.second] == '#') {
          next.second = now.second;
        }
        sta.push(next);
      }
    }
    if (ok) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}