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

#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORE(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, a) FOR(i, 0, a)
const int INF = 1 << 30;
using namespace std;
typedef pair<int, int> pr;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int sy, sx;
int W, H;
bool maps[9][9] = {};
int shared[9][9] = {};
bool bridged[9][9][2] = {};
int frees = 0;
bool able(int y, int x) { return (!maps[y][x]) && shared[y][x] < 2; }
void final(int y, int x, bool ok[9][9]) {
  ok[y][x] = true;
  REP(i, 2)
  if (bridged[y][x][i] && (!ok[y + dy[i]][x + dx[i]]))
      final(y + dy[i], x + dx[i], ok);
  if (bridged[y - 1][x][0] && (!ok[y - 1][x]))
    final(y - 1, x, ok);
  if (bridged[y][x - 1][0] && (!ok[y][x - 1]))
    final(y, x - 1, ok);
  return;
}
bool check() {
  bool ok[9][9] = {};
  int ans = 0;
  REP(i, H + 2)
  REP(j, W + 2)
  if (!maps[i][j]) {
    final(i, j, ok);
    goto ends;
  }
ends:;
  REP(i, H + 2)
  REP(j, W + 2)
  ans += ok[i][j];
  return ans == frees;
}
bool search(int y, int x) {
  if (y > H)
    return check();

  if (!able(y, x))
    return search(y + x / W, x % W + 1);
  bool ans = false;
  int cans = able(y + 1, x) + able(y, x + 1);
  if (cans + shared[y][x] < 2)
    return false;
  if (shared[y][x] == 0) {
    shared[y][x + 1]++;
    shared[y + 1][x]++;
    REP(i, 2) bridged[y][x][i] = true;
    ans = search(y + x / W, x % W + 1);
    REP(i, 2) bridged[y][x][i] = false;
    shared[y][x + 1]--;
    shared[y + 1][x]--;
    return ans;
  }
  if (shared[y][x] == 1) {
    REP(i, 2) {
      if (able(y + dy[i], x + dx[i])) {
        shared[y + dy[i]][x + dx[i]]++;
        bridged[y][x][i] = true;
        ans |= search(y + x / W, x % W + 1);
        bridged[y][x][i] = true;
        shared[y + dy[i]][x + dx[i]]--;
      }
    }
    return ans;
  }
}
int main() {

  // your code goes here
  while (cin >> W >> H && W) {
    frees = 0;
    REP(i, H + 2)
    REP(j, W + 2) {
      maps[i][j] = 1;
      shared[i][j] = 0;
      REP(k, 2) bridged[i][j][k] = 0;
    }
    FORE(i, 1, H)
    FORE(j, 1, W) {
      cin >> maps[i][j];
      if (!maps[i][j])
        frees++;
    }

    if (frees && search(1, 1))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  label:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORE(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, a) FOR(i, 0, a)
const int INF = 1 << 30;
using namespace std;
typedef pair<int, int> pr;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
bool search(int y, int x, bool maps[9][9], int frees) {
  if (maps[y][x])
    return false;
  maps[y][x] = true;
  frees--;
  if (frees == 0)
    return true;
  REP(i, 4)
  if (search(y + dy[i], x + dx[i], maps, frees))
    return true;
  maps[y][x] = false;
}
int main() {

  // your code goes here
  int W, H;
  while (cin >> W >> H && W) {
    bool maps[9][9] = {};
    int frees = 0;
    REP(i, H + 2)
    REP(j, W + 2)
    maps[i][j] = 1;
    FORE(i, 1, H)
    FORE(j, 1, W) {
      cin >> maps[i][j];
      if (!maps[i][j])
        frees++;
    }
    FORE(i, 1, H)
    FORE(j, 1, W)
    if (!maps[i][j]) {
      cout << (search(i, j, maps, frees) ? "Yes" : "No") << endl;
      goto label;
    }
  label:;
  }
  return 0;
}