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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int const dxL[] = {-1, 0, 1, 0};
int const dxR[] = {1, 0, -1, 0};
int const dy[] = {0, -1, 0, 1};
typedef pair<int, int> Pii;
#define X first
#define Y second
int const INF = 1 << 28;
int W, H;
char grL[51][51], grR[51][51];
bool used[2500][2500];
inline bool isValid(int x, int y) {
  if (0 <= x && x < W && 0 <= y && y < H)
    return true;
  return false;
}

inline bool inF(int x, int y, int isL) {
  if (isL)
    return grL[y][x] != '#';
  return grR[y][x] != '#';
}
inline int toNode(int x, int y) { return y * W + x; }
inline void toPos(int node, int &x, int &y) { x = node % W, y = node / W; }
int main() {

  while (cin >> W >> H && (W | H)) {
    int sxL, syL, sxR, syR;
    int gxL, gyL, gxR, gyR;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < 2 * W; j++) {
        if (j < W) {
          cin >> grL[i][j];
          if (grL[i][j] == 'L') {
            sxL = j, syL = i;
          }
          if (grL[i][j] == '%') {
            gxL = j, gyL = i;
          }
        } else {
          cin >> grR[i][j - W];
          if (grR[i][j - W] == 'R') {
            sxR = j - W, syR = i;
          }
          if (grR[i][j - W] == '%') {
            gxR = j - W, gyR = i;
          }
        }
      }
    }

    queue<Pii> que;
    que.push(make_pair(toNode(sxL, syL), toNode(sxR, syR)));
    bool ok = false;
    memset(used, false, sizeof used);
    while (!que.empty()) {
      const int nowL = que.front().first;
      const int nowR = que.front().second;
      que.pop();
      int lx, ly;
      toPos(nowL, lx, ly);
      int rx, ry;
      toPos(nowR, rx, ry);

      if (lx == gxL && ly == gyL && rx == gxR && ry == gyR) {
        ok = true;
        break;
      }

      if (lx == gxL && ly == gyL)
        continue;
      if (rx == gxR && ry == gyR)
        continue;

      for (int i = 0; i < 4; i++) {
        int nxL = lx + dxL[i], nyL = ly + dy[i];
        int nxR = rx + dxR[i], nyR = ry + dy[i];

        if (!isValid(nxL, nyL) || !inF(nxL, nyL, 1)) {
          nxL = lx, nyL = ly;
        }
        if (!isValid(nxR, nyR) || !inF(nxR, nyR, 0)) {
          nxR = rx, nyR = ry;
        }
        if (used[toNode(nxL, nyL)][toNode(nxR, nyR)])
          continue;
        que.push(make_pair(toNode(nxL, nyL), toNode(nxR, nyR)));
        used[toNode(nxL, nyL)][toNode(nxR, nyR)] = true;
      }
    }
    if (ok)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
int const dxL[] = {-1, 0, 1, 0};
int const dxR[] = {1, 0, -1, 0};
int const dy[] = {0, -1, 0, 1};
typedef pair<int, int> Pii;
#define X first
#define Y second
int const INF = 1 << 28;
int W, H;
char grL[51][51], grR[51][51];
int dist[300][300];
inline bool isValid(int x, int y) {
  if (0 <= x && x < W && 0 <= y && y < H)
    return true;
  return false;
}

inline bool inF(int x, int y, int isL) {
  if (isL)
    return grL[y][x] != '#';
  return grR[y][x] != '#';
}
inline int toNode(int x, int y) { return y * W + x; }
inline void toPos(int node, int &x, int &y) { x = node % W, y = node / W; }
int main() {

  while (cin >> W >> H && (W | H)) {
    int sxL, syL, sxR, syR;
    int gxL, gyL, gxR, gyR;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < 2 * W; j++) {
        if (j < W) {
          cin >> grL[i][j];
          if (grL[i][j] == 'L') {
            sxL = j, syL = i;
          }
          if (grL[i][j] == '%') {
            gxL = j, gyL = i;
          }
        } else {
          cin >> grR[i][j - W];
          if (grR[i][j - W] == 'R') {
            sxR = j - W, syR = i;
          }
          if (grR[i][j - W] == '%') {
            gxR = j - W, gyR = i;
          }
        }
      }
    }

    queue<int> qL, qR;
    qL.push(toNode(sxL, syL));
    qR.push(toNode(sxR, syR));
    bool ok = false;
    fill(dist[0], dist[0] + 300 * 300, INF);
    dist[toNode(sxL, syL)][toNode(sxR, syR)] = 0;
    while (!qL.empty()) {
      int nowL = qL.front();
      qL.pop();
      int nowR = qR.front();
      qR.pop();
      int lx, ly;
      toPos(nowL, lx, ly);
      int rx, ry;
      toPos(nowR, rx, ry);
      if (lx == gxL && ly == gyL && rx == gxR && ry == gyR) {
        ok = true;
        break;
      }
      if (lx == gxL && ly == gyL) {
        continue;
      }
      if (rx == gxR && ry == gyR) {
        continue;
      }
      for (int i = 0; i < 4; i++) {
        int nxL = lx + dxL[i], nyL = ly + dy[i];
        int nxR = rx + dxR[i], nyR = ry + dy[i];
        if ((!isValid(nxL, nyL)) || (!isValid(nxR, nyR)))
          continue;
        if ((!inF(nxL, nyL, 1)) && (!inF(nxR, nyR, 0)))
          continue;
        if (!inF(nxL, nyL, 1)) {
          nxL = lx, nyL = ly;
        }
        if (!inF(nxR, nyR, 0)) {
          nxR = rx, nyR = ry;
        }
        if (dist[toNode(nxL, nyL)][toNode(nxR, nyR)] != INF)
          continue;
        qL.push(toNode(nxL, nyL));
        qR.push(toNode(nxR, nyR));
        dist[toNode(nxL, nyL)][toNode(nxR, nyR)] = dist[nowL][nowR] + 1;
      }
    }
    if (ok)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}