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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
const P dx[4] = {P(0, 0), P(0, 0), P(-1, 1), P(1, -1)};
const P dy[4] = {P(1, 1), P(-1, -1), P(0, 0), P(0, 0)};
bool bmap[55][55][55][55];
char L_cmap[51][51], R_cmap[51][51];
int main() {
  int H, W;
  while (cin >> W >> H && H + W) {

    P Ls, Rs, Lg, Rg;
    rep(i, 55) rep(j, 55) rep(k, 55) rep(l, 55) bmap[i][j][k][l] = false;

    rep(i, H) {
      string s;
      cin >> s;
      rep(j, W) {
        L_cmap[i][j] = s[j];
        if (s[j] == 'L')
          Ls = P(j, i);
        if (s[j] == '%')
          Lg = P(j, i);
      }
      s.clear();
      cin >> s;
      rep(j, W) {
        R_cmap[i][j] = s[j];
        if (s[j] == 'R')
          Rs = P(j, i);
        if (s[j] == '%')
          Rg = P(j, i);
      }
    }

    queue<PP> que;
    que.push(PP(Ls, Rs));
    bmap[Ls.F][Ls.S][Rs.F][Rs.S] = true;
    bool fin = false;
    while (!que.empty()) {
      PP pp = que.front();
      que.pop();
      if (pp.F == Lg && pp.S == Rg) {
        fin = true;
        cout << "Yes" << endl;
        break;
      }
      if (pp.F == Lg || pp.S == Rg)
        continue;

      rep(i, 4) {
        int L_nx = pp.F.F + dx[i].F, L_ny = pp.F.S + dy[i].F;
        int R_nx = pp.S.F + dx[i].S, R_ny = pp.S.S + dy[i].S;
        if (!(0 <= L_nx & L_nx < W && 0 <= L_ny && L_ny < H) ||
            L_cmap[L_ny][L_nx] == '#') {
          L_nx = pp.F.F, L_ny = pp.F.S;
        }
        if (!(0 <= R_nx & R_nx < W && 0 <= R_ny && R_ny < H) ||
            R_cmap[R_ny][R_nx] == '#') {
          R_nx = pp.S.F, R_ny = pp.S.S;
        }
        if (bmap[L_nx][L_ny][R_nx][R_ny])
          continue;
        bmap[L_nx][L_ny][R_nx][R_ny] = true;

        que.push(insert(L_nx, L_ny, R_nx, R_ny));
      }
    }
    if (!fin)
      cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
const P dx[4] = {P(0, 0), P(0, 0), P(-1, 1), P(1, -1)};
const P dy[4] = {P(1, 1), P(-1, -1), P(0, 0), P(0, 0)};
int main() {
  int H, W;
  while (cin >> W >> H && H + W) {
    char L_cmap[H + 1][W + 1], R_cmap[H + 1][W + 1];
    bool bmap[H + 1][W + 1][H + 1][W + 1];
    P Ls, Rs, Lg, Rg;
    rep(i, H) rep(j, W) rep(k, H) rep(l, W) bmap[i][j][k][l] = false;

    rep(i, H) {
      string s;
      cin >> s;
      rep(j, W) {
        L_cmap[i][j] = s[j];
        if (s[j] == 'L')
          Ls = P(j, i);
        if (s[j] == '%')
          Lg = P(j, i);
      }
      cin >> s;
      rep(j, W) {
        R_cmap[i][j] = s[j];
        if (s[j] == 'R')
          Rs = P(j, i);
        if (s[j] == '%')
          Rg = P(j, i);
      }
    }

    queue<PP> que;
    que.push(PP(Ls, Rs));
    bmap[Ls.F][Ls.S][Rs.F][Rs.S] = true;
    bool fin = false;
    while (!que.empty()) {
      PP pp = que.front();
      que.pop();
      if (pp.F == Lg && pp.S == Rg) {
        fin = true;
        cout << "Yes" << endl;
        break;
      }
      if (pp.F == Lg || pp.S == Rg)
        continue;

      rep(i, 4) {
        int L_nx = pp.F.F + dx[i].F, L_ny = pp.F.S + dy[i].F;
        int R_nx = pp.S.F + dx[i].S, R_ny = pp.S.S + dy[i].S;
        if (!(0 <= L_nx & L_nx < W && 0 <= L_ny && L_ny < H) ||
            L_cmap[L_ny][L_nx] == '#') {
          L_nx = pp.F.F, L_ny = pp.F.S;
        }
        if (!(0 <= R_nx & R_nx < W && 0 <= R_ny && R_ny < H) ||
            R_cmap[R_ny][R_nx] == '#') {
          R_nx = pp.S.F, R_ny = pp.S.S;
        }
        if (bmap[L_nx][L_ny][R_nx][R_ny])
          continue;
        bmap[L_nx][L_ny][R_nx][R_ny] = true;
        que.push(insert(L_nx, L_ny, R_nx, R_ny));
      }
    }
    if (!fin)
      cout << "No" << endl;
  }
  return 0;
}