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

#include <bits/stdc++.h>
using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define F first
#define S second

typedef pair<int, int> pii;

vector<string> va, vb;
int H, W;
int dp[55][55][55][55];
int aay[] = {0, 1, 0, -1};
int aax[] = {1, 0, -1, 0};
int bby[] = {0, 1, 0, -1};
int bbx[] = {-1, 0, 1, 0};
queue<pair<pii, pii>> q;
bool check(int a, int b) {
  if (0 <= a && a < H && 0 <= b && b < W)
    return true;
  return false;
}
int dfs(int a, int b, int c, int d) {
  if (dp[a][b][c][d] >= 0)
    return dp[a][b][c][d];
  int ret = 0;
  // cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
  dp[a][b][c][d] = 0;
  if (va[a][b] == '%' && vb[c][d] == '%') {
    return dp[a][b][c][d] = 1;
  }
  if (va[a][b] == '%' || vb[c][d] == '%') {
    return dp[a][b][c][d] = 0;
  }
  rep(i, 4) {
    int aa = a + aay[i];
    int bb = b + aax[i];
    int cc = c + bby[i];
    int dd = d + bbx[i];
    if (!check(aa, bb)) {
      aa = a;
      bb = b;
    }
    if (!check(cc, dd)) {
      cc = c;
      dd = d;
    }
    if (va[aa][bb] == '#') {
      aa = a;
      bb = b;
    }
    if (vb[cc][dd] == '#') {
      cc = c;
      dd = d;
    }
    if (dfs(aa, bb, cc, dd) == 1)
      ret = 1;
  }
  return dp[a][b][c][d] = ret;
}
int main() {
  while (cin >> W >> H) {
    if (W == 0 && H == 0)
      break;
    pii L, R;
    va = vector<string>(H);
    vb = vector<string>(H);
    rep(i, H) cin >> va[i] >> vb[i];
    rep(i, H) {
      rep(j, W) {
        if (va[i][j] == 'L') {
          va[i][j] = '.';
          L = pii(i, j);
        }
        if (vb[i][j] == 'R') {
          vb[i][j] = '.';
          R = pii(i, j);
        }
      }
    }
    rep(i, 55) {
      rep(j, 55) {
        rep(k, 55) {
          rep(l, 55) { dp[i][j][k][l] = -1; }
        }
      }
    }
    bool f = false;
    queue<pair<pii, pii>> q;
    q.push(make_pair(L, R));
    dp[L.F][L.S][R.F][R.S] = 1;
    int a, b, c, d;
    while (q.size()) {
      pair<pii, pii> top = q.front();
      q.pop();
      a = top.F.F;
      b = top.F.S;
      c = top.S.F;
      d = top.S.S;
      if (va[a][b] == '%' && vb[c][d] == '%') {
        f = true;
        continue;
      }
      if (va[a][b] == '%' || vb[c][d] == '%') {
        continue;
      }
      // cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
      rep(i, 4) {
        int aa = a + aay[i];
        int bb = b + aax[i];
        int cc = c + bby[i];
        int dd = d + bbx[i];
        if (!check(aa, bb)) {
          aa = a;
          bb = b;
        }
        if (!check(cc, dd)) {
          cc = c;
          dd = d;
        }
        if (va[aa][bb] == '#') {
          aa = a;
          bb = b;
        }
        if (vb[cc][dd] == '#') {
          cc = c;
          dd = d;
        }
        if (dp[aa][bb][cc][dd] >= 0)
          continue;
        else {
          dp[aa][bb][cc][dd] = 1;
          q.push(make_pair(pii(aa, bb), pii(cc, dd)));
        }
      }
    }
    if (f)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define F first
#define S second

typedef pair<int, int> pii;

vector<string> va, vb;
int H, W;
int dp[55][55][55][55];
int aay[] = {0, 1, 0, -1};
int aax[] = {1, 0, -1, 0};
int bby[] = {0, 1, 0, -1};
int bbx[] = {-1, 0, 1, 0};
bool check(int a, int b) {
  if (0 <= a && a < H && 0 <= b && b < W)
    return true;
  return false;
}
int dfs(int a, int b, int c, int d) {
  if (dp[a][b][c][d] >= 0)
    return dp[a][b][c][d];
  int ret = 0;
  // cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
  dp[a][b][c][d] = 0;
  if (va[a][b] == '%' && vb[c][d] == '%') {
    return dp[a][b][c][d] = 1;
  }
  if (va[a][b] == '%' || vb[c][d] == '%') {
    return dp[a][b][c][d] = 0;
  }
  rep(i, 4) {
    int aa = a + aay[i];
    int bb = b + aax[i];
    int cc = c + bby[i];
    int dd = d + bbx[i];
    if (!check(aa, bb)) {
      aa = a;
      bb = b;
    }
    if (!check(cc, dd)) {
      cc = c;
      dd = d;
    }
    if (va[aa][bb] == '#') {
      aa = a;
      bb = b;
    }
    if (vb[cc][dd] == '#') {
      cc = c;
      dd = d;
    }
    if (dfs(aa, bb, cc, dd) == 1)
      ret = 1;
  }
  return dp[a][b][c][d] = ret;
}
int main() {
  while (cin >> W >> H) {
    if (W == 0 && H == 0)
      break;
    pii L, R;
    va = vector<string>(H);
    vb = vector<string>(H);
    rep(i, H) cin >> va[i] >> vb[i];
    rep(i, H) {
      rep(j, W) {
        if (va[i][j] == 'L') {
          va[i][j] = '.';
          L = pii(i, j);
        }
        if (vb[i][j] == 'R') {
          vb[i][j] = '.';
          R = pii(i, j);
        }
      }
    }
    rep(i, 55) {
      rep(j, 55) {
        rep(k, 55) {
          rep(l, 55) { dp[i][j][k][l] = -1; }
        }
      }
    }
    if (dfs(L.first, L.second, R.first, R.second))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}