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

#include <bits/stdc++.h>
using namespace std;

int W, H;
char fie[2][55][55];

bool used[50][50][50][50];
int dx[2][4] = {{0, 1, 0, -1}, {0, -1, 0, 1}};
int dy[] = {1, 0, -1, 0};

void move(int &x, int &y, int id, int rl) {
  int nx = x + dx[rl][id];
  int ny = y + dy[id];
  if (nx < 0 || ny < 0 || nx >= W || ny >= H)
    return;
  if (fie[rl][nx][ny] == '#')
    return;
  x = nx;
  y = ny;
}

struct state {
  int lx, ly, rx, ry;
};

bool solve(int lx, int ly, int rx, int ry) {
  queue<state> q;
  q.push((state){lx, ly, rx, ry});
  used[lx][ly][rx][ry] = true;
  while (!q.empty()) {
    state p = q.front();
    q.pop();
    lx = p.lx;
    ly = p.ly;
    rx = p.rx;
    ry = p.ry;
    if (fie[0][lx][ly] == '%' && fie[1][rx][ry] == '%')
      return true;
    if (fie[0][lx][ly] == '%' || fie[1][rx][ry] == '%')
      continue;
    for (int i = 0; i < 4; i++) {
      int nlx = lx, nly = ly, nrx = rx, nry = ry;
      move(nlx, nly, i, 0);
      move(nrx, nry, i, 1);
      if (used[nlx][nly][nrx][nry])
        continue;
      q.push((state){nlx, nly, nrx, nry});
      used[nlx][nly][nrx][nry] = true;
    }
  }
  return false;
}

int main() {
  while (cin >> W >> H && (W || H)) {
    int lx, ly, rx, ry;
    memset(used, 0, sizeof(used));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> fie[0][j][i];
        if (fie[0][j][i] == 'L' || fie[0][j][i] == 'R') {
          lx = j;
          ly = i;
        }
      }
      for (int j = 0; j < W; j++) {
        cin >> fie[1][j][i];
        if (fie[1][j][i] == 'L' || fie[1][j][i] == 'R') {
          rx = j;
          ry = i;
        }
      }
    }
    if (solve(lx, ly, rx, ry))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int W, H;
char fie[2][55][55];

bool used[55][55][55][55];
int dx[2][4] = {{0, 1, 0, -1}, {0, -1, 0, 1}};
int dy[] = {1, 0, -1, 0};

void move(int &x, int &y, int id, int rl) {
  int nx = x + dx[rl][id];
  int ny = y + dy[id];
  if (nx < 0 || ny < 0 || nx >= W || ny >= H)
    return;
  if (fie[rl][nx][ny] == '#')
    return;
  x = nx;
  y = ny;
}

bool solve(int lx, int ly, int rx, int ry) {
  if (used[lx][ly][rx][ry])
    return false;
  used[lx][ly][rx][ry] = true;
  if (fie[0][lx][ly] == '%' && fie[1][rx][ry] == '%')
    return true;
  if (fie[0][lx][ly] == '%' || fie[1][rx][ry] == '%')
    return false;
  for (int i = 0; i < 4; i++) {
    int nlx = lx, nly = ly, nrx = rx, nry = ry;
    move(nlx, nly, i, 0);
    move(nrx, nry, i, 1);
    if (used[nlx][nly][nrx][nry])
      continue;
    if (solve(nlx, nly, nrx, nry))
      return true;
  }
  return false;
}

int main() {
  while (cin >> W >> H && (W || H)) {
    int lx, ly, rx, ry;
    memset(used, 0, sizeof(used));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> fie[0][j][i];
        if (fie[0][j][i] == 'L' || fie[0][j][i] == 'R') {
          lx = j;
          ly = i;
        }
      }
      for (int j = 0; j < W; j++) {
        cin >> fie[1][j][i];
        if (fie[1][j][i] == 'L' || fie[1][j][i] == 'R') {
          rx = j;
          ry = i;
        }
      }
    }
    if (solve(lx, ly, rx, ry))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}