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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using lint = long long;
using ldouble = long double;

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

bool solve() {
  int H, W;
  cin >> W >> H;
  if (W == 0)
    return false;

  vector<string> L(H), R(H);
  for (int x = 0; x < H; ++x) {
    cin >> L[x] >> R[x];
    reverse(R[x].begin(), R[x].end());
  }

  int lsx, lsy, rsx, rsy;
  for (int x = 0; x < H; ++x) {
    for (int y = 0; y < W; ++y) {
      if (L[x][y] == 'L')
        lsx = x, lsy = y;
      if (R[x][y] == 'R')
        rsx = x, rsy = y;
    }
  }

  vector<vector<vector<vector<bool>>>> visited(H);
  for (auto &v1 : visited) {
    v1.resize(W);
    for (auto &v2 : v1) {
      v2.resize(H);
      for (auto &v3 : v2) {
        v3.assign(W, false);
      }
    }
  }

  queue<tuple<int, int, int, int>> que;
  que.emplace(lsx, lsy, rsx, rsy);
  visited[lsx][lsy][rsx][rsy] = true;

  bool judge = false;
  while (!que.empty()) {
    int lx, ly, rx, ry;
    tie(lx, ly, rx, ry) = que.front();
    que.pop();

    bool l = (L[lx][ly] == '%');
    bool r = (R[rx][ry] == '%');
    if (l && r) {
      judge = true;
      break;
    }
    if (l || r)
      continue;

    for (int i = 0; i < 4; ++i) {
      int lnx = lx + dx[i];
      int lny = ly + dy[i];
      if (lnx < 0 || H <= lnx || lny < 0 || W <= lny || L[lnx][lny] == '#')
        lnx = lx, lny = ly;

      int rnx = rx + dx[i];
      int rny = ry + dy[i];
      if (rnx < 0 || H <= rnx || rny < 0 || W <= rny || R[rnx][rny] == '#')
        rnx = rx, rny = ry;

      if (visited[lnx][lny][rnx][rny])
        continue;
      visited[lnx][lny][rnx][rny] = true;
      que.emplace(lnx, lny, rnx, rny);
    }
  }

  cout << (judge ? "Yes" : "No") << endl;
  return true;
}

int main() {
  while (solve()) {
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

using lint = long long;
using ldouble = long double;

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

int H, W;
vector<string> L, R;
vector<vector<vector<vector<int>>>> dp;
// -1 : 未探索
// 0  : 到達不可
// 1  : 到達可
// 2  : 探索中

bool dfs(int lx, int ly, int rx, int ry) {
  auto &ret = dp[lx][ly][rx][ry];
  if (ret >= 0)
    return ret == 1;

  bool l = (L[lx][ly] == '%');
  bool r = (R[rx][ry] == '%');
  if (l && r)
    return (ret = 1) == 1;
  if (l || r)
    return (ret = 0) == 1;

  ret = 2;
  for (int i = 0; i < 4; ++i) {
    int lnx = lx + dx[i];
    int lny = ly + dy[i];
    if (lnx < 0 || H <= lnx || lny < 0 || W <= lny || L[lnx][lny] == '#')
      lnx = lx, lny = ly;

    int rnx = rx + dx[i];
    int rny = ry + dy[i];
    if (rnx < 0 || H <= rnx || rny < 0 || W <= rny || R[rnx][rny] == '#')
      rnx = rx, rny = ry;

    if (dfs(lnx, lny, rnx, rny))
      return (ret = 1) == 1;
  }
  return (ret = 0) == 1;
}

bool solve() {
  cin >> W >> H;
  if (W == 0)
    return false;

  L.resize(H);
  R.resize(H);
  for (int x = 0; x < H; ++x) {
    cin >> L[x] >> R[x];
    reverse(R[x].begin(), R[x].end());
  }

  int lsx, lsy, rsx, rsy;
  for (int x = 0; x < H; ++x) {
    for (int y = 0; y < W; ++y) {
      if (L[x][y] == 'L')
        lsx = x, lsy = y;
      if (R[x][y] == 'R')
        rsx = x, rsy = y;
    }
  }

  dp.resize(H);
  for (auto &v1 : dp) {
    v1.resize(W);
    for (auto &v2 : v1) {
      v2.resize(H);
      for (auto &v3 : v2) {
        v3.assign(W, -1);
      }
    }
  }

  cout << (dfs(lsx, lsy, rsx, rsy) ? "Yes" : "No") << endl;
  return true;
}

int main() {
  while (solve()) {
  }
  return 0;
}