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

// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

const int S = 51;
// ax, ay, bx, by
bool visited[S][S][S][S];
int W, H;

struct Elem {
  int ax, ay, bx, by;
};

pii search_p(const vector<string> &vs, char target) {
  rep(i, 0, H) rep(j, 0, W) {
    if (vs[i][j] == target)
      return make_pair(i, j);
  }
  return make_pair(-1, -1);
}

void move_p(const vector<string> &vs, int &x, int &y, int dir, int mode) {
  int nx = max(0LL, min(H - 1, x + dx[dir]));
  int ny = max(0LL, min(W - 1, y + dy[dir] * mode));
  if (vs[nx][ny] == '#')
    return;
  x = nx;
  y = ny;
}

signed main() {
  while (cin >> W >> H, W || H) {
    memset(visited, false, sizeof(visited));

    vector<string> lboard(H), rboard(H);
    rep(i, 0, H) cin >> lboard[i] >> rboard[i];
    pii init_a = search_p(lboard, 'L');
    pii init_b = search_p(rboard, 'R');
    int a = init_a.first, b = init_a.second;
    int x = init_b.first, y = init_b.second;

    pii goal_a = search_p(lboard, '%');
    pii goal_b = search_p(rboard, '%');
    int A = goal_a.first, B = goal_a.second;
    int X = goal_b.first, Y = goal_b.second;

    queue<Elem> que;
    que.push(Elem{a, b, x, y});
    visited[a][b][x][y] = true;
    while (!que.empty()) {
      Elem cur = que.front();
      que.pop();
      rep(k, 0, 4) {
        Elem nxt = cur;
        move_p(lboard, nxt.ax, nxt.ay, k, -1);
        move_p(rboard, nxt.bx, nxt.by, k, 1);
        if (visited[nxt.ax][nxt.ay][nxt.bx][nxt.by])
          continue;
        bool p = make_pair(nxt.ax, nxt.ay) == goal_a;
        bool q = make_pair(nxt.bx, nxt.by) == goal_b;
        if (p ^ q)
          continue;
        visited[nxt.ax][nxt.ay][nxt.bx][nxt.by] = true;
        que.push(nxt);
      }
    }

    if (!visited[A][B][X][Y])
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[2][4] = {{0, 0, 1, -1}, {0, 0, 1, -1}};
int dy[2][4] = {{1, -1, 0, 0}, {-1, 1, 0, 0}};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

const int S = 51;
// ax, ay, bx, by
int step[S][S][S][S];
bool visited[S][S][S][S];
int W, H;

struct Elem {
  int ax, ay, bx, by;
};

pii search_p(vector<string> &vs, char target) {
  rep(i, 0, H) rep(j, 0, W) {
    if (vs[i][j] == target)
      return make_pair(i, j);
  }
}

void move_p(vector<string> &vs, int &x, int &y, int dir, int mode) {
  int nx = x + dx[mode][dir];
  int ny = y + dy[mode][dir];
  if (nx < 0 || nx >= H || ny < 0 || ny >= W)
    return;
  if (vs[nx][ny] == '#')
    return;
  x = nx;
  y = ny;
}

signed main() {
  while (cin >> W >> H, W || H) {
    rep(i, 0, S) rep(j, 0, S) rep(k, 0, S) rep(l, 0, S) step[i][j][k][l] = INF;
    memset(visited, false, sizeof(visited));

    vector<string> lboard(H), rboard(W);
    rep(i, 0, H) cin >> lboard[i] >> rboard[i];
    pii init_a = search_p(lboard, 'L');
    pii init_b = search_p(rboard, 'R');
    int a = init_a.first, b = init_a.second;
    int x = init_b.first, y = init_b.second;
    step[a][b][x][y] = 0;

    queue<Elem> que;
    que.push(Elem{a, b, x, y});
    visited[a][b][x][y] = true;
    while (!que.empty()) {
      Elem cur = que.front();
      que.pop();
      rep(k, 0, 4) {
        Elem nxt = cur;
        move_p(lboard, nxt.ax, nxt.ay, k, 0);
        move_p(rboard, nxt.bx, nxt.by, k, 1);
        if (visited[nxt.ax][nxt.ay][nxt.bx][nxt.by])
          continue;
        if (step[nxt.ax][nxt.ay][nxt.bx][nxt.by] >
            step[cur.ax][cur.ay][cur.bx][cur.by] + 1) {
          step[nxt.ax][nxt.ay][nxt.bx][nxt.by] =
              step[cur.ax][cur.ay][cur.bx][cur.by] + 1;
          que.push(nxt);
        }
      }
    }

    pii goal_a = search_p(lboard, '%');
    pii goal_b = search_p(rboard, '%');
    a = goal_a.first, b = goal_a.second;
    x = goal_b.first, y = goal_b.second;
    if (step[a][b][x][y] == INF)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}