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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

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

template <class T, class U, class V>
inline T clamp(const T &x, const U &lo, const V &hi) {
  return x < lo ? lo : x > hi ? hi : x;
}

bool solve(int W, int H, const vector<string> &RoomL,
           const vector<string> &RoomR) {
  int sxL, syL, sxR, syR;
  rep(i, H) rep(j, W) {
    if (RoomL[i][j] == 'L')
      sxL = i, syL = j;
    if (RoomR[i][j] == 'R')
      sxR = i, syR = j;
  }
  bool vis[H][W][H][W];
  memset(vis, 0, W * H * W * H);
  vis[sxL][syL][sxR][syR] = true;
  queue<tuple<int, int, int, int>> Q;
  for (Q.emplace(sxL, syL, sxR, syR); !Q.empty();) {
    int xL, yL, xR, yR;
    tie(xL, yL, xR, yR) = Q.front();
    Q.pop();
    rep(k, 4) {
      int nxL = clamp(xL + dx[k], 0, H - 1), nyL = clamp(yL + dy[k], 0, W - 1),
          nxR = clamp(xR + dx[k], 0, H - 1), nyR = clamp(yR - dy[k], 0, W - 1);
      if (RoomL[nxL][nyL] == '#' and RoomR[nxR][nyR] == '#')
        continue;
      if (RoomL[nxL][nyL] == '#')
        nxL -= dx[k], nyL -= dy[k];
      if (RoomR[nxR][nyR] == '#')
        nxR -= dx[k], nyR += dy[k];
      if (RoomL[nxL][nyL] == '%' and RoomR[nxR][nyR] == '%')
        return true;
      if (RoomL[nxL][nyL] == '%' or RoomR[nxR][nyR] == '%' or
          vis[nxL][nyL][nxR][nyR])
        continue;
      vis[nxL][nyL][nxR][nyR] = true;
      Q.emplace(nxL, nyL, nxR, nyR);
    }
  }
  return false;
}

int main() {
  for (int W, H; cin >> W >> H, W | H;) {
    vector<string> RoomL(H), RoomR(H);
    rep(i, H) cin >> RoomL[i] >> RoomR[i];
    cout << (solve(W, H, RoomL, RoomR) ? "Yes" : "No") << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

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

template <class T, class U, class V>
inline T clamp(const T &x, const U &lo, const V &hi) {
  return x < lo ? lo : x > hi ? hi : x;
}

bool solve(int W, int H, const vector<string> &RoomL,
           const vector<string> &RoomR) {
  int sxL, syL, sxR, syR;
  rep(i, H) rep(j, W) {
    if (RoomL[i][j] == 'L')
      sxL = i, syL = j;
    if (RoomR[i][j] == 'R')
      sxR = i, syR = j;
  }
  bool vis[W][H][W][H];
  memset(vis, 0, W * H * W * H);
  vis[sxL][syL][sxR][syR] = true;
  queue<tuple<int, int, int, int>> Q;
  for (Q.emplace(sxL, syL, sxR, syR); !Q.empty();) {
    int xL, yL, xR, yR;
    tie(xL, yL, xR, yR) = Q.front();
    Q.pop();
    rep(k, 4) {
      int nxL = clamp(xL + dx[k], 0, H - 1), nyL = clamp(yL + dy[k], 0, W - 1),
          nxR = clamp(xR + dx[k], 0, H - 1), nyR = clamp(yR - dy[k], 0, W - 1);
      if (RoomL[nxL][nyL] == '#' and RoomR[nxR][nyR] == '#')
        continue;
      if (RoomL[nxL][nyL] == '#')
        nxL -= dx[k], nyL -= dy[k];
      if (RoomR[nxR][nyR] == '#')
        nxR -= dx[k], nyR += dy[k];
      if (RoomL[nxL][nyL] == '%' and RoomR[nxR][nyR] == '%')
        return true;
      if (RoomL[nxL][nyL] == '%' or RoomR[nxR][nyR] == '%' or
          vis[nxL][nyL][nxR][nyR])
        continue;
      vis[nxL][nyL][nxR][nyR] = true;
      Q.emplace(nxL, nyL, nxR, nyR);
    }
  }
  return false;
}

int main() {
  for (int W, H; cin >> W >> H, W | H;) {
    vector<string> RoomL(H), RoomR(H);
    rep(i, H) cin >> RoomL[i] >> RoomR[i];
    cout << (solve(W, H, RoomL, RoomR) ? "Yes" : "No") << endl;
  }
}