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

#include <bits/stdc++.h>
using namespace std;
int ans;
int h, w;
string ma1[55];
string ma2[55];
int dp[55][55][55][55];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
struct node {
  int a, b, c, d;
};
void bfs(int x1, int y1, int x2, int y2) {
  node n = (node){x1, y1, x2, y2};
  queue<node> q;
  q.push(n);
  while (!q.empty()) {
    node t = q.front();
    q.pop();
    int lx = t.a;
    int ly = t.b;
    int rx = t.c;
    int ry = t.d;
    if (ans)
      continue;
    if (dp[lx][ly][rx][ry])
      continue;
    if (ma1[lx][ly] == '%' && ma2[rx][ry] == '%') {
      ans = 1;
      return;
    }
    if (ma1[lx][ly] == '%' || ma2[rx][ry] == '%') {
      continue;
    }
    dp[lx][ly][rx][ry] = 1;
    for (int i = 0; i < 4; i++) {
      int nlx = lx + dx[i];
      int nly = ly + dy[i];
      int nrx = rx + dx[i];
      int nry = ry - dy[i];
      if (nlx == h || nlx < 0)
        nlx = lx;
      if (nly == w || nly < 0)
        nly = ly;
      if (ma1[nlx][nly] == '#')
        nlx = lx, nly = ly;
      if (nrx == h || nrx < 0)
        nrx = rx;
      if (nry == w || nry < 0)
        nry = ry;
      if (ma2[nrx][nry] == '#')
        nrx = rx, nry = ry;
      if (dp[nlx][nly][nrx][nry] == 0) {
        node d = (node){nlx, nly, nrx, nry};
        q.push(d);
      }
    }
  }
  return;
}

int main() {
  while (cin >> w >> h, w + h) {
    ans = 0;
    memset(dp, 0, sizeof(dp));
    int sl[2], sr[2];
    for (int i = 0; i < h; i++) {
      cin >> ma1[i] >> ma2[i];
      for (int j = 0; j < w; j++) {
        if (ma1[i][j] == 'L')
          sl[0] = i, sl[1] = j;
        if (ma2[i][j] == 'R')
          sr[0] = i, sr[1] = j;
      }
    }
    bfs(sl[0], sl[1], sr[0], sr[1]);
    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int ans;
int h, w;
string ma1[55];
string ma2[55];
int dp[55][55][55][55];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void dfs(int lx, int ly, int rx, int ry) {
  if (ans)
    return;
  // cout<<lx<<" "<<ly<<" "<<rx<<" "<<ry<<endl;
  if (dp[lx][ly][rx][ry])
    return;
  if (ma1[lx][ly] == '%' && ma2[rx][ry] == '%') {
    ans = 1;
    return;
  }
  dp[lx][ly][rx][ry] = 1;
  for (int i = 0; i < 4; i++) {
    int nlx = lx + dx[i];
    int nly = ly + dy[i];
    int nrx = rx + dx[i];
    int nry = ry - dy[i];
    if (nlx == h || nlx < 0)
      nlx = lx;
    if (nly == w || nly < 0)
      nly = ly;
    if (ma1[nlx][nly] == '#')
      nlx = lx, nly = ly;
    if (nrx == h || nrx < 0)
      nrx = rx;
    if (nry == w || nry < 0)
      nry = ry;
    if (ma2[nrx][nry] == '#')
      nrx = rx, nry = ry;
    if (dp[nlx][nly][nrx][nry] == 0)
      dfs(nlx, nly, nrx, nry);
  }
  return;
}

int main() {
  while (cin >> w >> h, w + h) {
    ans = 0;
    memset(dp, 0, sizeof(dp));
    int sl[2], sr[2];
    for (int i = 0; i < h; i++) {
      cin >> ma1[i] >> ma2[i];
      for (int j = 0; j < w; j++) {
        if (ma1[i][j] == 'L')
          sl[0] = i, sl[1] = j;
        if (ma2[i][j] == 'R')
          sr[0] = i, sr[1] = j;
      }
    }
    dfs(sl[0], sl[1], sr[0], sr[1]);
    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}