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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int H, W;
int sx, sy;
int bcnt;
bool field[7][7];
bool res = false;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

void solve(int nowx, int nowy, int ncnt) {
  if (res)
    return;
  if (bcnt - 1 == ncnt) {
    for (int i = 0; i < 4; i++) {
      int gx = nowx + dx[i], gy = nowy + dy[i];
      if (sx == gx && sy == gy) {
        res = true;
        return;
      }
    }
  }
  int neigh_cnt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j])
        continue;
      neigh_cnt = 0;
      for (int k = 0; k < 4; k++) {
        int nx = j + dx[k], ny = i + dy[k];
        if (sx == nx && sy == ny) {
          neigh_cnt++;
          continue;
        }
        if (nowx == nx && nowy == ny) {
          neigh_cnt++;
          continue;
        }
        if (nx >= 0 && nx < W && ny >= 0 && ny < H && field[ny][nx] == false) {
          neigh_cnt++;
        }
      }
      if (neigh_cnt == 1) {
        return;
      }
    }
  }
  for (int i = 0; i < 4; i++) {
    int nx = nowx + dx[i], ny = nowy + dy[i];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && field[ny][nx] == false) {
      field[ny][nx] = true;
      solve(nx, ny, ncnt + 1);
      field[ny][nx] = false;
    }
  }
  return;
}

int main() {
  while (cin >> W >> H, H) {
    res = false;
    bcnt = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> field[i][j];
        if (!field[i][j]) {
          if (bcnt == 0) {
            sx = j;
            sy = i;
          }
          bcnt++;
        }
      }
    }
    if (bcnt % 2 == 1)
      cout << "No" << endl;
    else {
      bool flg = true;
      int neigh_cnt;
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          if (field[i][j])
            continue;
          neigh_cnt = 0;
          for (int k = 0; k < 4; k++) {
            int nx = j + dx[k], ny = i + dy[k];
            if (nx >= 0 && nx < W && ny >= 0 && ny < H &&
                field[ny][nx] == false) {
              neigh_cnt++;
            }
          }
          if (neigh_cnt == 1) {
            flg = false;
            break;
          }
        }
        if (!flg)
          break;
      }
      if (!flg) {
        cout << "No" << endl;
      } else {
        field[sy][sx] = true;
        solve(sx, sy, 0);
        if (res)
          cout << "Yes" << endl;
        else
          cout << "No" << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int H, W;
int sx, sy;
int bcnt;
bool field[7][7];
bool res = false;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

void solve(int nowx, int nowy, int ncnt) {
  if (res)
    return;
  if (bcnt - 1 == ncnt) {
    for (int i = 0; i < 4; i++) {
      int gx = nowx + dx[i], gy = nowy + dy[i];
      if (sx == gx && sy == gy) {
        res = true;
        return;
      }
    }
  }
  /*
  int neigh_cnt = 0;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(field[i][j]) continue;
      neigh_cnt = 0;
      for(int k=0;k<4;k++){
        int nx=nowx+dx[k],ny=nowy+dy[k];
        if(nx>=0 && nx<W && ny>=0 && ny<H && field[ny][nx] == false){
          neigh_cnt++;
        }
      }
      if(neigh_cnt == 1) return;
    }
  }
  */
  for (int i = 0; i < 4; i++) {
    int nx = nowx + dx[i], ny = nowy + dy[i];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && field[ny][nx] == false) {
      field[ny][nx] = true;
      solve(nx, ny, ncnt + 1);
      field[ny][nx] = false;
    }
  }
  return;
}

int main() {
  while (cin >> W >> H, H) {
    res = false;
    bcnt = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> field[i][j];
        if (!field[i][j]) {
          if (bcnt == 0) {
            sx = j;
            sy = i;
          }
          bcnt++;
        }
      }
    }
    if (bcnt % 2 == 1)
      cout << "No" << endl;
    else {
      bool flg = true;
      int neigh_cnt;
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          if (field[i][j])
            continue;
          neigh_cnt = 0;
          for (int k = 0; k < 4; k++) {
            int nx = j + dx[k], ny = i + dy[k];
            if (nx >= 0 && nx < W && ny >= 0 && ny < H &&
                field[ny][nx] == false) {
              neigh_cnt++;
            }
          }
          if (neigh_cnt == 1) {
            flg = false;
            break;
          }
        }
        if (!flg)
          break;
      }
      if (!flg) {
        cout << "No" << endl;
      } else {
        solve(sx, sy, 0);
        if (res)
          cout << "Yes" << endl;
        else
          cout << "No" << endl;
      }
    }
  }
}