Problem p01271 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int w, h;
pair<int, int> sa, sb, ga, gb;
char mapA[52][52], mapB[52][52];
bool memo[52][52][52][52];

int dxa[] = {0, 0, 1, -1};
int dxb[] = {0, 0, -1, 1};
int dya[] = {1, -1, 0, 0};
int dyb[] = {1, -1, 0, 0};

bool solve(pair<int, int> posA, pair<int, int> posB) {
  queue<pair<pair<int, int>, pair<int, int>>> q;
  q.push(pair<pair<int, int>, pair<int, int>>(posA, posB));

  while (!q.empty()) {
    posA = q.front().first;
    posB = q.front().second;
    q.pop();

    for (int i = 0; i < 4; i++) {
      pair<int, int> na =
          pair<int, int>(posA.first + dxa[i], posA.second + dya[i]);
      pair<int, int> nb =
          pair<int, int>(posB.first + dxb[i], posB.second + dyb[i]);

      if (na.first < 0 || na.first >= w || na.second < 0 || na.second >= h ||
          mapA[na.second][na.first] == '#')
        na = posA;
      if (nb.first < 0 || nb.first >= w || nb.second < 0 || nb.second >= h ||
          mapB[nb.second][nb.first] == '#')
        nb = posB;

      if (na == ga && nb == gb)
        return true;
      if (na == ga || nb == gb)
        continue;
      if (memo[na.first][na.second][nb.first][nb.second])
        continue;

      q.push(pair<pair<int, int>, pair<int, int>>(na, nb));
      memo[na.first][na.second][nb.first][nb.second] = true;
    }
  }
  return false;
}

int main(void) {
  while (cin >> w >> h && (w || h)) {
    memset(memo, 0, sizeof(memo));
    for (int i = 0; i < h; i++) {
      cin >> mapA[i] >> mapB[i];
      for (int j = 0; j < w; j++) {
        if (mapA[i][j] == 'L') {
          sa = pair<int, int>(j, i);
        } else if (mapA[i][j] == '%') {
          ga = pair<int, int>(j, i);
        }
        if (mapB[i][j] == 'R') {
          sb = pair<int, int>(j, i);
        } else if (mapB[i][j] == '%') {
          gb = pair<int, int>(j, i);
        }
      }
    }

    if (solve(sa, sb))
      cout << "Yes\\n";
    else
      cout << "No\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <set>
using namespace std;

int w, h;
pair<int, int> sa, sb, ga, gb;
char mapA[52][52], mapB[52][52];
bool memo[52][52][52][52];

int dxa[] = {0, 0, 1, -1};
int dxb[] = {0, 0, -1, 1};
int dya[] = {1, -1, 0, 0};
int dyb[] = {1, -1, 0, 0};

bool solve(pair<int, int> posA, pair<int, int> posB) {
  memo[posA.first][posA.second][posB.first][posB.second] = true;

  for (int i = 0; i < 4; i++) {
    pair<int, int> na =
        pair<int, int>(posA.first + dxa[i], posA.second + dya[i]);
    pair<int, int> nb =
        pair<int, int>(posB.first + dxb[i], posB.second + dyb[i]);

    if (na.first < 0 || na.first >= w || na.second < 0 || na.second >= h ||
        mapA[na.second][na.first] == '#')
      na = posA;
    if (nb.first < 0 || nb.first >= w || nb.second < 0 || nb.second >= h ||
        mapB[nb.second][nb.first] == '#')
      nb = posB;

    if (na == ga && nb == gb)
      return true;
    if (na == ga || nb == gb)
      continue;
    if (memo[na.first][na.second][nb.first][nb.second])
      continue;
    if (solve(na, nb))
      return true;
  }
  return false;
}

int main(void) {
  while (cin >> w >> h && (w || h)) {
    memset(memo, 0, sizeof(memo));
    for (int i = 0; i < h; i++) {
      cin >> mapA[i] >> mapB[i];
      for (int j = 0; j < w; j++) {
        if (mapA[i][j] == 'L') {
          sa = pair<int, int>(j, i);
        } else if (mapA[i][j] == '%') {
          ga = pair<int, int>(j, i);
        }
        if (mapB[i][j] == 'R') {
          sb = pair<int, int>(j, i);
        } else if (mapB[i][j] == '%') {
          gb = pair<int, int>(j, i);
        }
      }
    }

    if (solve(sa, sb))
      cout << "Yes\\n";
    else
      cout << "No\\n";
  }

  return 0;
}