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

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

struct state {
  int lx, ly, rx, ry, step;
};
int w, h;
char rmap[51][51], lmap[51][51];
queue<state> quu;
int lx, ly, rx, ry;
int d[51][51][51][51];
int xmj[4] = {0, 0, 1, -1}, ymj[4] = {1, -1, 0, 0};
int a[2], b[2];

void lmove(int *x, int *y, int i) {

  int lnx = *x - xmj[i], lny = *y + ymj[i];

  if (lnx < 0 || lnx >= w || lny < 0 || lny >= h)
    return;

  if (lmap[lny][lnx] == '#')
    return;

  *x = lnx;
  *y = lny;
}

void rmove(int *x, int *y, int i) {

  int rnx = *x + xmj[i], rny = *y + ymj[i];

  if (rnx < 0 || rnx >= w || rny < 0 || rny >= h)
    return;

  if (rmap[rny][rnx] == '#')
    return;

  *x = rnx;
  *y = rny;
}

void wide(int lx, int ly, int rx, int ry) {

  memset(d, 0, sizeof(d));
  quu.push((state){lx, ly, rx, ry, 1});
  d[ly][lx][ry][ry] = 1;
  while (!quu.empty()) {

    state st;
    st = quu.front();
    quu.pop();
    // cout << st.lx << " " << st.ly << " " << st.rx << " " << st.ry << endl;
    for (int i = 0; i < 4; i++) {
      int lnx = st.lx, lny = st.ly;
      int rnx = st.rx, rny = st.ry;

      lmove(&lnx, &lny, i);
      rmove(&rnx, &rny, i);

      if (rmap[rny][rnx] == '%' && lmap[lny][lnx] != '%')
        continue;
      if (rmap[rny][rnx] != '%' && lmap[lny][lnx] == '%')
        continue;

      if (d[lny][lnx][rny][rnx] == 0) {
        quu.push((state){lnx, lny, rnx, rny, st.step + 1});
        d[lny][lnx][rny][rnx] = 1;
      }
    }
  }

  if (d[a[0]][a[1]][b[0]][b[1]])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {

  while (cin >> w >> h && (w || h)) {

    for (int i = 0; i < h * 2; i++) {
      if (i % 2 == 0) {
        for (int j = 0; j < w; j++) {
          cin >> lmap[i / 2][j];
          if (lmap[i / 2][j] == 'L') {
            lx = j;
            ly = i / 2;
          }
          if (lmap[i / 2][j] == '%') {
            a[0] = i / 2;
            a[1] = j;
            //   cout << a[1] << " " << a[0] << endl;
          }
        }
      } else {
        for (int j = 0; j < w; j++) {
          cin >> rmap[(i - 1) / 2][j];
          if (rmap[(i - 1) / 2][j] == 'R') {
            rx = j;
            ry = (i - 1) / 2;
          }
          if (rmap[(i - 1) / 2][j] == '%') {
            b[0] = (i - 1) / 2;
            b[1] = j;
            //      cout << b[1] << " " << b[0] << endl;
          }
          //  cout << rx << " " << ry << endl;

          // cout << i;
        }
      }
    }
    wide(lx, ly, rx, ry);
  }
}

Error: 0

Fixed submission:

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

struct state {
  int lx, ly, rx, ry, step;
};
int w, h;
char rmap[51][51], lmap[51][51];
queue<state> quu;
int lx, ly, rx, ry;
int d[51][51][51][51];
int xmj[4] = {0, 0, 1, -1}, ymj[4] = {1, -1, 0, 0};
int a[2], b[2];

void wide(int lx, int ly, int rx, int ry) {

  memset(d, 0, sizeof(d));
  quu.push((state){lx, ly, rx, ry, 1});

  while (!quu.empty()) {

    state st;
    st = quu.front();
    quu.pop();
    // cout << st.lx << " " << st.ly << " " << st.rx << " " << st.ry << endl;
    for (int i = 0; i < 2; i++) {
      int lnx = st.lx + xmj[i], lny = st.ly + ymj[i];
      int rnx = st.rx + xmj[i], rny = st.ry + ymj[i];

      if (lnx >= 0 && lnx < w && lny >= 0 && lny < h && rnx >= 0 && rnx < w &&
          rny >= 0 && rny < h &&
          (rmap[rny][rnx] != '#' || lmap[lny][lnx] != '#') &&
          d[lny][lnx][rny][rnx] == 0) {
        if (rmap[rny][rnx] == '#') {
          quu.push((state){lnx, lny, st.lx, st.ly, st.step + 1});
          d[lny][lnx][st.ry][st.rx] = 1;
        } else if (lmap[lny][lnx] == '#') {
          quu.push((state){st.lx, st.ly, rnx, rny, st.step + 1});
          d[st.ly][st.lx][rny][rnx] = 1;
        } else {
          quu.push((state){lnx, lny, rnx, rny, st.step + 1});
          d[lny][lnx][rny][rnx] = 1;
        }
      }
    }
    for (int i = 2; i < 4; i++) {
      int lnx = st.lx - xmj[i], lny = st.ly + ymj[i];
      int rnx = st.rx + xmj[i], rny = st.ry + ymj[i];
      if (lnx >= 0 && lnx < w && lny >= 0 && lny < h && rnx >= 0 && rnx < w &&
          rny >= 0 && rny < h &&
          (rmap[rny][rnx] != '#' || lmap[lny][lnx] != '#') &&
          d[lny][lnx][rny][rnx] == 0) {
        if (rmap[rny][rnx] == '#') {
          quu.push((state){lnx, lny, st.lx, st.ly, st.step + 1});
          d[lny][lnx][st.ry][st.rx] = 1;
        } else if (lmap[lny][lnx] == '#') {
          quu.push((state){st.lx, st.ly, rnx, rny, st.step + 1});
          d[st.ly][st.lx][rny][rnx] = 1;
        } else {
          quu.push((state){lnx, lny, rnx, rny, st.step + 1});
          d[lny][lnx][rny][rnx] = 1;
        }
      }
    }
  }

  if (d[a[0]][a[1]][b[0]][b[1]])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {

  while (cin >> w >> h && (w || h)) {

    for (int i = 0; i < h * 2; i++) {
      if (i % 2 == 0) {
        for (int j = 0; j < w; j++) {
          cin >> lmap[i / 2][j];
          if (lmap[i / 2][j] == 'L') {
            lx = j;
            ly = i / 2;
          }
          if (lmap[i / 2][j] == '%') {
            a[0] = i / 2;
            a[1] = j;
            //   cout << a[1] << " " << a[0] << endl;
          }
        }
      } else {
        for (int j = 0; j < w; j++) {
          cin >> rmap[(i - 1) / 2][j];
          if (rmap[(i - 1) / 2][j] == 'R') {
            rx = j;
            ry = (i - 1) / 2;
          }
          if (rmap[(i - 1) / 2][j] == '%') {
            b[0] = (i - 1) / 2;
            b[1] = j;
            //      cout << b[1] << " " << b[0] << endl;
          }
          //  cout << rx << " " << ry << endl;

          // cout << i;
        }
      }
    }
    wide(lx, ly, rx, ry);
  }
}