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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;

int W, H;
string roomL[55], roomR[55];

bool range_check(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

bool visited[55][55][55][55];
bool ok(P len, P rin) {
  memset(visited, 0, sizeof(visited));
  visited[len.fr][len.sc][rin.fr][rin.sc] = true;
  queue<PP> que;
  que.push(PP(len, rin));

  while (que.size()) {
    PP pp = que.front();
    que.pop();
    P next_len = pp.fr;
    P next_rin = pp.sc;

    // printf("[debug] %d %d %d
    // %d\\n",next_len.fr,next_len.sc,next_rin.fr,next_rin.sc);

    rep(i, 4) {
      next_len = pp.fr;
      next_rin = pp.sc;
      next_len.fr += dy[i];
      next_len.sc += dx[i];
      next_rin.fr += dy[i];
      next_rin.sc -= dx[i];

      if (!range_check(next_len.fr, next_len.sc) ||
          roomL[next_len.fr][next_len.sc] == '#') {
        next_len = pp.fr;
      }

      if (!range_check(next_rin.fr, next_rin.sc) ||
          roomR[next_rin.fr][next_rin.sc] == '#') {
        next_rin = pp.sc;
      }

      if ((roomL[next_len.fr][next_len.sc] == '%') &&
          (roomR[next_rin.fr][next_rin.sc] == '%'))
        return true;
      if ((roomL[next_len.fr][next_len.sc] == '%') ||
          (roomR[next_rin.fr][next_rin.sc] == '%'))
        continue;
      if (visited[next_len.fr][next_len.sc][next_rin.fr][next_rin.sc])
        continue;

      visited[next_len.fr][next_len.sc][next_rin.fr][next_rin.sc] = true;
      que.push(PP(next_len, next_rin));
    }
  }

  return false;
}

void solve() {
  rep(y, H) cin >> roomL[y] >> roomR[y];
  P len, rin;
  rep(y, H) rep(x, W) {
    if (roomL[y][x] == 'L')
      len = P(y, x);
    if (roomR[y][x] == 'R')
      rin = P(y, x);
  }
  if (ok(len, rin))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {
  while (cin >> W >> H) {
    if (!W)
      break;
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;

int W, H;
string roomL[55], roomR[55];

bool range_check(int y, int x) {
  if (y < 0 || x < 0)
    return false;
  if (H <= y || W <= x)
    return false;

  return true;
}

bool ok(P len, P rin) {
  bool visited[55][55][55][55] = {};
  visited[len.fr][len.sc][rin.fr][rin.sc] = true;
  queue<PP> que;
  que.push(PP(len, rin));

  while (que.size()) {
    PP pp = que.front();
    que.pop();
    P next_len = pp.fr;
    P next_rin = pp.sc;

    // printf("[debug] %d %d %d
    // %d\\n",next_len.fr,next_len.sc,next_rin.fr,next_rin.sc);

    if (roomL[next_len.fr][next_len.sc] == '%' &&
        roomR[next_rin.fr][next_rin.sc] == '%') {
      return true;
    }
    if (roomL[next_len.fr][next_len.sc] == '%' ||
        roomR[next_rin.fr][next_rin.sc] == '%') {
      visited[next_len.fr][next_len.sc][next_rin.fr][next_rin.sc] = false;
      continue;
    }
    rep(i, 4) {
      next_len = pp.fr;
      next_rin = pp.sc;

      if (range_check(next_len.fr + dy[i], next_len.sc + dx[i]) &&
          roomL[next_len.fr + dy[i]][next_len.sc + dx[i]] != '#') {
        next_len.fr += dy[i];
        next_len.sc += dx[i];
      }
      if (range_check(next_rin.fr + dy[i], next_rin.sc - dx[i]) &&
          roomR[next_rin.fr + dy[i]][next_rin.sc - dx[i]] != '#') {
        next_rin.fr += dy[i];
        next_rin.sc -= dx[i];
      }

      if (!visited[next_len.fr][next_len.sc][next_rin.fr][next_len.sc]) {
        visited[next_len.fr][next_len.sc][next_rin.fr][next_len.sc] = true;
        que.push(PP(next_len, next_rin));
      }
    }
  }

  return false;
}

void solve() {
  rep(y, H) cin >> roomL[y] >> roomR[y];
  P len, rin;
  rep(y, H) rep(x, W) {
    if (roomL[y][x] == 'L')
      len = P(y, x);
    if (roomR[y][x] == 'R')
      rin = P(y, x);
  }
  if (ok(len, rin))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

int main() {
  while (cin >> W >> H) {
    if (!W)
      break;
    solve();
  }
  return 0;
}