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

#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)
#define RANGE(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i <= i##_end; i++)
#define DSRNG(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i >= i##_end; i--)
#define UNIQUE(v)                                                              \\
  {                                                                            \\
    sort((v).begin(), (v).end());                                              \\
    (v).erase(unique((v).begin(), (v).end()), (v).end());                      \\
  }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")
#define DUMP(q)                                                                \\
  cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q)                                                             \\
  cerr << "[DEBUG] " #q ": [";                                                 \\
  REP(dumpall_i, (q).size()) {                                                 \\
    cerr << q[dumpall_i] << (dumpall_i == (q).size() - 1 ? "" : ", ");         \\
  }                                                                            \\
  cerr << "] at " __FILE__ ":" << __LINE__ << endl

// gcc拡張マクロ
#define gcd __gcd
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using namespace std;

// モジュール

// 処理内容
int main() {

  ll w, h;
  while (cin >> w >> h, w && h) {

    vector<string> rl(h), rr(h);
    REP(i, h) {
      cin >> rl[i] >> rr[i];
      rl[i] = "#" + rl[i] + "#";
      rr[i] = "#" + rr[i] + "#";
    }
    string wall(w + 2, '#');
    rl.insert(rl.begin(), wall);
    rr.insert(rr.begin(), wall);
    rl.emplace_back(wall);
    rr.emplace_back(wall);
    w += 2;
    h += 2;

    ll lsy, lsx, rsy, rsx;
    ll lgy, lgx, rgy, rgx;
    REP(i, h) REP(j, w) {
      if (rl[i][j] == '%') {
        lgy = i;
        lgx = j;
      } else if (rl[i][j] == 'L') {
        lsy = i;
        lsx = j;
      }
      if (rr[i][j] == '%') {
        rgy = i;
        rgx = j;
      } else if (rr[i][j] == 'R') {
        rsy = i;
        rsx = j;
      }
    }

    // {ly, lx, ry, rx}
    deque<tuple<ll, ll, ll, ll>> q;
    vector<vector<vector<vector<bool>>>> visited(
        h, vector<vector<vector<bool>>>(
               w, vector<vector<bool>>(h, vector<bool>(w, false))));
    q.emplace_back(lsy, lsx, rsy, rsx);
    visited[lsy][lsx][rsy][rsx] = true;
    while (!q.empty()) {

      ll ly = get<0>(q.front());
      ll lx = get<1>(q.front());
      ll ry = get<2>(q.front());
      ll rx = get<3>(q.front());
      q.pop_front();
      if (ly == lgy && lx == lgx && ry == rgy && rx == rgx) {
        break;
      }

      // Lin and Ren go upward
      if (ly > 0 && ry > 0) {
        ll ly2 = rl[ly - 1][lx] != '#' ? ly - 1 : ly;
        ll lx2 = lx;
        ll ry2 = rr[ry - 1][rx] != '#' ? ry - 1 : ry;
        ll rx2 = rx;
        bool is_locked =
            (ly2 == lgy && lx2 == lgx) != (ry2 == rgy && rx2 == rgx);
        bool is_blocked = rl[ly2][lx2] == '#' || rr[ry2][rx2] == '#';
        if (!visited[ly2][lx2][ry2][rx2] && !is_locked && !is_blocked) {
          q.emplace_back(ly2, lx2, ry2, rx2);
          visited[ly2][lx2][ry2][rx2] = true;
        }
      }

      // Lin and Ren go downward
      if (ly < h - 1 && ry < h - 1) {
        ll ly2 = rl[ly + 1][lx] != '#' ? ly + 1 : ly;
        ll lx2 = lx;
        ll ry2 = rr[ry + 1][rx] != '#' ? ry + 1 : ry;
        ll rx2 = rx;
        bool is_locked =
            (ly2 == lgy && lx2 == lgx) != (ry2 == rgy && rx2 == rgx);
        bool is_blocked = rl[ly2][lx2] == '#' || rr[ry2][rx2] == '#';
        if (!visited[ly2][lx2][ry2][rx2] && !is_locked && !is_blocked) {
          q.emplace_back(ly2, lx2, ry2, rx2);
          visited[ly2][lx2][ry2][rx2] = true;
        }
      }

      // Lin goes left and Ren goes right
      if (lx > 0 && rx < w - 1) {
        ll ly2 = ly;
        ll lx2 = rl[ly][lx - 1] != '#' ? lx - 1 : lx;
        ll ry2 = ry;
        ll rx2 = rr[ry][rx + 1] != '#' ? rx + 1 : rx;
        bool is_locked =
            (ly2 == lgy && lx2 == lgx) != (ry2 == rgy && rx2 == rgx);
        bool is_blocked = rl[ly2][lx2] == '#' || rr[ry2][rx2] == '#';
        if (!visited[ly2][lx2][ry2][rx2] && !is_locked && !is_blocked) {
          q.emplace_back(ly2, lx2, ry2, rx2);
          visited[ly2][lx2][ry2][rx2] = true;
        }
      }

      // Lin goes right and Ren goes left
      if (lx < w - 1 && rx > 0) {
        ll ly2 = ly;
        ll lx2 = rl[ly][lx + 1] != '#' ? lx + 1 : lx;
        ll ry2 = ry;
        ll rx2 = rr[ry][rx - 1] != '#' ? rx - 1 : rx;
        bool is_locked =
            (ly2 == lgy && lx2 == lgx) != (ry2 == rgy && rx2 == rgx);
        bool is_blocked = rl[ly2][lx2] == '#' || rr[ry2][rx2] == '#';
        if (!visited[ly2][lx2][ry2][rx2] && !is_locked && !is_blocked) {
          q.emplace_back(ly2, lx2, ry2, rx2);
          visited[ly2][lx2][ry2][rx2] = true;
        }
      }
    }

    std::cout << Yes(visited[lgy][lgx][rgy][rgx]) << endl;
  }
}


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)
#define RANGE(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i <= i##_end; i++)
#define DSRNG(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i >= i##_end; i--)
#define UNIQUE(v)                                                              \\
  {                                                                            \\
    sort((v).begin(), (v).end());                                              \\
    (v).erase(unique((v).begin(), (v).end()), (v).end());                      \\
  }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")
