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

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int GOAL = 1;
const int WAY = 0;
const int WALL = -1;

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

struct state {
  int l_x, l_y, r_x, r_y;
  state(int l_x, int l_y, int r_x, int r_y)
      : l_x(l_x), l_y(l_y), r_x(r_x), r_y(r_y) {}
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  for (int w, h; cin >> w >> h, w;) {
    vector<vector<int>> l_map(h + 2, vector<int>(w + 2, WALL));
    vector<vector<int>> r_map(h + 2, vector<int>(w + 2, WALL));

    int l_sx, l_sy, r_sx, r_sy;
    for (int i = 1; i <= h; ++i) {
      string l, r;
      cin >> l >> r;

      for (int j = 0; j < w; ++j) {
        switch (l[j]) {
        case '%':
          l_map[i][j + 1] = GOAL;
          break;
        case '#':
          l_map[i][j + 1] = WALL;
          break;
        case 'L':
          l_sx = j + 1;
          l_sy = i;
        default:
          l_map[i][j + 1] = WAY;
        }

        switch (r[j]) {
        case '%':
          r_map[i][j + 1] = GOAL;
          break;
        case '#':
          r_map[i][j + 1] = WALL;
          break;
        case 'R':
          r_sx = j + 1;
          r_sy = i;
        default:
          r_map[i][j + 1] = WAY;
        }
      }
    }

    stack<state> sta;
    sta.push(state(l_sx, l_sy, r_sx, r_sy));
    bool reachable = false;
    bool visited[52][52][52][52] = {};

    while (!sta.empty()) {
      const state &s = sta.top();
      int l_x = s.l_x, l_y = s.l_y, r_x = s.r_x, r_y = s.r_y;
      sta.pop();

      visited[l_x][l_y][r_x][r_y] = true;

      if (l_map[l_y][l_x] == GOAL && r_map[r_y][r_x] == GOAL) {
        reachable = true;
        break;
      }

      if (l_map[l_y][l_x] == GOAL || r_map[r_y][r_x] == GOAL)
        continue;

      for (int i = 0; i < 4; ++i) {
        int l_nx = l_x + dx[i], l_ny = l_y + dy[i], r_nx = r_x - dx[i],
            r_ny = r_y + dy[i];
        if (l_map[l_ny][l_nx] == WALL) {
          l_nx = l_x;
          l_ny = l_y;
        }

        if (r_map[r_ny][r_nx] == WALL) {
          r_nx = r_x;
          r_ny = r_y;
        }

        if (!visited[l_nx][l_ny][r_nx][r_ny])
          sta.push(state(l_nx, l_ny, r_nx, r_ny));
      }
    }

    cout << (reachable ? "Yes" : "No") << endl;
  }

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int GOAL = 1;
const int WAY = 0;
const int WALL = -1;

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

int w, h;
bool visited[52][52][52][52];
vector<vector<int>> l_map, r_map;

bool dfs(int l_x, int l_y, int r_x, int r_y) {
  visited[l_x][l_y][r_x][r_y] = true;

  if (l_map[l_y][l_x] == GOAL && r_map[r_y][r_x] == GOAL)
    return true;

  for (int i = 0; i < 4; ++i) {
    int l_nx = l_x + dx[i], l_ny = l_y + dy[i], r_nx = r_x - dx[i],
        r_ny = r_y + dy[i];
    if (l_map[l_ny][l_nx] == WALL) {
      l_nx = l_x;
      l_ny = r_y;
    }

    if (r_map[r_ny][r_nx] == WALL) {
      r_nx = r_x;
      r_ny = r_y;
    }

    if (!visited[l_nx][l_ny][r_nx][r_ny] && dfs(l_nx, l_ny, r_nx, r_ny))
      return true;
  }

  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> w >> h, w) {
    l_map.clear();
    l_map.resize(h + 2, vector<int>(w + 2, WALL));

    r_map.clear();
    r_map.resize(h + 2, vector<int>(w + 2, WALL));

    int l_sx, l_sy, r_sx, r_sy;
    for (int i = 1; i <= h; ++i) {
      string l, r;
      cin >> l >> r;

      for (int j = 0; j < w; ++j) {
        switch (l[j]) {
        case '%':
          l_map[i][j + 1] = GOAL;
          break;
        case '#':
          l_map[i][j + 1] = WALL;
          break;
        case 'L':
          l_sx = j + 1;
          l_sy = i;
        default:
          l_map[i][j + 1] = WAY;
        }

        switch (r[j]) {
        case '%':
          r_map[i][j + 1] = GOAL;
          break;
        case '#':
          r_map[i][j + 1] = WALL;
          break;
        case 'R':
          r_sx = j + 1;
          r_sy = i;
        default:
          r_map[i][j + 1] = WAY;
        }
      }
    }

    memset(visited, false, sizeof(visited));
    cout << (dfs(l_sx, l_sy, r_sx, r_sy) ? "Yes" : "No") << endl;
  }

  return EXIT_SUCCESS;
}