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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int w, h;
int sly, slx, sry, srx;
string s[1000];
string t[1000];
bool f[60][60][60][60];

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

bool is_area(int y, int x) { return y >= 0 && y < h && x >= 0 && x < w; }

bool solve() {
  bool res = false;
  queue<pair<P, P>> que;
  que.push({{sly, slx}, {sry, srx}});
  while (!que.empty()) {
    pair<P, P> pp = que.front();
    que.pop();
    ll ly = pp.first.first, lx = pp.first.second, ry = pp.second.first,
       rx = pp.second.second;
    if (f[ly][lx][ry][rx])
      continue;
    f[ly][lx][ry][rx] = 1;
    rep(i, 4) {
      ll nly = min((ll)h, max((ll)0, ly + dy[i])),
         nry = min((ll)h, max((ll)0, ry + dy[i])),
         nlx = min((ll)w, max((ll)0, lx + dx[i])),
         nrx = min((ll)w, max((ll)0, rx + dx[i]));
      if (is_area(nly, nlx) && is_area(nry, nrx)) {
        if (s[nly][nlx] != t[nry][nrx]) {
          if (s[nly][nlx] == '%' || t[nry][nrx] == '%')
            continue;
          if (s[nly][nlx] == '#') {
            if (!f[ly][lx][nry][nrx]) {
              que.push({{ly, lx}, {nry, nrx}});
            }
          } else if (t[nry][nrx] == '#') {
            if (!f[nly][nlx][ry][rx]) {
              que.push({{nly, nlx}, {ry, rx}});
            }
          }
        } else {
          if (s[nly][nlx] == '%')
            res = true;
          else if (s[nly][nlx] == '.') {
            if (!f[nly][nlx][nry][nrx]) {
              que.push({{nly, nlx}, {nry, nrx}});
            }
          }
        }
      }
    }
  }
  return res;
}

int main() {
  while (cin >> w >> h && w + h) {
    rep(i, 60) rep(j, 60) rep(k, 60) rep(l, 60) { f[i][j][k][l] = 0; }
    rep(i, h) {
      cin >> s[i] >> t[i];
      reverse(ALL(t[i]));
    }
    rep(i, h) {
      rep(j, w) {
        if (s[i][j] == 'L') {
          sly = i, slx = j;
          s[i][j] = '.';
        }
        if (t[i][j] == 'R') {
          sry = i, srx = j;
          t[i][j] = '.';
        }
      }
    }
    if (solve())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int w, h;
P sl, sr;
string s[1000];
string t[1000];
bool f[60][60][60][60];

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

// set<pair<P,P>>

bool solve(int ly, int lx, int ry, int rx) {
  bool res = 0;
  f[ly][lx][ry][rx] = 1;
  REP(i, 4) {
    int nly = ly + dy[i], nry = ry + dy[i], nlx = lx + dx[i], nrx = rx + dx[i];
    if (nly >= 0 && nly < h && nlx >= 0 && nlx < w && nry >= 0 && nry < h &&
        nrx >= 0 && nrx < w) {
      if (f[nly][nrx][nry][nrx])
        continue;
      if (s[nly][nlx] != t[nry][nrx]) {
        if (s[nly][nlx] == '%' || t[nry][nrx] == '%')
          continue;
        if (s[nly][nlx] == '#')
          res |= solve(ly, lx, nry, nrx);
        else if (t[nry][nrx] == '#')
          res |= solve(nly, nlx, ry, rx);
        else
          res |= solve(nly, nlx, nry, nrx);
      } else {
        if (s[nly][nlx] == '%')
          res |= true;
        else if (s[nly][nlx] == '.')
          res |= solve(nly, nlx, nry, nrx);
      }
    }
  }
  return res;
}

int main() {
  while (cin >> w >> h && w + h) {
    REP(i, 60) REP(j, 60) REP(k, 60) REP(l, 60) { f[i][j][k][l] = 0; }
    REP(i, h) {
      cin >> s[i] >> t[i];
      reverse(ALL(t[i]));
    }
    REP(i, h) {
      REP(j, w) {
        if (s[i][j] == 'L') {
          sl = {i, j};
          s[i][j] = '.';
        }
        if (t[i][j] == 'R') {
          sr = {i, j};
          t[i][j] = '.';
        }
      }
    }
    if (solve(sl.first, sl.second, sr.first, sr.second))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}