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

#include <iostream>
#include <queue>
using namespace std;

struct S {
  int ly, lx, ry, rx;

  S(int _ly, int _lx, int _ry, int _rx) : ly(_ly), lx(_lx), ry(_ry), rx(_rx){};
};

const int MAX = 52;
const int ldy[4] = {0, 1, 0, -1};
const int ldx[4] = {1, 0, -1, 0};
const int rdy[4] = {0, 1, 0, -1};
const int rdx[4] = {-1, 0, 1, 0};

char l_grid[MAX][MAX];
char r_grid[MAX][MAX];
int visited[MAX][MAX][MAX][MAX];
bool goal;

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;

    if (W == 0 && H == 0) {
      break;
    }

    goal = false;

    for (int i = 0; i < MAX; i++) {
      for (int j = 0; j < MAX; j++) {
        l_grid[i][j] = '#';
        r_grid[i][j] = '#';
      }
    }

    for (int i = 0; i < MAX; i++) {
      for (int j = 0; j < MAX; j++) {
        for (int k = 0; k < MAX; k++) {
          for (int l = 0; l < MAX; l++) {
            visited[i][j][k][l] = false;
          }
        }
      }
    }

    int ly, lx, ry, rx;

    for (int y = 1; y <= H; y++) {
      for (int x = 1; x <= W; x++) {
        cin >> l_grid[y][x];

        if (l_grid[y][x] == 'L') {
          ly = y;
          lx = x;
          l_grid[y][x] = '.';
        }
      }

      for (int x = 1; x <= W; x++) {
        cin >> r_grid[y][x];

        if (r_grid[y][x] == 'R') {
          ry = y;
          rx = x;
          r_grid[y][x] = '.';
        }
      }
    }

    queue<S> q;
    q.push(S(ly, lx, ry, rx));
    visited[ly][lx][ry][rx] = true;

    while (q.empty() == false) {
      int size = q.size();

      for (int i = 0; i < size; i++) {
        S now = q.front();
        q.pop();

        for (int j = 0; j < 4; j++) {
          int nly, nlx, nry, nrx;

          if (l_grid[now.ly + ldy[j]][now.lx + ldx[j]] == '#') {
            nly = now.ly;
            nlx = now.lx;
          } else {
            nly = now.ly + ldy[j];
            nlx = now.lx + ldx[j];
          }

          if (r_grid[now.ry + rdy[j]][now.rx + rdx[j]] == '#') {
            nry = now.ry;
            nrx = now.rx;
          } else {
            nry = now.ry + rdy[j];
            nrx = now.rx + rdx[j];
          }

          if (l_grid[nly][nlx] == '%' && r_grid[nry][nrx] == '%') {
            goal = true;
            goto End;
          } else if (l_grid[nly][nlx] == '%' || r_grid[nry][nrx] == '%') {
            continue;
          }

          if (visited[nly][nlx][nry][nrx] == false) {
            q.push(S(nly, nlx, nry, nrx));
            visited[nly][nlx][nry][nrx] = true;
          }
        }
      }
    }

  End:

    if (goal == true) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

const int MAX = 52;
const int ldy[4] = {0, 1, 0, -1};
const int ldx[4] = {1, 0, -1, 0};
const int rdy[4] = {0, 1, 0, -1};
const int rdx[4] = {-1, 0, 1, 0};

char l_grid[MAX][MAX];
int l_visited[MAX][MAX];
char r_grid[MAX][MAX];
int r_visited[MAX][MAX];
bool goal;

void dfs(int ly, int lx, int ry, int rx, int deep) {
  if (goal == true) {

  } else if (l_grid[ly][lx] == '%' && r_grid[ry][rx] == '%') {
    goal = true;
  } else if (l_grid[ly][lx] == '%' || r_grid[ry][rx] == '%') {

  } else {
    for (int i = 0; i < 4; i++) {
      int nly, nlx, nry, nrx;
      if (l_grid[ly + ldy[i]][lx + ldx[i]] == '#') {
        nly = ly;
        nlx = lx;
      } else {
        nly = ly + ldy[i];
        nlx = lx + ldx[i];
      }
      if (r_grid[ry + rdy[i]][rx + rdx[i]] == '#') {
        nry = ry;
        nrx = rx;
      } else {
        nry = ry + rdy[i];
        nrx = rx + rdx[i];
      }

      if (nly == ly && nlx == lx && nry == ry && nrx == rx) {
        continue;
      }

      if (l_visited[nly][nlx] != -1 && r_visited[nry][nrx] != -1 &&
          l_visited[nly][nlx] == r_visited[nry][nrx]) {
        continue;
      }

      l_visited[ly][lx] = deep + 1;
      r_visited[ry][rx] = deep + 1;

      dfs(nly, nlx, nry, nrx, deep + 1);
    }
  }
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;

    if (W == 0 && H == 0) {
      break;
    }

    goal = false;

    for (int i = 0; i < MAX; i++) {
      for (int j = 0; j < MAX; j++) {
        l_grid[i][j] = '#';
        l_visited[i][j] = -1;
        r_grid[i][j] = '#';
        r_visited[i][j] = -1;
      }
    }

    int ly, lx, ry, rx;

    for (int y = 1; y <= H; y++) {
      for (int x = 1; x <= W; x++) {
        cin >> l_grid[y][x];

        if (l_grid[y][x] == 'L') {
          ly = y;
          lx = x;
          l_grid[y][x] = '.';
        }
      }

      for (int x = 1; x <= W; x++) {
        cin >> r_grid[y][x];

        if (r_grid[y][x] == 'R') {
          ry = y;
          rx = x;
          r_grid[y][x] = '.';
        }
      }
    }

    dfs(ly, lx, ry, rx, 0);

    if (goal == true) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}