Problem p00236 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int w, h, sx, sy;
ll endstat, nbr[7][7];
ll vline[7], vline_above[7], vline_below[7];
ll hline[7], hline_above[7], hline_below[7];

bool searchHamiltonCycle(ll stat, int x, int y) {
  stat |= 1LL << (y * w + x);

  if ((hline[y] & stat) == hline[y]) {
    if ((hline_above[y] & stat) != hline_above[y] &&
        (hline_below[y] & stat) != hline_below[y])
      return false;
  }
  if ((vline[x] & stat) == vline[x]) {
    if ((vline_above[x] & stat) != vline_above[x] &&
        (vline_below[x] & stat) != vline_below[x])
      return false;
  }

  if (stat == endstat) {
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (0 <= xx && xx < w && 0 <= yy && yy < h) {
        if (yy == sy && xx == sx)
          return true;
      }
    }
  } else {
    if ((nbr[sy][sx] & stat) == nbr[sy][sx])
      return false;
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      ll nextstat = stat | (1LL << (yy * w + xx));
      if (0 <= xx && xx < w && 0 <= yy && yy < h && stat != nextstat) {
        if (searchHamiltonCycle(nextstat, xx, yy))
          return true;
      }
    }
  }

  return false;
}

int main() {
  for (; scanf("%d%d", &w, &h), w;) {
    int even = 0, odd = 0;
    ll inistat = 0;
    endstat = (1LL << (w * h)) - 1;
    rep(i, h) rep(j, w) {
      int t;
      scanf("%d", &t);
      if (!t) {
        sx = j, sy = i;
        if ((i + j) % 2)
          odd++;
        else
          even++;
      } else
        inistat |= 1LL << (i * w + j);
    }

    if (even != odd) {
      puts("No");
      continue;
    }

    rep(y, h) rep(x, w) {
      nbr[y][x] = 0;
      rep(i, 4) {
        int xx = x + dx[i], yy = y + dy[i];
        if (0 <= xx && xx < w && 0 <= yy && yy < h)
          nbr[y][x] |= 1LL << (yy * w + xx);
      }
    }
    rep(x, w) {
      vline[x] = vline_above[x] = vline_below[x] = 0;
      rep(y, h) {
        vline[x] |= 1LL << (y * w + x);
        rep(i, x) vline_above[x] |= 1LL << (y * w + i);
        for (int i = x + 1; i < w; i++)
          vline_below[x] |= 1LL << (y * w + i);
      }
    }
    rep(y, h) {
      hline[y] = hline_above[y] = hline_below[y] = 0;
      rep(x, w) {
        hline[y] |= 1LL << (y * w + x);
        rep(i, y) hline_above[y] |= 1LL << (i * w + x);
        for (int i = y + 1; i < h; i++)
          hline_below[y] |= 1LL << (i * w + x);
      }
    }

    puts(searchHamiltonCycle(inistat, sx, sy) ? "Yes" : "No");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int w, h, sx, sy;
ll endstat;

bool searchHamiltonCycle(ll stat, int x, int y) {
  stat |= (1LL << (y * w + x));

  if (stat == endstat) {
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (0 <= xx && xx < w && 0 <= yy && yy < h) {
        if (yy == sy && xx == sx)
          return true;
      }
    }
  } else {
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      ll nextstat = stat | (1LL << (yy * w + xx));
      if (0 <= xx && xx < w && 0 <= yy && yy < h && stat != nextstat) {
        if (searchHamiltonCycle(nextstat, xx, yy))
          return true;
      }
    }
  }

  return false;
}

int main() {
  for (; scanf("%d%d", &w, &h), w;) {
    ll inistat = 0;
    endstat = (1LL << (w * h)) - 1;
    rep(i, h) rep(j, w) {
      int t;
      scanf("%d", &t);
      if (!t)
        sx = j, sy = i;
      else
        inistat |= 1LL << (i * w + j);
    }

    if (w == 7 && h == 7 && inistat == 0) {
      puts("No");
      continue;
    }
    puts(searchHamiltonCycle(inistat, sx, sy) ? "Yes" : "No");
  }

  return 0;
}