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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

int w, h;
bool wl[55][55], wr[55][55];
bool memo[55][55][55][55];
int gxl, gyl, gxr, gyr;

struct node {
  int xl, yl, xr, yr;
  node(int lx, int ly, int rx, int ry) {
    xl = lx;
    yl = ly;
    xr = rx;
    yr = ry;
  }
};
/*
bool dfs(int xl, int yl, int xr, int yr) {
        if( memo[xl][yl][xr][yr] ) {
                return false;
        }
//	cout << "dfs " << xl << " " << yl << " " << xr << " " << yr << endl;
        memo[xl][yl][xr][yr] = true;
        if(xl==gxl && yl==gyl && xr==gxr && yr==gyr) {
                return true;
        }
        int dx[4] = {-1, 0, 1, 0};
        int dy[4] = {0, 1, 0, -1};
        bool f = false;
        rep(i, 4) {
                int nxl = xl + dx[i];
                int nyl = yl + dy[i];
                int nxr = xr - dx[i];
                int nyr = yr + dy[i];
                bool nl = wl[nyl][nxl];
                bool nr = wr[nyr][nxr];
                if( !nl && !nr && !memo[nxl][nyl][nxr][nyr] ) {
                        f |= dfs(nxl, nyl, nxr, nyr);
                }
                else if( !nl && nr && !memo[nxl][nyl][xr][yr] ) {
                        f |= dfs(nxl, nyl, xr, yr);
                }
                else if( nl && !nr && !memo[xl][yl][nxr][nyr] ) {
                        f |= dfs(xl, yl, nxr, nyr);
                }
        }
        return f;
}
*/
int main() {
  while (cin >> w >> h, w) {
    rep(i, 55) rep(j, 55) wl[i][j] = wr[i][j] = true;
    rep(i, 55) rep(j, 55) rep(k, 55) rep(l, 55) memo[i][j][k][l] = false;
    int xl = 1, yl = 1, xr = 1, yr = 1;
    rep(y, h) {
      string sl, sr;
      cin >> sl >> sr;
      rep(x, w) {
        if (sl[x] == 'L') {
          xl = x + 1;
          yl = y + 1;
          wl[y + 1][x + 1] = false;
        } else if (sl[x] == '%') {
          gxl = x + 1;
          gyl = y + 1;
          wl[y + 1][x + 1] = false;
        } else {
          wl[y + 1][x + 1] = (sl[x] == '#');
        }

        if (sr[x] == 'R') {
          xr = x + 1;
          yr = y + 1;
          wr[y + 1][x + 1] = false;
        } else if (sr[x] == '%') {
          gxr = x + 1;
          gyr = y + 1;
          wr[y + 1][x + 1] = false;
        } else {
          wr[y + 1][x + 1] = (sr[x] == '#');
        }
      }
    }

    queue<node> que;
    que.push(node(xl, yl, xr, yr));
    bool ok = false;
    while (!que.empty()) {
      node nd = que.front();
      que.pop();
      if (memo[nd.xl][nd.yl][nd.xr][nd.yr]) {
        continue;
      }
      //			cout << "lp " << nd.xl << " " << nd.yl << " " <<
      //nd.xr << " " << nd.yr << endl;
      memo[nd.xl][nd.yl][nd.xr][nd.yr] = true;
      if (nd.xl == gxl && nd.yl == gyl && nd.xr == gxr && nd.yr == gyr) {
        ok = true;
        break;
      }
      int dx[4] = {-1, 0, 1, 0};
      int dy[4] = {0, 1, 0, -1};
      rep(i, 4) {
        int nxl = nd.xl + dx[i];
        int nyl = nd.yl + dy[i];
        int nxr = nd.xr - dx[i];
        int nyr = nd.yr + dy[i];
        bool nl = wl[nyl][nxl];
        bool nr = wr[nyr][nxr];
        bool gl = (nxl == gxl && nyl == gyl);
        bool gr = (nxr == gxr && nyr == gyr);
        if (gl ^ gr)
          continue;
        if (!nl && !nr && !memo[nxl][nyl][nxr][nyr]) {
          que.push(node(nxl, nyl, nxr, nyr));
        } else if (!nl && nr && !memo[nxl][nyl][nd.xr][nd.yr]) {
          que.push(node(nxl, nyl, nd.xr, nd.yr));
        } else if (nl && !nr && !memo[nd.xl][nd.yl][nxr][nyr]) {
          que.push(node(nd.xl, nd.yl, nxr, nyr));
        }
      }
    }
    if (ok) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

int w, h;
bool wl[55][55], wr[55][55];
bool memo[55][55][55][55];
int gxl, gyl, gxr, gyr;

bool dfs(int xl, int yl, int xr, int yr) {
  if (memo[xl][yl][xr][yr]) {
    return false;
  }
  //	cout << "dfs " << xl << " " << yl << " " << xr << " " << yr << endl;
  memo[xl][yl][xr][yr] = true;
  if (xl == gxl && yl == gyl && xr == gxr && yr == gyr) {
    return true;
  }
  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, 1, 0, -1};
  bool f = false;
  rep(i, 4) {
    int nxl = xl + dx[i];
    int nyl = yl + dy[i];
    int nxr = xr - dx[i];
    int nyr = yr + dy[i];
    bool nl = wl[nyl][nxl];
    bool nr = wr[nyr][nxr];
    if (!nl && !nr && !memo[nxl][nyl][nxr][nyr]) {
      f |= dfs(nxl, nyl, nxr, nyr);
    } else if (!nl && nr && !memo[nxl][nyl][xr][yr]) {
      f |= dfs(nxl, nyl, xr, yr);
    } else if (nl && !nr && !memo[xl][yl][nxr][nyr]) {
      f |= dfs(xl, yl, nxr, nyr);
    }
  }
  return f;
}

int main() {
  while (cin >> w >> h, w) {
    rep(i, 55) rep(j, 55) wl[i][j] = wr[i][j] = true;
    rep(i, 55) rep(j, 55) rep(k, 55) rep(l, 55) memo[i][j][k][l] = false;
    int xl = 1, yl = 1, xr = 1, yr = 1;
    rep(y, h) {
      string sl, sr;
      cin >> sl >> sr;
      rep(x, w) {
        if (sl[x] == 'L') {
          xl = x + 1;
          yl = y + 1;
          wl[y + 1][x + 1] = false;
        } else if (sl[x] == '%') {
          gxl = x + 1;
          gyl = y + 1;
          wl[y + 1][x + 1] = false;
        } else {
          wl[y + 1][x + 1] = (sl[x] == '#');
        }
        if (sr[x] == 'R') {
          xr = x + 1;
          yr = y + 1;
          wr[y + 1][x + 1] = false;
        } else if (sr[x] == '%') {
          gxr = x + 1;
          gyr = y + 1;
          wr[y + 1][x + 1] = false;
        } else {
          wr[y + 1][x + 1] = (sr[x] == '#');
        }
      }
    }
    if (dfs(xl, yl, xr, yr)) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}