Problem p00739 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#pragma comment(linker, "/STACK:400000000")

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (it = X.begin(); it != X.end(); it++)
#define iss istringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sst stringstream
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int w, h, r[13][13], state[13][13], id[13][13], b;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
int dy2[] = {1, 0, -1, 0}, dx2[] = {0, 1, 0, -1};
int sy1, sx1, sy2, sx2, rot, inv;
int co1[37], co2[37];
vi y, x;

bool err = 1;

pi rot_inv(int Dy, int Dx) {
  if (inv == 1)
    Dx = -Dx;
  if (rot == 0)
    return pi(Dy, Dx);
  if (rot == 1)
    return pi(Dx, -Dy);
  if (rot == 2)
    return pi(-Dy, -Dx);
  if (rot == 3)
    return pi(-Dx, Dy);
}

int visit[13][13];

int dfs(int y, int x, int target) {
  visit[y][x] = 1;
  int res = 1;
  rep(i, 4) {
    int ny = y + dy[i], nx = x + dx[i];
    if (state[ny][nx] == target && !visit[ny][nx]) {
      res += dfs(ny, nx, target);
    }
  }
  return res;
}

void show() {
  cout << sy1 << " " << sx1 << " " << sy2 << " " << sx2 << " " << rot << " "
       << inv << endl;
  rep(i, h + 2) {
    rep(j, w + 2) cout << state[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}

int f(int num) {
  if (num == b) {
    int sy1, sx1;
    rep(i, b) {
      if (state[y[i]][x[i]] == 1) {
        sy1 = y[i], sx1 = x[i];
        break;
      }
    }
    memset(visit, 0, sizeof(visit));
    int res1 = dfs(sy1, sx1, 1);
    // if(res1==b/2)show();
    return res1 == b / 2;
  }
  rep(i, b) if (state[y[i]][x[i]] == 0) {
    if (co1[i] != -1 && state[y[co1[i]]][x[co1[i]]] == 0) {
      state[y[i]][x[i]] = 1;
      state[y[co1[i]]][x[co1[i]]] = 2;
      if (f(num + 2))
        return 1;
      state[y[i]][x[i]] = 0;
      state[y[co1[i]]][x[co1[i]]] = 0;
    }
    if (co2[i] != -1 && state[y[co2[i]]][x[co2[i]]] == 0) {
      state[y[i]][x[i]] = 2;
      state[y[co2[i]]][x[co2[i]]] = 1;
      if (f(num + 2))
        return 1;
      state[y[i]][x[i]] = 0;
      state[y[co2[i]]][x[co2[i]]] = 0;
    }
    return 0;
  }
}

int main() {
  int tc = 0;
  time_t start = clock(), pre = start;
  while (cin >> w >> h && w) {
    memset(r, 0, sizeof(r));
    memset(id, -1, sizeof(id));
    y.clear();
    x.clear();
    b = 0;
    rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
      cin >> r[i][j];
      if (r[i][j]) {
        y.pb(i);
        x.pb(j);
        id[i][j] = b++;
      }
    }
    if (b % 2) {
      cout << "NO\n";
      goto end;
    }
    memset(state, 0, sizeof(state));
    rep(i, b) rep2(j, i + 1, b) rep(k, 4) rep(l, 2) {
      sy1 = y[i];
      sx1 = x[i];
      sy2 = y[j];
      sx2 = x[j];
      rot = k;
      inv = l;
      fill(co1, co1 + b, -1);
      fill(co2, co2 + b, -1);
      rep(m, b) if (m != i && m != j) {
        pi diff = rot_inv(y[m] - sy1, x[m] - sx1);
        int ny = sy2 + diff.fir, nx = sx2 + diff.sec;
        if (ny >= 1 && ny <= h && nx >= 1 && nx <= w && id[ny][nx] != -1 &&
            id[ny][nx] != i && id[ny][nx] != j && (ny != y[m] || nx != x[m])) {
          co1[m] = id[ny][nx];
          co2[id[ny][nx]] = m;
        }
      }
      state[y[i]][x[i]] = 1;
      state[y[j]][x[j]] = 2;
      int used[37] = {};
      used[i] = used[j] = 1;
      int num = 2;
      while (1) {
        int flag = 0;
        rep(m, b) if (state[y[m]][x[m]] == 0) {
          bool ok1 = co1[m] != -1 && !used[co1[m]];
          bool ok2 = co2[m] != -1 && !used[co2[m]];
          if (!ok1 && !ok2)
            goto skip;
          if (!ok2) {
            state[y[m]][x[m]] = 1;
            state[y[co1[m]]][x[co1[m]]] = 2;
            used[m] = used[co1[m]] = 1;
            num += 2;
            flag = 1;
          } else if (!ok1) {
            state[y[m]][x[m]] = 2;
            state[y[co2[m]]][x[co2[m]]] = 1;
            used[m] = used[co2[m]] = 1;
            num += 2;
            flag = 1;
          }
        }
        if (!flag)
          break;
      }
      if (f(num)) {
        cout << "YES\n";
        goto end;
      }
    skip:;
      rep(m, b) state[y[m]][x[m]] = 0;
    }
    cout << "NO\n";
  end:;
    time_t now = clock();
    if (err)
      cerr << "#" << ++tc << ": " << (double)(now - pre) / CLOCKS_PER_SEC
           << endl;
    pre = now;
  }
  time_t now = clock();
  if (err)
    cerr << "Total: " << (double)(now - start) / CLOCKS_PER_SEC << endl;
  if (err)
    cerr << "  Ave: " << (double)(now - start) / CLOCKS_PER_SEC / tc << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#pragma comment(linker, "/STACK:400000000")

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (it = X.begin(); it != X.end(); it++)
#define iss istringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sst stringstream
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int w, h, r[13][13], state[13][13], id[13][13], b;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
int dy2[] = {1, 0, -1, 0}, dx2[] = {0, 1, 0, -1};
int sy1, sx1, sy2, sx2, rot, inv;
int co1[37], co2[37];
vi y, x;

bool err = 0;

pi rot_inv(int Dy, int Dx) {
  if (inv == 1)
    Dx = -Dx;
  if (rot == 0)
    return pi(Dy, Dx);
  if (rot == 1)
    return pi(Dx, -Dy);
  if (rot == 2)
    return pi(-Dy, -Dx);
  if (rot == 3)
    return pi(-Dx, Dy);
}

int visit[13][13];

int dfs(int y, int x, int target) {
  visit[y][x] = 1;
  int res = 1;
  rep(i, 4) {
    int ny = y + dy[i], nx = x + dx[i];
    if (state[ny][nx] == target && !visit[ny][nx]) {
      res += dfs(ny, nx, target);
    }
  }
  return res;
}

void show() {
  cout << sy1 << " " << sx1 << " " << sy2 << " " << sx2 << " " << rot << " "
       << inv << endl;
  rep(i, h + 2) {
    rep(j, w + 2) cout << state[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}

int f(int num) {
  if (num == b) {
    int sy1, sx1;
    rep(i, b) {
      if (state[y[i]][x[i]] == 1) {
        sy1 = y[i], sx1 = x[i];
        break;
      }
    }
    memset(visit, 0, sizeof(visit));
    int res1 = dfs(sy1, sx1, 1);
    // if(res1==b/2)show();
    return res1 == b / 2;
  }
  rep(i, b) if (state[y[i]][x[i]] == 0) {
    if (co1[i] != -1 && state[y[co1[i]]][x[co1[i]]] == 0) {
      state[y[i]][x[i]] = 1;
      state[y[co1[i]]][x[co1[i]]] = 2;
      if (f(num + 2))
        return 1;
      state[y[i]][x[i]] = 0;
      state[y[co1[i]]][x[co1[i]]] = 0;
    }
    if (co2[i] != -1 && state[y[co2[i]]][x[co2[i]]] == 0) {
      state[y[i]][x[i]] = 2;
      state[y[co2[i]]][x[co2[i]]] = 1;
      if (f(num + 2))
        return 1;
      state[y[i]][x[i]] = 0;
      state[y[co2[i]]][x[co2[i]]] = 0;
    }
    return 0;
  }
}

int main() {
  int tc = 0;
  time_t start = clock(), pre = start;
  while (cin >> w >> h && w) {
    memset(r, 0, sizeof(r));
    memset(id, -1, sizeof(id));
    y.clear();
    x.clear();
    b = 0;
    rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
      cin >> r[i][j];
      if (r[i][j]) {
        y.pb(i);
        x.pb(j);
        id[i][j] = b++;
      }
    }
    if (b % 2) {
      cout << "NO\n";
      goto end;
    }
    memset(state, 0, sizeof(state));
    rep(i, b) rep2(j, i + 1, b) rep(k, 4) rep(l, 2) {
      sy1 = y[i];
      sx1 = x[i];
      sy2 = y[j];
      sx2 = x[j];
      rot = k;
      inv = l;
      fill(co1, co1 + b, -1);
      fill(co2, co2 + b, -1);
      rep(m, b) if (m != i && m != j) {
        pi diff = rot_inv(y[m] - sy1, x[m] - sx1);
        int ny = sy2 + diff.fir, nx = sx2 + diff.sec;
        if (ny >= 1 && ny <= h && nx >= 1 && nx <= w && id[ny][nx] != -1 &&
            id[ny][nx] != i && id[ny][nx] != j && (ny != y[m] || nx != x[m])) {
          co1[m] = id[ny][nx];
          co2[id[ny][nx]] = m;
        }
      }
      state[y[i]][x[i]] = 1;
      state[y[j]][x[j]] = 2;
      int used[37] = {};
      used[i] = used[j] = 1;
      int num = 2;
      while (1) {
        int flag = 0;
        rep(m, b) if (state[y[m]][x[m]] == 0) {
          bool ok1 = co1[m] != -1 && !used[co1[m]];
          bool ok2 = co2[m] != -1 && !used[co2[m]];
          if (!ok1 && !ok2)
            goto skip;
          if (!ok2) {
            state[y[m]][x[m]] = 1;
            state[y[co1[m]]][x[co1[m]]] = 2;
            used[m] = used[co1[m]] = 1;
            num += 2;
            flag = 1;
          } else if (!ok1) {
            state[y[m]][x[m]] = 2;
            state[y[co2[m]]][x[co2[m]]] = 1;
            used[m] = used[co2[m]] = 1;
            num += 2;
            flag = 1;
          }
        }
        if (!flag)
          break;
      }
      if (f(num)) {
        cout << "YES\n";
        goto end;
      }
    skip:;
      rep(m, b) state[y[m]][x[m]] = 0;
    }
    cout << "NO\n";
  end:;
    time_t now = clock();
    if (err)
      cerr << "#" << ++tc << ": " << (double)(now - pre) / CLOCKS_PER_SEC
           << endl;
    pre = now;
  }
  time_t now = clock();
  if (err)
    cerr << "Total: " << (double)(now - start) / CLOCKS_PER_SEC << endl;
  if (err)
    cerr << "  Ave: " << (double)(now - start) / CLOCKS_PER_SEC / tc << endl;
}