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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int room[2][100 * 100];
int start[2], goal[2];
int dz[] = {-1, 1, -52, 52};
int dz_[] = {1, -1, -52, 52};

int toz(int x, int y) { return x + y * 52; }
int ptoz(int z0, int z1) { return z0 + z1 * 3000; }

bool search() {
  vector<bool> visited(3000 * 3000, false);
  queue<PII> q;
  q.push(MP(start[0], start[1]));
  visited[ptoz(start[0], start[1])] = true;

  while (!q.empty()) {
    PII now = q.front();
    q.pop();
    REP(i, 4) {
      int z0 = now.first + dz[i];
      int z1 = now.second + dz_[i];
      if (!room[0][z0])
        z0 -= dz[i];
      if (!room[1][z1])
        z1 -= dz_[i];

      if (z0 == goal[0]) {
        if (z1 == goal[1])
          return true;
        else
          continue;
      }
      if (z1 == goal[1])
        continue;

      if (!visited[ptoz(z0, z1)]) {
        visited[ptoz(z0, z1)] = true;
        q.push(MP(z0, z1));
      }
    }
  }

  return false;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int W, H;
  while (cin >> W >> H, W) {
    fill((int *)room, (int *)room + 2 * 100 * 100, 0);
    REP(y, H) {
      REP(i, 2) {
        string t;
        cin >> t;
        REP(x, W) {
          int st = 0;
          switch (t[x]) {
          case 'R':
          case 'L':
            st = 1;
            start[i] = toz(x + 1, y + 1);
            break;
          case '%':
            st = 1;
            goal[i] = toz(x + 1, y + 1);
            break;
          case '.':
            st = 1;
            break;
          }
          room[i][toz(x + 1, y + 1)] = st;
        }
      }
    }

    cout << (search() ? "Yes" : "No") << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int room[2][100 * 100];
int start[2], goal[2];
int dz[] = {-1, 1, -100, 100};
int dz_[] = {1, -1, -100, 100};

int toz(int x, int y) { return x + y * 100; }
int ptoz(int z0, int z1) { return z0 + z1 * 3000; }

bool search() {
  vector<bool> visited(3000 * 3000, false);
  queue<PII> q;
  q.push(MP(start[0], start[1]));
  visited[ptoz(start[0], start[1])] = true;

  while (!q.empty()) {
    PII now = q.front();
    q.pop();
    REP(i, 4) {
      int z0 = now.first + dz[i];
      int z1 = now.second + dz_[i];
      if (!room[0][z0])
        z0 -= dz[i];
      if (!room[1][z1])
        z1 -= dz_[i];

      if (z0 == goal[0]) {
        if (z1 == goal[1])
          return true;
        else
          continue;
      }
      if (z1 == goal[1])
        continue;

      if (!visited[ptoz(z0, z1)]) {
        visited[ptoz(z0, z1)] = true;
        q.push(MP(z0, z1));
      }
    }
  }

  return false;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int W, H;
  while (cin >> W >> H, W) {
    fill((int *)room, (int *)room + 2 * 100 * 100, 0);
    REP(y, H) {
      REP(i, 2) {
        string t;
        cin >> t;
        REP(x, W) {
          int st = 0;
          switch (t[x]) {
          case 'R':
          case 'L':
            st = 1;
            start[i] = toz(x + 1, y + 1);
            break;
          case '%':
            st = 1;
            goal[i] = toz(x + 1, y + 1);
            break;
          case '.':
            st = 1;
            break;
          }
          room[i][toz(x + 1, y + 1)] = st;
        }
      }
    }

    cout << (search() ? "Yes" : "No") << endl;
  }

  return 0;
}