#define DUMP(q)                                                                \\
  cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q)                                                             \\
  cerr << "[DEBUG] " #q ": [";                                                 \\
  REP(dumpall_i, (q).size()) {                                                 \\
    cerr << q[dumpall_i] << (dumpall_i == (q).size() - 1 ? "" : ", ");         \\
  }                                                                            \\
  cerr << "] at " __FILE__ ":" << __LINE__ << endl

// gcc拡張マクロ
#define gcd __gcd
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using namespace std;

// モジュール

// 処理内容
int main() {

  ll w, h;
  while (cin >> w >> h, w && h) {

    vector<string> rl(h), rr(h);
    REP(i, h) cin >> rl[i] >> rr[i];
    ll lsy, lsx, rsy, rsx;
    ll lgy, lgx, rgy, rgx;
    REP(i, h) REP(j, w) {
      if (rl[i][j] == '%') {
        lsy = i;
        lsx = j;
      } else if (rl[i][j] == 'L') {
        lgy = i;
        lgx = j;
      }
      if (rr[i][j] == '%') {
        rsy = i;
        rsx = j;
      } else if (rr[i][j] == 'R') {
        rgy = i;
        rgx = j;
      }
    }

    // {ly, lx, ry, rx}
    deque<tuple<ll, ll, ll, ll>> q;
    vector<vector<vector<vector<bool>>>> visited(
        h, vector<vector<vector<bool>>>(
               w, vector<vector<bool>>(h, vector<bool>(w, false))));
    q.emplace_back(lsy, lsx, rsy, rsx);
    visited[lsy][lsx][rsy][rsx] = true;
    while (!q.empty()) {

      ll ly = get<0>(q.front());
      ll lx = get<1>(q.front());
      ll ry = get<2>(q.front());
      ll rx = get<3>(q.front());
      q.pop_front();

      // Lin and Ren go upward
      if (ly > 0 && ry > 0) {
        ll ly2 = rl[ly - 1][lx] != '#' ? ly - 1 : ly;
        ll lx2 = lx;
        ll ry2 = rr[ry - 1][lx] != '#' ? ry - 1 : ry;
        ll rx2 = rx;
        if (!visited[ly2][lx2][ry2][rx2]) {
          q.emplace_back(ly2, lx2, ry2, rx2);
          visited[ly2][lx2][ry2][rx2] = true;
        }
        if (ly < h - 1)
          if (rl[lx][ly + 1] == '#') {
            ll ly2 = ly;
            if (!visited[ly2][lx2][ry2][rx2]) {
              q.emplace_back(ly2, lx2, ry2, rx2);
              visited[ly2][lx2][ry2][rx2] = true;
            }
          }
        if (ry < h - 1)
          if (rr[rx][ry + 1] == '#') {
            ll ry2 = ry;
            if (!visited[ly2][lx2][ry2][rx2]) {
              q.emplace_back(ly2, lx2, ry2, rx2);
              visited[ly2][lx2][ry2][rx2] = true;
            }
          }
      }

      // Lin and Ren go downward
      if (ly < h - 1 && ry < h - 1) {
        ll ly2 = rl[ly + 1][lx] != '#' ? ly + 1 : ly;
        ll lx2 = lx;
        ll ry2 = rr[ry + 1][lx] != '#' ? ry + 1 : ry;
        ll rx2 = rx;
        if (!visited[ly2][lx2][ry2][rx2]) {
          q.emplace_back(ly2, lx2, ry2, rx2);
          visited[ly2][lx2][ry2][rx2] = true;
        }
        if (ly > 0)
          if (rl[lx][ly - 1] == '#') {
            ll ly2 = ly;
            if (!visited[ly2][lx2][ry2][rx2]) {
              q.emplace_back(ly2, lx2, ry2, rx2);
              visited[ly2][lx2][ry2][rx2] = true;
            }
          }
        if (ry > 0)
          if (rr[rx][ry - 1] == '#') {
            ll ry2 = ry;
            if (!visited[ly2][lx2][ry2][rx2]) {
              q.emplace_back(ly2, lx2, ry2, rx2);
              visited[ly2][lx2][ry2][rx2] = true;
            }
          }
      }

      // Lin goes left and Ren goes right
      if (lx > 0 && rx < w - 1) {
        ll ly2 = ly;
        ll lx2 = rl[ly][lx - 1] != '#' ? lx - 1 : lx;
        ll ry2 = ry;
        ll rx2 = rr[ry][rx + 1] != '#' ? rx + 1 : rx;
        if (!visited[ly2][lx2][ry2][rx2]) {
          q.emplace_back(ly2, lx2, ry2, rx2);
          visited[ly2][lx2][ry2][rx2] = true;
        }
        if (lx < w - 1)
          if (rl[ly][lx + 1] == '#') {
            ll lx2 = lx;
            if (!visited[ly2][lx2][ry2][rx2]) {
              q.emplace_back(ly2, lx2, ry2, rx2);
              visited[ly2][lx2][ry2][rx2] = true;
            }
          }
        if (rx > 0)
          if (rr[ry][rx - 1] == '#') {
            ll rx2 = rx;
            if (!visited[ly2][lx2][ry2][rx2]) {
              q.emplace_back(ly2, lx2, ry2, rx2);
              visited[ly2][lx2][ry2][rx2] = true;
            }
          }
      }

      // Lin goes right and Ren goes left
      if (lx < w - 1 && rx > 0) {
        ll ly2 = ly;
        ll lx2 = rl[ly][lx + 1] != '#' ? lx + 1 : lx;
        ll ry2 = ry;
        ll rx2 = rr[ry][rx - 1] != '#' ? rx - 1 : rx;
        if (!visited[ly2][lx2][ry2][rx2]) {
          q.emplace_back(ly2, lx2, ry2, rx2);
          visited[ly2][lx2][ry2][rx2] = true;
        }
        if (lx > 0)
          if (rl[ly][lx - 1] == '#') {
            ll lx2 = lx;
            if (!visited[ly2][lx2][ry2][rx2]) {
              q.emplace_back(ly2, lx2, ry2, rx2);
              visited[ly2][lx2][ry2][rx2] = true;
            }
          }
        if (rx < w - 1)
          if (rr[ry][rx + 1] == '#') {
            ll rx2 = rx;
            if (!visited[ly2][lx2][ry2][rx2]) {
              q.emplace_back(ly2, lx2, ry2, rx2);
              visited[ly2][lx2][ry2][rx2] = true;
            }
          }
      }
    }

    std::cout << Yes(visited[lgy][lgx][rgy][rgx]) << endl;
  }
}