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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
struct cl {
  int v[4], x, y;
};
int dx[9] = {0, 0, 0, 0, 0, 1, -1, 2, -2};
int dy[9] = {0, 1, -1, 2, -2, 0, 0, 0, 0};
int valid(int x, int y) { return 0 <= x && x < 3 && 0 <= y && y < 3; }
bool operator<(const cl &x, const cl &y) {
  rep(i, 4) if (x.v[i] != y.v[i]) return x.v[i] < y.v[i];
  return x.x * 4 + x.y < y.x * 4 + y.y;
}
bool operator==(const cl &x, const cl &y) {
  rep(i, 4) if (x.v[i] != y.v[i]) return false;
  return x.x * 4 + x.y == y.x * 4 + y.y;
}
int main() {
  while (true) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    cl be;
    rep(i, 4) be.v[i] = 0;
    be.x = 1, be.y = 1;
    vector<cl> vc, nvc;
    vc.pb(be);
    rep(i, N) {
      int f[4][4];
      rep(j, 4) rep(k, 4) cin >> f[j][k];
      for (auto cloud : vc) {
        int v[4];
        rep(j, 4) v[j] = cloud.v[j] + 1;
        int x = cloud.x, y = cloud.y;
        if (x == 0 && y == 0)
          v[0] = 0;
        if (x == 0 && y == 2)
          v[1] = 0;
        if (x == 2 && y == 0)
          v[2] = 0;
        if (x == 2 && y == 2)
          v[3] = 0;
        bool can = true;
        rep(ii, 2) rep(jj, 2) if (f[x + ii][y + jj]) can = false;
        rep(j, 4) if (v[j] == 7) can = false;
        if (!can)
          continue;
        rep(j, 9) {
          int nx = x + dx[j], ny = y + dy[j];
          if (!valid(nx, ny))
            continue;
          cl hoge;
          rep(k, 4) hoge.v[k] = v[k];
          hoge.x = nx, hoge.y = ny;
          nvc.pb(hoge);
        }
      }
      sort(all(nvc));
      nvc.erase(unique(all(nvc)), nvc.end());
      vc = nvc;
      nvc.clear();
      //			show(vc.size());
    }
    cout << (vc.size() ? 1 : 0) << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
struct cl {
  int a, b, c, d, id;
};
bool operator<(const cl &x, const cl &y) {
  return x.a * 10000 + x.b * 1000 + x.c * 100 + x.d * 15 + x.id <
         y.a * 10000 + y.b * 1000 + y.c * 100 + y.d * 15 + y.id;
}
bool operator==(const cl &x, const cl &y) {
  return x.a == y.a && x.b == y.b && x.c == y.c && x.d == y.d && x.id == y.id;
}
inline bool ok(int x) { return x % 4 < 3 && x / 4 < 3; }
int mv[8] = {1, 2, -1, -2, 4, 8, -4, -8};
int main() {
  while (true) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    cl be = cl{7, 7, 7, 7, 5};
    vector<cl> vc;
    vc.pb(be);
    rep(i, N) {
      int x[16];
      rep(j, 16) cin >> x[j];
      vector<cl> nvc;
      for (auto cloud : vc) {
        int a = cloud.a, b = cloud.b, c = cloud.c, d = cloud.d, id = cloud.id;
        if (x[id] || x[id + 1] || x[id + 4] || x[id + 5])
          continue;
        rep(di, 8) {
          int ni = id + mv[di];
          if (!ok(ni))
            continue;
          a--, b--, c--, d--;
          if (ni == 0)
            a = 7;
          if (ni == 2)
            b = 7;
          if (ni == 8)
            c = 7;
          if (ni == 10)
            d = 7;
          if (a == 0 || b == 0 || c == 0 || d == 0)
            continue;
          nvc.pb(cl{a, b, c, d, ni});
        }
      }
      sort(all(nvc));
      nvc.erase(unique(all(nvc)), nvc.end());
      vc = nvc;
    }
    cout << (vc.size() > 0) << endl;
  }
}