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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>

using namespace std;

typedef pair<int, int> P;
typedef pair<P, P> PP;

P ls, rs, lg, rg;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}, h, w;
bool f[52][52][52][52];
char lf[52][53], rf[52][53];

int range(int a, int b) { return a >= 0 && a < h && b >= 0 && b < w; }

int bfs() {
  int i, lx, ly, rx, ry;
  queue<PP> que;
  que.push(PP(ls, rs));
  while (que.size()) {
    lx = que.front().first.second, ly = que.front().first.first,
    rx = que.front().second.second, ry = que.front().second.first;
    que.pop();
    if (lg.first == ly && lg.second == lx ||
        rg.first == ry && rg.second == rx) {
      if (lg.first == ly && lg.second == lx && rg.first == ry &&
          rg.second == rx)
        return 1;
      else
        continue;
    }
    for (i = 0; i < 4; i++) {
      if (lf[ly + dy[i]][lx + dx[i]] != '#' &&
          rf[ry + dy[i]][rx - dx[i]] != '#' &&
          f[ly + dy[i]][lx + dx[i]][ry + dy[i]][rx - dx[i]]) {
        f[ly + dy[i]][lx + dx[i]][ry + dy[i]][rx - dx[i]] = 0;
        que.push(PP(P(ly + dy[i], lx + dx[i]), P(ry + dy[i], rx - dx[i])));
      } else if (lf[ly + dy[i]][lx + dx[i]] != '#' &&
                 rf[ry + dy[i]][rx - dx[i]] == '#' &&
                 f[ly + dy[i]][lx + dx[i]][ry][rx]) {
        f[ly + dy[i]][lx + dx[i]][ry][rx] = 0;
        que.push(PP(P(ly + dy[i], lx + dx[i]), P(ry, rx)));
      } //
      else if (lf[ly + dy[i]][lx + dx[i]] == '#' &&
               rf[ry + dy[i]][rx - dx[i]] != '#' &&
               f[ly][lx][ry + dy[i]][rx - dx[i]]) {
        f[ly][lx][ry + dy[i]][rx - dx[i]] = 0;
        que.push(PP(P(ly, lx), P(ry + dy[i], rx - dx[i])));
      }
    }
  }
  return 0;
}

int main(void) {
  int i, j;
  while (1) {
    scanf("%d%d%*c", &w, &h);
    if (w == 0 && h == 0)
      break;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        lf[i + 1][j + 1] = getchar();
        if (lf[i + 1][j + 1] == 'L') {
          ls = P(i + 1, j + 1);
          lf[i + 1][j + 1] = '.';
        } else if (lf[i + 1][j + 1] == '%') {
          lg = P(i + 1, j + 1);
          lf[i + 1][j + 1] = '.';
        }
      }
      getchar(); // 空白読み飛ばし
      for (j = 0; j < w; j++) {
        rf[i + 1][j + 1] = getchar();
        if (rf[i + 1][j + 1] == 'R') {
          rs = P(i + 1, j + 1);
          rf[i + 1][j + 1] = '.';
        } else if (rf[i + 1][j + 1] == '%') {
          rg = P(i + 1, j + 1);
          rf[i + 1][j + 1] = '.';
        }
      }
      getchar(); // 改行文字読み飛ばし
    }
    for (i = 0; i < h + 2; i++) {
      lf[i][0] = rf[i][0] = '#';
      lf[i][w + 1] = rf[i][w + 1] = '#';
    }
    for (i = 0; i < w + 2; i++) {
      lf[0][i] = rf[0][i] = '#';
      lf[h + 1][i] = rf[h + 1][i] = '#';
    }
    memset(f, -1, sizeof(f));
    f[ls.first][ls.second][rs.first][rs.second] = 0;
    if (bfs())
      printf("Yes\\n");
    else
      printf("No\\n");
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <utility>

using namespace std;

typedef pair<int, int> P;
typedef long long int ll;

P lg, rg;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}, h, w;
ll f[50][50][50];
char lf[50][51], rf[50][51];

int range(int a, int b) { return a >= 0 && a < h && b >= 0 && b < w; }

bool dfs(int ly, int lx, int ry, int rx) {
  int i;
  if (lg.first == ly && lg.second == lx || rg.first == ry && rg.second == rx) {
    if (lg.first == ly && lg.second == lx && rg.first == ry && rg.second == rx)
      return 1;
    return 0;
  }
  for (i = 0; i < 4; i++) {
    if (range(ly + dy[i], lx + dx[i]) && range(ry + dy[i], rx - dx[i])) {
      if (lf[ly + dy[i]][lx + dx[i]] != '#' &&
          rf[ry + dy[i]][rx - dx[i]] != '#' &&
          !(f[ly + dy[i]][lx + dx[i]][ry + dy[i]] & (ll)(1 << (rx - dx[i])))) {
        f[ly + dy[i]][lx + dx[i]][ry + dy[i]] |= (ll)(1 << (rx - dx[i]));
        if (dfs(ly + dy[i], lx + dx[i], ry + dy[i], rx - dx[i]))
          return 1;
      } else if (lf[ly + dy[i]][lx + dx[i]] != '#' &&
                 rf[ry + dy[i]][rx - dx[i]] == '#' &&
                 !(f[ly + dy[i]][lx + dx[i]][ry] & (ll)(1 << rx))) {
        f[ly + dy[i]][lx + dx[i]][ry] |= (ll)(1 << rx);
        if (dfs(ly + dy[i], lx + dx[i], ry, rx))
          return 1;
      } else if (lf[ly + dy[i]][lx + dx[i]] == '#' &&
                 rf[ry + dy[i]][rx - dx[i]] != '#' &&
                 !(f[ly][lx][ry + dy[i]] & (ll)(1 << (rx - dx[i])))) {
        f[ly][lx][ry + dy[i]] |= (ll)(1 << (rx - dx[i]));
        if (dfs(ly, lx, ry + dy[i], rx - dx[i]))
          return 1;
      }
    }
  }
  return 0;
}

int main(void) {
  int i, j;
  P rs, ls;
  while (1) {
    scanf("%d%d%*c", &w, &h);
    if (w == 0 && h == 0)
      break;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        lf[i][j] = getchar();
        if (lf[i][j] == 'L') {
          ls = P(i, j);
          lf[i][j] = '.';
        } else if (lf[i][j] == '%') {
          lg = P(i, j);
          lf[i][j] = '.';
        }
      }
      getchar(); // 空白読み飛ばし
      for (j = 0; j < w; j++) {
        rf[i][j] = getchar();
        if (rf[i][j] == 'R') {
          rs = P(i, j);
          rf[i][j] = '.';
        } else if (rf[i][j] == '%') {
          rg = P(i, j);
          rf[i][j] = '.';
        }
      }
      getchar(); // 改行文字読み飛ばし
    }
    memset(f, 0, sizeof(f));
    f[ls.first][ls.second][rs.first] = (ll)(1 << rs.second);
    if (dfs(ls.first, ls.second, rs.first, rs.second))
      printf("YES\\n");
    else
      printf("NO\\n");
  }
  return 0;
}