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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define fi first
#define sc second
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;

int w, h;
P as, ae, bs, be;
string a[50], b[50];
bool use[50][50][50][50];
int daw[4] = {1, 0, -1, 0}, dah[4] = {0, 1, 0, -1};
int dbw[4] = {-1, 0, 1, 0}, dbh[4] = {0, 1, 0, -1};

void solve(int pp, int qq, int rr, int ss) {
  queue<PP> que;
  que.push(PP(P(pp, qq), P(rr, ss)));
  while (!que.empty()) {
    PP qu = que.front();
    que.pop();
    int p = qu.fi.fi, q = qu.fi.sc, r = qu.sc.fi, s = qu.sc.sc;
    rep(i, 4) {
      int dp = p + dah[i], dq = q + daw[i];
      int dr = r + dbh[i], ds = s + dbw[i];
      if (0 > dp || dp >= h || 0 > dq || dq >= w || a[dp][dq] == '#') {
        dp = p;
        dq = q;
      }
      if (0 > dr || dr >= h || 0 > ds || ds >= w || b[dr][ds] == '#') {
        dr = r;
        ds = s;
      }
      if (use[dp][dq][dr][ds])
        continue;
      if (ae == P(dp, dq) && be != P(dr, ds))
        continue;
      if (ae != P(dp, dq) && be == P(dr, ds))
        continue;
      use[dp][dq][dr][ds] = true;
      que.push(PP(P(dp, dq), P(dr, ds)));
    }
  }
}

int main() {
  while (scanf("%d%d", &w, &h), w || h) {
    memset(use, 0, sizeof(use));
    rep(i, h) cin >> a[i] >> b[i];
    rep(i, h) {
      rep(j, w) {
        if (a[i][j] == 'L')
          as = P(i, j);
        if (b[i][j] == 'R')
          bs = P(i, j);
        if (a[i][j] == '%')
          ae = P(i, j);
        if (b[i][j] == '%')
          be = P(i, j);
      }
    }
    int d = 0;
    use[as.fi][as.sc][bs.fi][bs.sc] = true;
    solve(as.fi, as.sc, bs.fi, bs.sc);
    if (use[ae.fi][ae.sc][be.fi][be.sc])
      puts("Yes");
    else
      puts("No");
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define fi first
#define sc second
using namespace std;
typedef pair<int, int> P;

int w, h;
P as, ae, bs, be;
string a[50], b[50];
bool use[50][50][50][50];
int daw[4] = {1, 0, -1, 0}, dah[4] = {0, 1, 0, -1};
int dbw[4] = {-1, 0, 1, 0}, dbh[4] = {0, 1, 0, -1};

void dfs(int p, int q, int r, int s) {
  rep(i, 4) {
    int dp = p + dah[i], dq = q + daw[i];
    int dr = r + dbh[i], ds = s + dbw[i];
    if (0 > dp || dp >= h || 0 > dq || dq >= w || a[dp][dq] == '#') {
      dp = p;
      dq = q;
    }
    if (0 > dr || dr >= h || 0 > ds || ds >= w || b[dr][ds] == '#') {
      dr = r;
      ds = s;
    }
    if (use[dp][dq][dr][ds])
      continue;
    if (ae == P(dp, dq) && be != P(dr, ds))
      continue;
    if (ae != P(dp, dq) && be == P(dr, ds))
      continue;
    use[dp][dq][dr][ds] = true;
    dfs(dp, dq, dr, ds);
  }
}

int main() {
  while (scanf("%d%d", &w, &h), w || h) {
    memset(use, 0, sizeof(use));
    rep(i, h) cin >> a[i] >> b[i];
    rep(i, h) {
      rep(j, w) {
        if (a[i][j] == 'L')
          as = P(i, j);
        if (b[i][j] == 'R')
          bs = P(i, j);
        if (a[i][j] == '%')
          ae = P(i, j);
        if (b[i][j] == '%')
          be = P(i, j);
      }
    }
    use[as.fi][as.sc][bs.fi][bs.sc] = true;
    dfs(as.fi, as.sc, bs.fi, bs.sc);
    if (use[ae.fi][ae.sc][be.fi][be.sc])
      puts("Yes");
    else
      puts("No");
  }
}