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

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int T_MAX = 11;

int tb[][3][3] = {
    {{9, 1, 9}, {1, 0, 1}, {9, 9, 9}},

    {{9, 1, 9}, {9, 0, 1}, {9, 1, 9}},

    {{9, 0, 9}, {1, 0, 1}, {9, 1, 9}},

    {{9, 1, 9}, {1, 0, 9}, {9, 1, 9}},
    // +
    {{9, 1, 9}, {1, 0, 1}, {9, 1, 9}},
};

int cur[9][9], W, H, sx, sy;
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};

bool done[9][9];
int dfs(int x, int y) {
  if (done[y][x])
    return 0;
  else
    done[y][x] = true;
  if (y == sy && x == sx)
    return 1;
  if (cur[y][x] != 0)
    return 0;
  int ans = 1;
  rep(i, 4) ans += dfs(x + dx[i], y + dy[i]);
  return ans;
}
void output() {
  rep(i, H + 2) {
    rep(j, W + 2) cout << cur[i][j];
    cout << endl;
  }
}
bool isTaboo(int c, int x, int y) {
  int ans = 0;
  rep(i, 9) rep(j, 9) done[i][j] = 0;
  rep(i, 4) ans += dfs(x + dx[i], y + dy[i]);
  return c != ans;
}

bool badFlag;
int nest[50] = {0};
int bt(int c, int x, int y) {
  if (nest[c]++ > 25000) {
    badFlag = true;
    return true;
  }
  if (c == 0)
    return (x == sx && y == sy);
  if (cur[y][x] != 0)
    return false;
  cur[y][x] = 2;
  if (isTaboo(c, x, y)) {
    cur[y][x] = 0;
    return false;
  }
  rep(i, 4) if (bt(c - 1, x + dx[i], y + dy[i])) return true;
  cur[y][x] = 0;
  return false;
}
int main() {
  while (cin >> W >> H && W) {
    int eCount = 0;
    rep(i, 9) rep(j, 9) cur[i][j] = 1;
    rep(i, H) rep(j, W) cin >> cur[i + 1][j + 1], eCount += !cur[i + 1][j + 1];
    rep(i, H + 2) rep(j, W + 2) if (cur[i][j] == 0) sx = j, sy = i;
    rep(i, 50) nest[i] = 0;
    badFlag = 0;
    cout << (eCount > 3 && bt(eCount, sx, sy) && !badFlag ? "Yes" : "No")
         << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int T_MAX = 11;

int tb[][3][3] = {
    {{9, 1, 9}, {1, 0, 1}, {9, 9, 9}},

    {{9, 1, 9}, {9, 0, 1}, {9, 1, 9}},

    {{9, 0, 9}, {1, 0, 1}, {9, 1, 9}},

    {{9, 1, 9}, {1, 0, 9}, {9, 1, 9}},
    // +
    {{9, 1, 9}, {1, 0, 1}, {9, 1, 9}},
};

int cur[9][9], W, H, sx, sy;
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};

bool done[9][9];
int dfs(int x, int y) {
  if (done[y][x])
    return 0;
  else
    done[y][x] = true;
  if (y == sy && x == sx)
    return 1;
  if (cur[y][x] != 0)
    return 0;
  int ans = 1;
  rep(i, 4) ans += dfs(x + dx[i], y + dy[i]);
  return ans;
}
void output() {
  rep(i, H + 2) {
    rep(j, W + 2) cout << cur[i][j];
    cout << endl;
  }
}
bool isTaboo(int c, int x, int y) {
  int ans = 0;
  rep(i, 9) rep(j, 9) done[i][j] = 0;
  rep(i, 4) ans += dfs(x + dx[i], y + dy[i]);
  return c != ans;
}

bool badFlag;
int nest[50] = {0};
int bt(int c, int x, int y) {
  if (nest[c]++ > 30000) {
    badFlag = true;
    return true;
  }
  if (c == 0)
    return (x == sx && y == sy);
  if (cur[y][x] != 0)
    return false;
  cur[y][x] = 2;
  if (isTaboo(c, x, y)) {
    cur[y][x] = 0;
    return false;
  }
  rep(i, 4) if (bt(c - 1, x + dx[i], y + dy[i])) return true;
  cur[y][x] = 0;
  return false;
}
int main() {
  while (cin >> W >> H && W) {
    int eCount = 0;
    rep(i, 9) rep(j, 9) cur[i][j] = 1;
    rep(i, H) rep(j, W) cin >> cur[i + 1][j + 1], eCount += !cur[i + 1][j + 1];
    rep(i, H + 2) rep(j, W + 2) if (cur[i][j] == 0) sx = j, sy = i;
    rep(i, 50) nest[i] = 0;
    badFlag = 0;
    cout << (eCount > 3 && bt(eCount, sx, sy) && !badFlag ? "Yes" : "No")
         << endl;
  }
}