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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<string> vs;
Def inf = sizeof(Def) == sizeof(ll) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int d[52][52][52][52];
typedef tuple<int, int, int, int> tp;
vs a, b;
int n, m;
int f(int x1, int y1, int x2, int y2) {
  //    cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;

  priority_queue<tp> q;
  q.push(tp{x1, y1, x2, y2});
  while (!q.empty()) {
    int x1, y1, x2, y2;
    tie(x1, y1, x2, y2) = q.top();
    q.pop();
    rep(i, 4) {

      int nx1 = x1 + dx[i];
      int ny1 = y1 + dy[i];
      int nx2 = x2 + dx[i];
      int ny2 = y2 + dy[(2 + i) % 4];
      if (nx1 < 0 || ny1 < 0 || nx1 >= n || ny1 >= m) {
        nx1 -= dx[i];
        ny1 -= dy[(i) % 4];
      }
      if (nx2 < 0 || ny2 < 0 || nx2 >= n || ny2 >= m) {
        nx2 -= dx[i];
        ny2 -= dy[(i + 2) % 4];
      }
      if (a[nx1][ny1] == '#') {
        nx1 -= dx[i];
        ny1 -= dy[i];
      }
      if (b[nx2][ny2] == '#') {
        nx2 -= dx[i];
        ny2 -= dy[(i + 2) % 4];
      }
      bool A = a[nx1][ny1] == '%';
      bool B = b[nx2][ny2] == '%';
      if (A != B)
        continue;
      if (d[nx1][ny1][nx2][ny2])
        continue;
      // cout<<nx1<<" "<<ny1<<" "<<nx2<<" "<<ny2<<endl;
      d[nx1][ny1][nx2][ny2] = 1;
      q.push(tp{nx1, ny1, nx2, ny2}); // f(nx1,ny1,nx2,ny2);
    }
  }
}
int main() {
  while (cin >> m >> n, n) {
    rep(i, 52) rep(j, 52) rep(k, 52) rep(l, 52) d[i][j][k][l] = 0;
    a = b = vs(n);
    rep(i, n) cin >> a[i] >> b[i];
    rep(i, n) rep(j, m) rep(k, n)
        rep(l, m) if (a[i][j] == 'L' && b[k][l] == 'R') {
      d[i][j][k][l] = 1;
      f(i, j, k, l);
    }
    rep(i, n) rep(j, m) rep(k, n)
        rep(l, m) if (a[i][j] == '%' && b[k][l] == '%') {
      if (d[i][j][k][l])
        cout << "Yes" << endl;
      else
        cout << "No" << endl;
    }
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<string> vs;
Def inf = sizeof(Def) == sizeof(ll) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int d[52][52][52][52];
vs a, b;
int n, m;
int f(int x1, int y1, int x2, int y2) {
  //    cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;
  rep(i, 4) {
    int nx1 = x1 + dx[i];
    int ny1 = y1 + dy[i];
    int nx2 = x2 + dx[i];
    int ny2 = y2 + dy[(2 + i) % 4];
    if (nx1 < 0 || ny1 < 0 || nx1 >= n || ny1 >= m) {
      nx1 -= dx[i];
      ny1 -= dy[(i) % 4];
    }
    if (nx2 < 0 || ny2 < 0 || nx2 >= n || ny2 >= m) {
      nx2 -= dx[i];
      ny2 -= dy[(i + 2) % 4];
    }
    if (a[nx1][ny1] == '#') {
      nx1 -= dx[i];
      ny1 -= dy[i];
    }
    if (b[nx2][ny2] == '#') {
      nx2 -= dx[i];
      ny2 -= dy[(i + 2) % 4];
    }
    if (d[nx1][ny1][nx2][ny2])
      continue;
    d[nx1][ny1][nx2][ny2] = 1;
    f(nx1, ny1, nx2, ny2);
  }
}
int main() {
  while (cin >> m >> n, n) {
    rep(i, 52) rep(j, 52) rep(k, 52) rep(l, 52) d[i][j][k][l] = 0;
    a = b = vs(n);
    rep(i, n) cin >> a[i] >> b[i];
    rep(i, n) rep(j, m) rep(k, n)
        rep(l, m) if (a[i][j] == 'L' && b[k][l] == 'R') {
      d[i][j][k][l] = 1;
      f(i, j, k, l);
    }
    rep(i, n) rep(j, m) rep(k, n)
        rep(l, m) if (a[i][j] == '%' && b[k][l] == '%') {
      if (d[i][j][k][l])
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    }
  }
}