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

#include <cstring>
#include <iostream>
#include <queue>

#define F first
#define S second

using namespace std;

struct State {
  int x1, y1, x2, y2;
  State() {}
  State(int a, int b, int c, int d) : x1(a), y1(b), x2(c), y2(d) {}
};

const int MAX = 55;
typedef pair<int, int> P;
char M[2][MAX][MAX];
bool come[MAX][MAX][MAX][MAX];
int W, H;
P s1, s2;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void init() { memset(come, false, sizeof(come)); }

void print() {
  for (int x = 0; x < 2; x++) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cout << M[x][i][j];
      cout << endl;
    }
    cout << endl << endl;
  }
}

void input() {
  for (int i = 0; i < H; i++)
    for (int x = 0; x < 2; x++)
      for (int j = 0; j < W; j++) {
        cin >> M[x][i][j];
        if (M[x][i][j] == 'L' && !x)
          s1 = P(i, j);
        if (M[x][i][j] == 'R' && x)
          s2 = P(i, j);
      }
}

bool isInside(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

bool dfs() {

  queue<State> Q;
  Q.push(State(s1.S, s1.F, s2.S, s2.F));

  while (!Q.empty()) {
    State now = Q.front();
    Q.pop();

    if (come[now.x1][now.y1][now.x2][now.y2])
      continue;
    come[now.x1][now.y1][now.x2][now.y2] = true;

    // cout << now.x1 << " " << now.y1 << " " << now.x2 << " " << now.y2 <<
    // endl;
    for (int i = 0; i < 4; i++) {
      int nx1 = now.x1 + dx[i];
      int ny1 = now.y1 + dy[i];
      int nx2 = now.x2 - dx[i];
      int ny2 = now.y2 + dy[i];
      if (!isInside(nx1, ny1) || M[0][ny1][nx1] == '#') {
        nx1 = now.x1;
        ny1 = now.y1;
      }
      if (!isInside(nx2, ny2) || M[1][ny2][nx2] == '#') {
        nx2 = now.x2;
        ny2 = now.y2;
      }
      if (M[0][ny1][nx1] == '%' && M[1][ny2][nx2] == '%')
        return true;
      if (M[0][ny1][nx1] != '%' && M[1][ny2][nx2] != '%')
        Q.push(State(nx1, ny1, nx2, ny2));
    }
  }
  return false;
}

void solve() {
  if (dfs())
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {
  while (cin >> W >> H && W + H) {
    init();
    input();
    //   print();
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>

#define F first
#define S second

using namespace std;

const int MAX = 55;
typedef pair<int, int> P;
char M[2][MAX][MAX];
bool come[MAX][MAX][MAX][MAX];
int W, H;
P s1, s2;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void init() { memset(come, false, sizeof(come)); }

void print() {
  for (int x = 0; x < 2; x++) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cout << M[x][i][j];
      cout << endl;
    }
    cout << endl << endl;
  }
}

void input() {
  for (int i = 0; i < H; i++)
    for (int x = 0; x < 2; x++)
      for (int j = 0; j < W; j++) {
        cin >> M[x][i][j];
        if (M[x][i][j] == 'L' && !x)
          s1 = P(i, j);
        if (M[x][i][j] == 'R' && x)
          s2 = P(i, j);
      }
}

bool isInside(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }

bool rec(int x1, int y1, int x2, int y2) {

  if (come[x1][y1][x2][y2])
    return false;
  come[x1][y1][x2][y2] = true;
  // cout << x1 <<  " " << y1 << " " << x2 << " " << y2 <<endl;
  if (M[0][y1][x1] == '%' && M[1][y2][x2] == '%')
    return true;
  if (M[0][y1][x1] == '%' || M[1][y2][x2] == '%')
    return false;

  bool tmp = false;

  for (int i = 0; i < 4; i++) {
    int nx1 = x1 + dx[i];
    int ny1 = y1 + dy[i];
    int nx2 = x2 - dx[i];
    int ny2 = y2 + dy[i];
    if (!isInside(nx1, ny1) || M[0][ny1][nx1] == '#') {
      nx1 = x1;
      ny1 = y1;
    }
    if (!isInside(nx2, ny2) || M[1][ny2][nx2] == '#') {
      nx2 = x2;
      ny2 = y2;
    }

    if (!come[nx1][ny1][nx2][ny2])
      tmp = (tmp || rec(nx1, ny1, nx2, ny2));
  }
  return tmp;
}

void solve() {
  if (rec(s1.S, s1.F, s2.S, s2.F))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {
  while (cin >> W >> H && W + H) {
    init();
    input();
    //   print();
    solve();
  }
  return 0;
}