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

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
struct Point {
  int x, y;
  Point(){};
  Point(int x, int y) : x(x), y(y){};
};
int W, H;
bool field[8][8];
int blank;
Point start;
bool res;

void check(Point p, int count) {
  if (res) {
    return;
  }
  for (int i = 0; i < 4; ++i) {
    if (start.x == p.x + dx[i] && start.y == p.y + dy[i] &&
        count == blank - 1) {
      res = true;
      return;
    }
  }

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (field[i][j] == false) {
        for (int k = 0; k < 4; ++k) {
          int nx = j + dx[k];
          int ny = i + dy[k];
          if (0 <= nx && nx < W && 0 <= ny && ny < H &&
              ((nx == start.x && ny == start.y) || (nx == p.x && ny == p.y))) {
            goto NEXT;
          }
        }
        int emptyNext = 0;
        for (int k = 0; k < 4; ++k) {
          int nx = j + dx[k];
          int ny = i + dy[k];
          if (0 <= nx && nx < W && 0 <= ny && ny < H &&
              field[ny][nx] == false) {
            ++emptyNext;
          }
        }
        if (emptyNext == 1) {
          return;
        }
      }
    NEXT:;
    }
  }
  for (int i = 0; i < 4; ++i) {
    int nx = p.x + dx[i];
    int ny = p.y + dy[i];
    if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] == false) {
      field[ny][nx] = true;
      check(Point(nx, ny), count + 1);
      field[ny][nx] = false;
    }
  }
}

void solve() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> W >> H, W || H) {
    memset(field, 0, sizeof(field));
    bool registered = false;
    blank = 0;
    res = false;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> field[i][j];
        if (field[i][j] == false) {
          ++blank;
        }
        if (registered == false && field[i][j] == false) {
          start.x = j;
          start.y = i;
          registered = true;
        }
      }
    }
    if (blank % 2 == 1) {
      cout << "No" << endl;
      goto END;
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (field[i][j] == false) {
          int emptyNext = 0;
          for (int k = 0; k < 4; ++k) {
            int nx = j + dx[k];
            int ny = i + dy[k];
            if (0 <= nx && nx < W && 0 <= ny && ny < H &&
                field[ny][nx] == false) {
              ++emptyNext;
            }
          }
          if (emptyNext == 1) {
            cout << "No" << endl;
            goto END;
          }
        }
      }
    }
    field[start.y][start.x] = true;
    check(start, 0);
    if (res) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  END:;
  }
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
struct Point {
  int x, y;
  Point(){};
  Point(int x, int y) : x(x), y(y){};
};
int W, H;
bool field[8][8];
bool visited[8][8];
int blank;
Point start;
bool res;

void check(Point p, int count) {
  if (res) {
    return;
  }
  for (int i = 0; i < 4; ++i) {
    if (start.x == p.x + dx[i] && start.y == p.y + dy[i] &&
        count == blank - 1) {
      res = true;
      return;
    }
  }
  for (int i = 0; i < 4; ++i) {
    int nx = p.x + dx[i];
    int ny = p.y + dy[i];
    if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] == false &&
        visited[ny][nx] == false) {
      visited[ny][nx] = true;
      check(Point(nx, ny), count + 1);
      visited[ny][nx] = false;
    }
  }
}

void solve() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> W >> H, W || H) {
    memset(visited, 0, sizeof(visited));
    bool registered = false;
    blank = 0;
    res = false;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> field[i][j];
        if (field[i][j] == false) {
          ++blank;
        }
        if (registered == false && field[i][j] == false) {
          start.x = j;
          start.y = i;
          registered = true;
        }
      }
    }
    if (blank % 2 == 1) {
      cout << "No" << endl;
      goto END;
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (field[i][j] == false) {
          int emptyNext = 0;
          for (int k = 0; k < 4; ++k) {
            int nx = j + dx[k];
            int ny = i + dy[k];
            if (0 <= nx && nx < W && 0 <= ny && ny < H &&
                field[ny][nx] == false) {
              ++emptyNext;
            }
          }
          if (emptyNext == 1) {
            cout << "No" << endl;
            goto END;
          }
        }
      }
    }
    visited[start.y][start.x] = true;
    check(start, 0);
    if (res) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  END:;
  }
}

int main() {
  solve();
  return (0);
}