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

#include <algorithm>
#include <iostream>
#include <queue>
#define F first
#define S second
using namespace std;

const int N = 50 + 1;
char l[N][N];
char r[N][N];

int r_di[] = {0, 0, -1, +1};
int r_dj[] = {-1, +1, 0, 0};
int l_di[] = {0, 0, -1, +1};
int l_dj[] = {+1, -1, 0, 0};

bool vis[N][N][N][N];

typedef pair<pair<int, int>, pair<int, int>> P;

inline bool &ref(P p) {
  pair<int, int> a = p.first;
  pair<int, int> b = p.second;
  return vis[a.first][a.second][b.first][b.second];
}

int main(void) {
  int h, w;
  while (cin >> w >> h && (h | w)) {
    fill(&vis[0][0][0][0], &vis[N - 1][N - 1][N - 1][N - 1] + 1, false);
    pair<int, int> pr, pl;
    pair<int, int> qr, ql;
    pr = pl = qr = ql = make_pair(-1, -1);
    for (int i = 0; i < h; ++i) {
      string s, t;
      cin >> s >> t;
      for (int j = 0; j < s.size(); ++j) {
        l[i][j] = s[j];
        if (s[j] == 'L')
          pl = make_pair(i, j);
        if (s[j] == '%')
          ql = make_pair(i, j);
      }
      for (int j = 0; j < t.size(); ++j) {
        r[i][j] = t[j];
        if (t[j] == 'R')
          pr = make_pair(i, j);
        if (t[j] == '%')
          qr = make_pair(i, j);
      }
    }
    queue<P> q;
    P ini = make_pair(pl, pr);
    ref(ini) = true;
    for (q.push(ini); q.size(); q.pop()) {
      P p = q.front();
      pair<int, int> pl = p.first;
      pair<int, int> pr = p.second;
      if (l[pl.F][pl.S] == '%' && r[pr.F][pr.S] != '%')
        continue;
      if (r[pr.F][pr.S] == '%' && l[pl.F][pl.S] != '%')
        continue;

      for (int d = 0; d < 4; ++d) {
        int pr_i = pr.first + r_di[d];
        int pr_j = pr.second + r_dj[d];
        int pl_i = pl.first + l_di[d];
        int pl_j = pl.second + l_dj[d];
        if (pr_i < 0 || h <= pr_i)
          pr_i = pr.first, pr_j = pr.second;
        if (pr_j < 0 || w <= pr_j)
          pr_i = pr.first, pr_j = pr.second;
        if (r[pr_i][pr_j] == '#')
          pr_i = pr.first, pr_j = pr.second;
        if (pl_i < 0 || h <= pl_i)
          pl_i = pl.first, pl_j = pl.second;
        if (pl_j < 0 || w <= pl_j)
          pl_i = pl.first, pl_j = pl.second;
        if (l[pl_i][pl_j] == '#')
          pl_i = pl.first, pl_j = pl.second;
        P next = make_pair(make_pair(pl_i, pl_j), make_pair(pr_i, pr_j));
        if (ref(next))
          continue;
        ref(next) = true;
        q.push(next);
      }
    }
    cout << (ref(make_pair(ql, qr)) ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#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 EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(), n.end()
#define pb push_back
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define INF (1 << 27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
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)};
const int idx[4] = {0, 1, 0, -1};
const int idy[4] = {1, 0, -1, 0};

int H, W;
char L_cmap[65][65], R_cmap[65][65];
// stinrg L_cmap[65],R_cmap[65];
bool L_used[65][65], R_used[65][65];
P Ls, Rs, Lg, Rg;
bool fin;
// vector<string> L_cmap,R_cmap;

void dfs(PP pp) {

  // cout << "here" << endl;
  if (fin)
    return;
  if (pp.F == Lg && pp.S == Rg) {
    cout << "Yes" << endl;
    fin = true;
    return;
  }
  // cout << "pp.F = " << pp.F.F << "," << pp.F.S << " pp.S = " << pp.S.F << ","
  // << pp.S.S << endl;
  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;
    // cout << "First next L = " << L_nx << "," << L_ny << " Pre next R = " <<
    // R_nx << "," << R_ny << endl;

    // if(!(0<=L_nx && L_nx <W && 0<= R_nx && R_nx <W && 0<= L_ny && L_ny <H &&
    // 0<= R_ny && R_ny < H))continue;
    if (!(0 <= L_nx))
      L_nx = 0;
    if (!(L_nx < W))
      L_nx = W - 1;
    if (!(0 <= R_nx))
      R_nx = 0;
    if (!(R_nx < W))
      R_nx = W - 1;

    if (!(0 <= L_ny))
      L_ny = 0;
    if (!(L_ny < H))
      L_ny = H - 1;
    if (!(0 <= R_ny))
      R_ny = 0;
    if (!(R_ny < H))
      R_ny = H - 1;

    if (L_cmap[L_ny][L_nx] != '#' && L_used[L_ny][L_nx])
      continue;
    if (R_cmap[R_ny][R_nx] != '#' && R_used[R_ny][R_nx])
      continue;
    if (L_cmap[L_ny][L_nx] == '#' && R_cmap[R_ny][R_nx] == '#')
      continue;

    // cout << "next L = " << L_nx << "," << L_ny << " Pre next R = " << R_nx <<
    // "," << R_ny << endl;
    if (L_cmap[L_ny][L_nx] == '#')
      L_nx = pp.F.F, L_ny = pp.F.S;
    if (R_cmap[R_ny][R_nx] == '#')
      R_nx = pp.S.F, R_ny = pp.S.S;

    R_used[R_ny][R_nx] = L_used[L_ny][L_nx] = true;
    // que.push(insert(L_nx,L_ny,R_nx,R_ny));
    if (fin)
      return;
    dfs(insert(L_nx, L_ny, R_nx, R_ny));
    if (fin)
      return;
    R_used[R_ny][R_nx] = L_used[L_ny][L_nx] = false;
  }
  return;
}

int main() {
  // clock_t start,end;
  // start = clock();
  while (true) {
    cin >> W >> H;
    if (W + H == 0)
      break;
    // L_cmap.clear(),R_cmap.clear();
    fin = false;
    int index = 0;
    rep(i, H) {
      string s;
      cin >> s;
      // if(i == 0 || s != L_cmap[index])
      rep(j, W) {
        L_cmap[i][j] = s[j], L_used[i][j] = false;
        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], R_used[i][j] = false;
        if (s[j] == 'R')
          Rs = P(j, i);
        if (s[j] == '%')
          Rg = P(j, i);
      }
    }

    bool L_ok, R_ok;
    L_ok = R_ok = false;
    queue<P> que;
    que.push(Ls);
    L_used[Ls.S][Ls.F] = true;
    while (!que.empty()) {
      P pi = que.front();
      que.pop();
      if (pi == Lg) {
        L_ok = true;
        break;
      }
      rep(i, 4) {
        int nx = pi.F + idx[i], ny = pi.S + idy[i];
        if (!(0 <= nx && nx < W && 0 <= ny && ny < H) ||
            L_cmap[ny][nx] == '#' || L_used[ny][nx])
          continue;
        L_used[ny][nx] = true;
        que.push(P(nx, ny));
      }
    }
    if (!L_ok) {
      cout << "No" << endl;
      continue;
    }
    queue<P> R_que;
    R_que.push(Rs);
    R_used[Rs.S][Rs.F] = true;
    while (!R_que.empty()) {
      P pi = R_que.front();
      R_que.pop();
      // cout << "pi = " << pi.F << "," << pi.S << endl;
      if (pi == Rg) {
        R_ok = true;
        break;
      }
      rep(i, 4) {
        int nx = pi.F + idx[i], ny = pi.S + idy[i];
        if (!(0 <= nx && nx < W && 0 <= ny && ny < H) ||
            R_cmap[ny][nx] == '#' || R_used[ny][nx])
          continue;
        R_used[ny][nx] = true;
        R_que.push(P(nx, ny));
      }
    }

    if (!L_ok || !R_ok) {
      // cout << L_ok << " " << R_ok << endl;
      cout << "No" << endl;
      continue;
    }

    rep(i, H) rep(j, W) L_used[i][j] = R_used[i][j] = false;
    L_used[Ls.S][Ls.F] = true, R_used[Rs.S][Rs.F] = true;
    dfs(insert(Ls.F, Ls.S, Rs.F, Rs.S));

    if (!fin)
      cout << "No" << endl;
  }
  // end = clock();
  // cout << setiosflags(ios::fixed) << setprecision(10) <<
  // (double)(end-start)/CLOCKS_PER_SEC << endl;
  return 0;
}

// cout<< "Ls = " << Ls.F << " " << Ls.S << ", Lg = " << Lg.F << " " << Lg.S <<
// ", Rs = " << Rs.F << " " << Rs.S << ", Rg = " << Rg.F << " " << Rg.S << endl;
// rep(i,4){
// rep(j,51)rep(k,51)R_used[j][k] = L_used[j][k] = false;
// if(!(0<= Ls.F+dx[i].F && Ls.F+dx[i].F < W && 0<= Ls.S+dy[i].F && Ls.S+dy[i].F
// < H  && 0<= Rs.F+dx[i].S && Rs.F+dx[i].S < W  && 0<= Rs.S+dy[i].S &&
// Rs.S+dy[i].S < H))continue; cout << Ls.F+dx[i].F << " " << Ls.S+dy[i].F<< "
// "<< Rs.F+dx[i].S<< " " << Rs.S+dy[i].S << endl;
// dfs(insert(Ls.F+dx[i].F,Ls.S+dy[i].F,Rs.F+dx[i].S,Rs.S+dy[i].S));
// cout << "----------" << endl;
// }