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

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int W, H;

int g[7][7];
int cg[7][7];
int ly, lx;

bool inside(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

bool neighbor(int y, int x) { return abs(y - ly) + abs(x - lx) == 1; }

void ff(int y, int x) {
  if (inside(y, x) && cg[y][x] == 0) {
    for (int i = 0; i < 4; i++) {
      cg[y][x] = 1;
      ff(y + (~i & i - 1), x + (i ? 2 - i : 0));
    }
  }
}

bool dfs(int y, int x, int r) {
  if (r == 1)
    return neighbor(y, x);
  g[y][x] = 1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (g[i][j] == 0) {
        copy(g[0], g[7], cg[0]);
        ff(i, j);
        for (int k = 0; k < H; k++) {
          for (int l = 0; l < W; l++) {
            if (cg[k][l] == 0) {
              goto fail;
            }
          }
        }
        for (int i = 0; i < 4; i++) {
          int ny = y + (~i & i - 1);
          int nx = x + (i ? 2 - i : 0);
          if (inside(ny, nx) && g[ny][nx] == 0) {
            for (int j = 0; j < 4; j++) {
              if (i == j)
                continue;
              int oy = y + (~j & j - 1);
              int ox = x + (j ? 2 - j : 0);
              if (inside(oy, ox) && g[oy][ox] == 0 && !neighbor(oy, ox)) {
                int c = 0;
                for (int k = 0; k < 4; k++) {
                  int ony = oy + (~k & k - 1);
                  int onx = ox + (k ? 2 - k : 0);
                  c += inside(ony, onx) && g[ony][onx] == 0;
                }
                if (c < 2)
                  goto next;
              }
            }
            if (dfs(ny, nx, r - 1))
              return true;
          next:;
          }
        }
        goto fail;
      }
    }
  }
fail:
  g[y][x] = 0;
  return false;
}

int main() {
  while (cin >> W >> H, W) {
    int c = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> g[i][j];
        if (g[i][j] == 0) {
          ly = i;
          lx = j;
          c++;
        }
      }
    }
    cout << ((c && dfs(ly, lx, c)) ? "Yes" : "No") << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int W, H;

int g[7][7];
int cg[7][7];
int ly, lx;

bool inside(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

void ff(int y, int x) {
  if (inside(y, x) && cg[y][x] == 0) {
    for (int i = 0; i < 4; i++) {
      cg[y][x] = 1;
      ff(y + (~i & i - 1), x + (i ? 2 - i : 0));
    }
  }
}

bool dfs(int y, int x, int r) {
  // for(int i=0;i<H;i++){
  //   for(int j=0;j<W;j++){
  //     cout<<g[i][j];
  //   }
  //   cout<<endl;
  // }
  // cout<<"=========="<<endl;
  if (r == 1)
    return abs(y - ly) + abs(x - lx) == 1;
  g[y][x] = 1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (g[i][j] == 0) {
        copy(g[0], g[7], cg[0]);
        ff(i, j);
        for (int k = 0; k < H; k++) {
          for (int l = 0; l < W; l++) {
            if (cg[k][l] == 0) {
              goto fail;
            }
          }
        }
        for (int i = 0; i < 4; i++) {
          int ny = y + (~i & i - 1);
          int nx = x + (i ? 2 - i : 0);
          if (inside(ny, nx) && g[ny][nx] == 0 && dfs(ny, nx, r - 1))
            return true;
        }
        goto fail;
      }
    }
  }
fail:
  g[y][x] = 0;
  return false;
}

int main() {
  while (cin >> W >> H, W) {
    int c = 0;
    vector<int> vy, vx;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> g[i][j];
        if (g[i][j] == 0) {
          vy.push_back(i);
          vx.push_back(j);
          ly = i;
          lx = j;
          c++;
        }
      }
    }
    // ly=vy[c/2];
    // lx=vx[c/2];
    cout << ((c && dfs(ly = vy[c / 2], lx = vx[c / 2], c)) ? "Yes" : "No")
         << endl;
  }
}