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

// C++11
#include "bits/stdc++.h"
#include <bitset>
#include <emmintrin.h>
#include <fstream>
#include <random>
#include <sstream>
#include <string>
#include <sys/time.h>

using namespace std;

inline long long GetTSC() {
  long long lo, hi;
  asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
  return lo + (hi << 32);
}
inline double GetSeconds() { return GetTSC() / 2.8e9; }

const long inf = pow(10, 15);
int di[] = {-1, 0, 1, 0};
int dj[] = {0, 1, 0, -1};
tuple<int, int> q[50 * 50 * 50 * 50];

void solve() {
  double starttime = GetSeconds();
  while (1) {
    int w, h;
    cin >> w >> h;
    // cerr << w << ", " << h << endl;
    if (w == 0 && h == 0)
      break;
    int lp, ls, rp, rs;
    bool lb[2500] = {};
    bool rb[2500] = {};
    bool f[2500][2500] = {};
    for (int i = 0; i < h; i++) {
      string s;
      cin >> s;
      for (int j = 0; j < w; j++) {
        if (s[j] == '#')
          lb[i * w + j] = 1;
        if (s[j] == '%')
          lp = i * w + j;
        if (s[j] == 'L')
          ls = i * w + j;
      }
      cin >> s;
      for (int j = 0; j < w; j++) {
        if (s[j] == '#')
          rb[i * w + j] = 1;
        if (s[j] == '%')
          rp = i * w + j;
        if (s[j] == 'R')
          rs = i * w + j;
      }
    }
    f[ls][rs] = 1;
    int qi = 0;
    int qe = 1;
    q[0] = make_tuple(ls, rs);
    bool ff = 0;
    while (qi < qe) {
      int lt = get<0>(q[qi]);
      int rt = get<1>(q[qi]);
      int li = lt / w;
      int lj = lt % w;
      int ri = rt / w;
      int rj = rt % w;
      qi++;
      // cerr << "qie: " << qi << ", " << qe << ", lij: " << li << ", " << lj <<
      // ", rij: " << ri << ", " << rj << endl;
      for (int i = 0; i < 4; i++) {
        int nli = li + di[i];
        int nlj = lj + dj[i];
        int nri = ri + di[i];
        int nrj = rj - dj[i];
        if (nli < 0 || nli >= h || nlj < 0 || nlj >= w || lb[nli * w + nlj]) {
          nli = li;
          nlj = lj;
        }
        if (nri < 0 || nri >= h || nrj < 0 || nrj >= w || rb[nri * w + nrj]) {
          nri = ri;
          nrj = rj;
        }
        int nlt = nli * w + nlj;
        int nrt = nri * w + nrj;
        if (nlt == lp && nrt == rp) {
          ff = 1;
          break;
        }
        if (nlt == lp || nrt == rp)
          continue;
        if (f[nlt][nrt])
          continue;
        f[nlt][nrt] = 1;
        q[qe++] = make_tuple(nlt, nrt);
      }
      if (ff)
        break;
    }
    if (ff) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  // cerr << "time: " << GetSeconds() - starttime << endl;

  return;
}

int main() {
  solve();
  return 0;
}


Error: 0
time: 0.00199675


Fixed submission:

// C++11
#include "bits/stdc++.h"
#include <bitset>
#include <emmintrin.h>
#include <fstream>
#include <random>
#include <sstream>
#include <string>
#include <sys/time.h>

using namespace std;

inline long long GetTSC() {
  long long lo, hi;
  asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
  return lo + (hi << 32);
}
inline double GetSeconds() { return GetTSC() / 2.8e9; }

const long inf = pow(10, 15);
int di[] = {-1, 0, 1, 0};
int dj[] = {0, 1, 0, -1};
tuple<int, int> q[50 * 50 * 50 * 50];

void solve() {
  double starttime = GetSeconds();
  while (1) {
    int w, h;
    cin >> w >> h;
    // cerr << w << ", " << h << endl;
    if (w == 0 && h == 0)
      break;
    int lp, ls, rp, rs;
    bool lb[2500] = {};
    bool rb[2500] = {};
    bool f[2500][2500] = {};
    for (int i = 0; i < h; i++) {
      string s;
      cin >> s;
      for (int j = 0; j < w; j++) {
        if (s[j] == '#')
          lb[i * w + j] = 1;
        if (s[j] == '%')
          lp = i * w + j;
        if (s[j] == 'L')
          ls = i * w + j;
      }
      cin >> s;
      for (int j = 0; j < w; j++) {
        if (s[j] == '#')
          rb[i * w + j] = 1;
        if (s[j] == '%')
          rp = i * w + j;
        if (s[j] == 'R')
          rs = i * w + j;
      }
    }
    f[ls][rs] = 1;
    int qi = 0;
    int qe = 1;
    q[0] = make_tuple(ls, rs);
    bool ff = 0;
    while (qi < qe) {
      int lt = get<0>(q[qi]);
      int rt = get<1>(q[qi]);
      int li = lt / w;
      int lj = lt % w;
      int ri = rt / w;
      int rj = rt % w;
      qi++;
      // cerr << "qie: " << qi << ", " << qe << ", lij: " << li << ", " << lj <<
      // ", rij: " << ri << ", " << rj << endl;
      for (int i = 0; i < 4; i++) {
        int nli = li + di[i];
        int nlj = lj + dj[i];
        int nri = ri + di[i];
        int nrj = rj - dj[i];
        if (nli < 0 || nli >= h || nlj < 0 || nlj >= w || lb[nli * w + nlj]) {
          nli = li;
          nlj = lj;
        }
        if (nri < 0 || nri >= h || nrj < 0 || nrj >= w || rb[nri * w + nrj]) {
          nri = ri;
          nrj = rj;
        }
        int nlt = nli * w + nlj;
        int nrt = nri * w + nrj;
        if (nlt == lp && nrt == rp) {
          ff = 1;
          break;
        }
        if (nlt == lp || nrt == rp)
          continue;
        if (f[nlt][nrt])
          continue;
        f[nlt][nrt] = 1;
        q[qe++] = make_tuple(nlt, nrt);
      }
      if (ff)
        break;
    }
    if (ff) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  cerr << "time: " << GetSeconds() - starttime << endl;

  return;
}

int main() {
  solve();
  return 0;
}