Problem p00739 (C++) — buggy submission, verdict: 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;

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() {
  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 = -1, sx1, sy2 = -1, sx2;
    rep(i, b) {
      if (state[y[i]][x[i]] == 1) {
        sy1 = y[i], sx1 = x[i];
      }
      if (state[y[i]][x[i]] == 2) {
        sy2 = y[i], sx2 = x[i];
      }
      if (sy1 != -1 && sy2 != -1)
        break;
    }
    memset(visit, 0, sizeof(visit));
    int res1 = dfs(sy1, sx1, 1), res2 = dfs(sy2, sx2, 2);
    // if(res1==b/2 && res2==b/2)show();
    return res1 == b / 2 && res2 == b / 2;
  }
  rep(i, b) if (state[y[i]][x[i]] == 0) {
    int touch1 = 0;
    rep(j, 4) {
      if (state[y[i] + dy[j]][x[i] + dx[j]] == 1) {
        touch1 = 1;
        break;
      }
    }
    if (!touch1)
      continue;
    int cur = i;
    if (co1[cur] != -1) {
      int ny = y[co1[cur]], nx = x[co1[cur]];
      if (state[ny][nx] == 0) {
        state[y[cur]][x[cur]] = 1;
        state[ny][nx] = 2;
        if (f(num + 2))
          return 1;
        state[y[cur]][x[cur]] = 0;
        state[ny][nx] = 0;
      }
    }
    if (co2[cur] != -1) {
      int touch2 = 0;
      rep(i, 4) {
        if (state[y[cur] + dy[i]][x[cur] + dx[i]] == 2) {
          touch2 = 1;
          break;
        }
      }
      if (!touch2)
        continue;
      int ny = y[co2[cur]], nx = x[co2[cur]];
      if (state[ny][nx] != 0)
        return 0;
      if (state[ny][nx] == 0) {
        state[y[cur]][x[cur]] = 2;
        state[ny][nx] = 1;
        if (f(num + 2))
          return 1;
        state[y[cur]][x[cur]] = 0;
        state[ny][nx] = 0;
      }
    }
    return 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) {
        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 &&
            (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;
      co1[i] = co2[i] = -1;
      co1[j] = co2[j] = -1;
      int used = 2;
      while (1) {
        int flag = 0;
        rep(m, b) if (state[y[m]][x[m]] == 0) {
          if (co1[m] == -1 && co2[m] == -1)
            goto skip;
          if (co2[m] == -1) {
            state[y[m]][x[m]] = 1;
            state[y[co1[m]]][x[co1[m]]] = 2;
            co1[i] = co2[i] = -1;
            co1[j] = co2[j] = -1;
            used += 2;
            flag = 1;
          }
          if (co1[m] == -1) {
            state[y[m]][x[m]] = 2;
            state[y[co2[m]]][x[co2[m]]] = 1;
            co1[i] = co2[i] = -1;
            co1[j] = co2[j] = -1;
            used += 2;
            flag = 1;
          }
        }
        if (!flag)
          break;
      }
      if (f(used)) {
        cout << "YES\\n";
        goto end;
      }
    skip:;
      rep(m, b) state[y[m]][x[m]] = 0;
    }
    cout << "NO\\n";
  end:;
    time_t now = clock();
    // cerr<<"#"<<++tc<<": "<<(double)(now-pre)/CLOCKS_PER_SEC<<endl;
    pre = now;
  }
  time_t now = clock();
  // cerr<<"Total: "<<(double)(now-start)/CLOCKS_PER_SEC<<endl;
  // cerr<<"  Ave: "<<(double)(now-start)/CLOCKS_PER_SEC/tc<<endl;
}

Error: WA

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;

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) {
  return b / 2;
  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() {
  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 = -1, sx1, sy2 = -1, sx2;
    rep(i, b) {
      if (state[y[i]][x[i]] == 1) {
        sy1 = y[i], sx1 = x[i];
      }
      if (state[y[i]][x[i]] == 2) {
        sy2 = y[i], sx2 = x[i];
      }
      if (sy1 != -1 && sy2 != -1)
        break;
    }
    memset(visit, 0, sizeof(visit));
    int res1 = dfs(sy1, sx1, 1), res2 = dfs(sy2, sx2, 2);
    // if(res1==b/2 && res2==b/2)show();
    return res1 == b / 2 && res2 == b / 2;
  }
  rep(i, b) if (state[y[i]][x[i]] == 0) {
    int touch1 = 0;
    rep(j, 4) {
      if (state[y[i] + dy[j]][x[i] + dx[j]] == 1) {
        touch1 = 1;
        break;
      }
    }
    if (!touch1)
      continue;
    int cur = i;
    if (co1[cur] != -1) {
      int ny = y[co1[cur]], nx = x[co1[cur]];
      if (state[ny][nx] == 0) {
        state[y[cur]][x[cur]] = 1;
        state[ny][nx] = 2;
        if (f(num + 2))
          return 1;
        state[y[cur]][x[cur]] = 0;
        state[ny][nx] = 0;
      }
    }
    if (co2[cur] != -1) {
      int touch2 = 0;
      rep(i, 4) {
        if (state[y[cur] + dy[i]][x[cur] + dx[i]] == 2) {
          touch2 = 1;
          break;
        }
      }
      if (!touch2)
        continue;
      int ny = y[co2[cur]], nx = x[co2[cur]];
      if (state[ny][nx] != 0)
        return 0;
      if (state[ny][nx] == 0) {
        state[y[cur]][x[cur]] = 2;
        state[ny][nx] = 1;
        if (f(num + 2))
          return 1;
        state[y[cur]][x[cur]] = 0;
        state[ny][nx] = 0;
      }
    }
    return 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) {
        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 &&
            (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;
      co1[i] = co2[i] = -1;
      co1[j] = co2[j] = -1;
      int used = 2;
      while (1) {
        int flag = 0;
        rep(m, b) if (state[y[m]][x[m]] == 0) {
          if (co1[m] == -1 && co2[m] == -1)
            goto skip;
          if (co2[m] == -1) {
            state[y[m]][x[m]] = 1;
            state[y[co1[m]]][x[co1[m]]] = 2;
            co1[i] = co2[i] = -1;
            co1[j] = co2[j] = -1;
            used += 2;
            flag = 1;
          }
          if (co1[m] == -1) {
            state[y[m]][x[m]] = 2;
            state[y[co2[m]]][x[co2[m]]] = 1;
            co1[i] = co2[i] = -1;
            co1[j] = co2[j] = -1;
            used += 2;
            flag = 1;
          }
        }
        if (!flag)
          break;
      }
      if (f(used)) {
        cout << "YES\\n";
        goto end;
      }
    skip:;
      rep(m, b) state[y[m]][x[m]] = 0;
    }
    cout << "NO\\n";
  end:;
    time_t now = clock();
    cerr << "#" << ++tc << ": " << (double)(now - pre) / CLOCKS_PER_SEC << endl;
    pre = now;
  }
  time_t now = clock();
  cerr << "Total: " << (double)(now - start) / CLOCKS_PER_SEC << endl;
  cerr << "  Ave: " << (double)(now - start) / CLOCKS_PER_SEC / tc << endl;
}