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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int dx1[] = {0, 1, 0, -1};
int dy1[] = {-1, 0, 1, 0};
int dx2[] = {0, -1, 0, 1};
int dy2[] = {-1, 0, 1, 0};

bool visited[50][50][50][50];

int w, h;
char ba1[50][50];
char ba2[50][50];

typedef pair<int, int> pii;
typedef pair<pii, pii> P;

bool solve(int x1, int y1, int x2, int y2) {
  //  printf("(%d,%d),(%d,%d)\\n",x1,y1,x2,y2);

  queue<P> Q;
  Q.push(P(pii(x1, y1), pii(x2, y2)));
  while (!Q.empty()) {
    pii p1 = Q.front().first;
    pii p2 = Q.front().second;
    int x1 = p1.first;
    int y1 = p1.second;
    int x2 = p2.first;
    int y2 = p2.second;
    Q.pop();
    if (visited[x1][y1][x2][y2])
      continue;

    visited[x1][y1][x2][y2] = 1;

    if (ba1[x1][y1] == '%' && ba2[x2][y2] == '%')
      return 1;
    else if (ba1[x1][y1] == '%' || ba2[x2][y2] == '%')
      continue;
    REP(i, 4) {
      int xx1 = x1 + dx1[i];
      int yy1 = y1 + dy1[i];
      int xx2 = x2 + dx2[i];
      int yy2 = y2 + dy2[i];

      if (xx1 < 0 || xx1 >= w || yy1 < 0 || yy1 >= h || ba1[xx1][yy1] == '#') {
        xx1 = x1;
        yy1 = y1;
      }
      if (xx2 < 0 || xx2 >= w || yy2 < 0 || yy2 >= h || ba2[xx2][yy2] == '#') {
        xx2 = x2;
        yy2 = y2;
      }
      if (!visited[xx1][yy1][xx2][yy2]) {
        Q.push(P(pii(xx1, yy1), pii(xx2, yy2)));
      }
    }
  }
  return 0;
}

int main() {
  while (cin >> w >> h, w || h) {

    int sx1, sy1, sx2, sy2;

    REP(y, h) {
      REP(x, w) {
        cin >> ba1[x][y];

        if (ba1[x][y] == 'L') {
          sx1 = x;
          sy1 = y;
        }
      }
      REP(x, w) {
        cin >> ba2[x][y];
        if (ba2[x][y] == 'R') {
          sx2 = x;
          sy2 = y;
        }
      }
    }
    memset(visited, 0, sizeof(visited));
    if (solve(sx1, sy1, sx2, sy2))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int dx1[] = {0, 1, 0, -1};
int dy1[] = {-1, 0, 1, 0};
int dx2[] = {0, -1, 0, 1};
int dy2[] = {-1, 0, 1, 0};

bool visited[50][50][50][50];

int w, h;
char ba1[50][50];
char ba2[50][50];

bool rec(int x1, int y1, int x2, int y2) {
  // printf("(%d,%d),(%d,%d)\\n",x1,y1,x2,y2);
  visited[x1][y1][x2][y2] = 1;

  if (ba1[x1][y1] == '%' && ba2[x2][y2] == '%')
    return 1;
  else if (ba1[x1][y1] == '%' || ba2[x2][y2] == '%')
    return 0;
  REP(i, 4) {
    int xx1 = x1 + dx1[i];
    int yy1 = y1 + dy1[i];
    int xx2 = x2 + dx2[i];
    int yy2 = y2 + dy2[i];

    if (xx1 < 0 || xx1 >= w || yy1 < 0 || yy1 >= h || ba1[xx1][yy1] == '#') {
      xx1 = x1;
      yy1 = y1;
    }
    if (xx2 < 0 || xx2 >= w || yy2 < 0 || yy2 >= h || ba2[xx2][yy2] == '#') {
      xx2 = x2;
      yy2 = y2;
    }
    if (!visited[xx1][yy1][xx2][yy2]) {
      if (rec(xx1, yy1, xx2, yy2))
        return 1;
    }
  }
  return 0;
}

int main() {
  while (cin >> w >> h, w || h) {

    int sx1, sy1, sx2, sy2;

    REP(y, h) {
      REP(x, w) {
        cin >> ba1[x][y];

        if (ba1[x][y] == 'L') {
          sx1 = x;
          sy1 = y;
        }
      }
      REP(x, w) {
        cin >> ba2[x][y];
        if (ba2[x][y] == 'R') {
          sx2 = x;
          sy2 = y;
        }
      }
    }
    memset(visited, 0, sizeof(visited));
    if (rec(sx1, sy1, sx2, sy2))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}