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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct info {
  int lx, ly, rx, ry;
  info(int lx, int ly, int rx, int ry) : lx(lx), ly(ly), rx(rx), ry(ry) {}
};

int main() {
  while (1) {
    int w, h;
    cin >> w >> h;
    if (w == 0)
      break;

    vector<vector<char>> l(h + 2, vector<char>(w + 2, '#'));
    vector<vector<char>> r(h + 2, vector<char>(w + 2, '#'));
    vector<vector<bool>> used(h * w, vector<bool>(h * w, false));
    int lx, ly, rx, ry;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> l[i][j];
        if (l[i][j] == 'L') {
          lx = j;
          ly = i;
        }
      }
      for (int j = 1; j <= w; j++) {
        cin >> r[i][j];
        if (r[i][j] == 'R') {
          rx = j;
          ry = i;
        }
      }
    }

    queue<info> search;
    search.push(info(lx, ly, rx, ry));
    bool success = false;
    while (!search.empty()) {
      int lx = search.front().lx;
      int ly = search.front().ly;
      int rx = search.front().rx;
      int ry = search.front().ry;
      search.pop();
      if (used[(ly - 1) * w + (lx - 1)][(ry - 1) * w + (rx - 1)])
        continue;
      used[(ly - 1) * w + (lx - 1)][(ry - 1) * w + (rx - 1)] = true;
      if (l[ly][lx] == '%') {
        if (r[ry][rx] == '%') {
          success = true;
          break;
        } else {
          continue;
        }
      } else if (r[ry][rx] == '%') {
        continue;
      }

      for (int i = 0; i < 4; i++) {
        int nlx = lx + dx[i], nly = ly + dy[i];
        int nrx, nry;
        if (i % 2 == 0) {
          nrx = rx + dx[(i + 2) % 4];
          nry = ry + dy[(i + 2) % 4];
        } else {
          nrx = rx + dx[i];
          nry = ry + dy[i];
        }
        int uc = 0;
        if (l[nly][nlx] == '#') {
          nlx = lx;
          nly = ly;
          uc++;
        }
        if (r[nry][nrx] == '#') {
          nrx = rx;
          nry = ry;
          uc++;
        }
        if (uc == 2)
          continue;
        search.push(info(nlx, nly, nrx, nry));
      }
    }

    if (success) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

vector<vector<char>> l;
vector<vector<char>> r;
vector<vector<bool>> u;
int w, h;

bool dfs(int lx, int ly, int rx, int ry) {
  // cout << lx << " " << ly << " " << rx << " " << ry << endl;
  if (u[(ly - 1) * w + (lx - 1)][(ry - 1) * w + (rx - 1)])
    return false;
  u[(ly - 1) * w + (lx - 1)][(ry - 1) * w + (rx - 1)] = true;
  if (l[ly][lx] == '%' && r[ry][rx] == '%')
    return true;
  for (int i = 0; i < 4; i++) {
    int nlx = lx + dx[i], nly = ly + dy[i];
    int nrx, nry;
    if (i % 2 == 0) {
      nrx = rx + dx[(i + 2) % 4];
      nry = ry + dy[(i + 2) % 4];
    } else {
      nrx = rx + dx[i];
      nry = ry + dy[i];
    }
    if (l[nly][nlx] == '#') {
      nlx = lx;
      nly = ly;
    }
    if (r[nry][nrx] == '#') {
      nrx = rx;
      nry = ry;
    }
    if (dfs(nlx, nly, nrx, nry)) {
      return true;
    }
  }
  return false;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0)
      break;

    vector<vector<char>> left(h + 2, vector<char>(w + 2, '#'));
    vector<vector<char>> right(h + 2, vector<char>(w + 2, '#'));
    vector<vector<bool>> used(h * w, vector<bool>(h * w, false));
    int lx, ly, rx, ry;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> left[i][j];
        if (left[i][j] == 'L') {
          lx = j;
          ly = i;
        }
      }
      for (int j = 1; j <= w; j++) {
        cin >> right[i][j];
        if (right[i][j] == 'R') {
          rx = j;
          ry = i;
        }
      }
    }
    l = left;
    r = right;
    u = used;

    if (dfs(lx, ly, rx, ry)) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}