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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int W, H;
bool memo[51][51][51][51];
string grid1[51];
string grid2[51];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct S {
  int x1, y1, x2, y2;
  S(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};
bool valid1(int x, int y) {
  return (x >= 0 && y >= 0 && x < W && y < H && grid1[y][x] != '#');
}
bool valid2(int x, int y) {
  return (x >= 0 && y >= 0 && x < W && y < H && grid2[y][x] != '#');
}
int main() {
  while (cin >> W >> H && W) {
    REP(i, H) cin >> grid1[i] >> grid2[i];
    memset(memo, 0, sizeof(memo));
    int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
    REP(y, H) REP(x, W) {
      if (grid1[y][x] == 'L') {
        x1 = x;
        y1 = y;
      }
      if (grid2[y][x] == 'R') {
        x2 = x;
        y2 = y;
      }
    }
    assert(x1 != -1 && y1 != -1 && x2 != -1 && y2 != -1);
    queue<S> que;
    que.push(S(x1, y1, x2, y2));
    bool ans = false;
    while (!que.empty()) {
      S s = que.front();
      que.pop();
      if (memo[s.x1][s.y1][s.x2][s.y2])
        continue;
      memo[s.x1][s.y1][s.x2][s.y2] = true;
      if (grid1[s.y1][s.x1] == '%' && grid2[s.y2][s.x2] == '%') {
        ans = true;
        break;
      }
      if (grid1[s.y1][s.x1] == '%' || grid2[s.y2][s.x2] == '%') {
        continue;
      }
      REP(r, 4) {
        int nx1 = s.x1, ny1 = s.y1, nx2 = s.x2, ny2 = s.y2;
        if (valid1(s.x1 + dx[r], s.y1 + dy[r])) {
          nx1 += dx[r];
          ny1 += dy[r];
        }
        if (valid2(s.x2 - dx[r], s.y2 + dy[r])) {
          nx2 += -dx[r];
          ny2 += dy[r];
        }
        if (!memo[nx1][ny1][nx2][ny2])
          que.push(S(nx1, ny1, nx2, ny2));
      }
    }
    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int W, H;
bool memo[50][50][50][50];
string grid1[50];
string grid2[50];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool valid1(int x, int y) {
  return (x >= 0 && y >= 0 && x < W && y < H && grid1[y][x] != '#');
}
bool valid2(int x, int y) {
  return (x >= 0 && y >= 0 && x < W && y < H && grid2[y][x] != '#');
}
bool dfs(int x1, int y1, int x2, int y2) {
  if (memo[x1][y1][x2][y2])
    return false;
  memo[x1][y1][x2][y2] = true;
  if (grid1[y1][x1] == '%' && grid2[y2][x2] == '%')
    return true;
  REP(r, 4) {
    int nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2;
    if (valid1(x1 + dx[r], y1 + dy[r])) {
      nx1 += dx[r];
      ny1 += dy[r];
    }
    if (valid2(x2 - dx[r], y2 + dy[r])) {
      nx2 += -dx[r];
      ny2 += dy[r];
    }
    if (dfs(nx1, ny1, nx2, ny2))
      return true;
  }
  return false;
}
int main() {
  while (cin >> W >> H && W) {
    REP(i, H) cin >> grid1[i] >> grid2[i];
    memset(memo, 0, sizeof(memo));
    int x1, y1, x2, y2;
    REP(y, H) REP(x, W) {
      if (grid1[y][x] == 'L') {
        x1 = x;
        y1 = y;
      }
      if (grid2[y][x] == 'R') {
        x2 = x;
        y2 = y;
      }
    }
    bool ans = dfs(x1, y1, x2, y2);
    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}