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

#include <iomanip>
#include <iostream>
#include <queue>

using namespace std;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

const int N = 7;

int si, sj;
int H, W;
bool visited[N][N];
int im[N][N];
int path[N][N];
int zeroCnt;

inline bool inside(int i, int j) { return 0 <= i && i < H && 0 <= j && j < W; }

void checkEnclosed(int ci, int cj, bool vis[][N]) {
  queue<pair<int, int>> q;
  pair<int, int> cur;
  q.push(make_pair(ci, cj));

  while (!q.empty()) {
    cur = q.front();
    q.pop();
    if (vis[cur.first][cur.second])
      continue;

    vis[cur.first][cur.second] = true;
    for (int k = 0; k < 4; ++k) {
      int ni = cur.first + di[k];
      int nj = cur.second + dj[k];
      if (inside(ni, nj) && im[ni][nj] == 0 && !visited[ni][nj]) {
        q.push(make_pair(ni, nj));
      }
    }
  }
  return;
}

void printVisited() {
  for (int x = 0; x < H; ++x) {
    for (int y = 0; y < W; ++y) {
      if (im[x][y] == 0) {
        if (visited[x][y]) {
          cout << "v"; // << endl;
        } else {
          cout << "-"; // << endl;
        }
      } else {
        cout << "X"; // << endl;
      }
    }
    cout << endl;
  }
  cout << endl;
}

bool dfs(int step, int ci, int cj, int prei, int prej) {
  static int cnt = 0;
  ++cnt;
  if (cnt % 1000000 == 0) {
    // cout << cnt << endl;
  }

  if (visited[ci][cj]) {
    if (step == zeroCnt && ci == si && cj == sj) {
      return true;
    }
    return false;
  }

  int tcnt = 0;
  for (int k = 0; k < 4; ++k) {
    int ni = si + di[k];
    int nj = sj + dj[k];
    if (inside(ni, nj) && im[ni][nj] == 0 && !visited[ni][nj]) {
      ++tcnt;
    }
  }
  if (tcnt == 0) {
    return false;
  }

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (im[i][j] == 0 && !visited[i][j]) {
        int cnt = 0;
        for (int k = 0; k < 4; ++k) {
          int ni = i + di[k];
          int nj = j + dj[k];
          if (ni == si && nj == sj) {
            cnt = 900;
          } else if (ni == prei && nj == prej) {
            ++cnt;
          } else if (inside(ni, nj) && im[ni][nj] == 0 && !visited[ni][nj]) {
            ++cnt;
          }
        }
        if (cnt <= 1) {
          return false;
        }
      }
    }
  }

  bool vis[N][N] = {
      false,
  };
  checkEnclosed(ci, cj, vis);

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (im[i][j] == 0 && !vis[i][j] && !visited[i][j]) {
        return false;
      }
    }
  }

  visited[ci][cj] = true;
  path[ci][cj] = step;
  for (int k = 0; k < 4; ++k) {
    int ni = ci + di[k];
    int nj = cj + dj[k];
    if (inside(ni, nj) && im[ni][nj] == 0) {
      if (dfs(step + 1, ni, nj, ci, cj)) {
        return true;
      }
    }
  }
  visited[ci][cj] = false;

  return false;
}

int main() {
  while (true) {
    cin >> W >> H;
    zeroCnt = 0;
    if (W == 0 && H == 0) {
      break;
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> im[i][j];
        visited[i][j] = false;
        if (im[i][j] == 0) {
          si = i;
          sj = j;
          ++zeroCnt;
        }
      }
    }
    if (dfs(0, si, sj, -1, -1)) {
      cout << "Yes" << endl;
      // for (int i = 0; i < H; ++i){
      //	for (int j = 0; j < W; ++j){
      //		cout << setw(3) << path[i][j];
      //		//<< ' ';
      //	}
      //	cout << endl;
      // }
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <queue>

using namespace std;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

const int N = 7;

int si, sj;
int H, W;
bool visited[N][N];
int im[N][N];
int path[N][N];
int zeroCnt;

inline bool inside(int i, int j) { return 0 <= i && i < H && 0 <= j && j < W; }

void checkEnclosed(int ci, int cj, bool vis[][N]) {
  queue<pair<int, int>> q;
  pair<int, int> cur;
  q.push(make_pair(ci, cj));

  for (cur = q.front(); !q.empty(); cur = q.front(), q.pop()) {
    if (vis[cur.first][cur.second])
      continue;

    vis[cur.first][cur.second] = true;
    for (int k = 0; k < 4; ++k) {
      int ni = cur.first + di[k];
      int nj = cur.second + dj[k];
      if (inside(ni, nj) && im[ni][nj] == 0 && !visited[ni][nj]) {
        q.push(make_pair(ni, nj));
      }
    }
  }
  return;
}

void printVisited() {
  for (int x = 0; x < H; ++x) {
    for (int y = 0; y < W; ++y) {
      if (im[x][y] == 0) {
        if (visited[x][y]) {
          cout << "v"; // << endl;
        } else {
          cout << "-"; // << endl;
        }
      } else {
        cout << "X"; // << endl;
      }
    }
    cout << endl;
  }
  cout << endl;
}

bool dfs(int step, int ci, int cj, int prei, int prej) {
  static int cnt = 0;
  ++cnt;
  if (cnt % 1000000 == 0) {
    // cout << cnt << endl;
  }

  if (visited[ci][cj]) {
    if (step == zeroCnt && ci == si && cj == sj) {
      return true;
    }
    return false;
  }

  // printVisited();
  int tcnt = 0;
  for (int k = 0; k < 4; ++k) {
    int ni = si + di[k];
    int nj = sj + dj[k];
    if (inside(ni, nj) && im[ni][nj] == 0 && !visited[ni][nj]) {
      ++tcnt;
    }
  }
  if (tcnt == 0) {
    // printVisited();
    return false;
  }

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (im[i][j] == 0 && !visited[i][j]) {
        int cnt = 0;
        for (int k = 0; k < 4; ++k) {
          int ni = i + di[k];
          int nj = j + dj[k];
          if (ni == si && nj == sj) {
            cnt = 900;
          } else if (ni == prei && nj == prej) {
            ++cnt;
          } else if (inside(ni, nj) && im[ni][nj] == 0 && !visited[ni][nj]) {
            ++cnt;
          }
        }
        if (cnt <= 1) {
          return false;
        }
      }
    }
  }

  /*
          bool vis[N][N] = { false, };
          checkEnclosed(ci, cj, vis);

          for (int i = 0; i < H; ++i) {
                  for (int j = 0; j < W; ++j) {
                          if (im[i][j] == 0 && !vis[i][j] && !visited[i][j]) {
                                  return false;
                          }
                  }
          }
  */

  visited[ci][cj] = true;
  path[ci][cj] = step;
  for (int k = 0; k < 4; ++k) {
    int ni = ci + di[k];
    int nj = cj + dj[k];
    if (inside(ni, nj) && im[ni][nj] == 0) {
      if (dfs(step + 1, ni, nj, ci, cj)) {
        return true;
      }
    }
  }
  visited[ci][cj] = false;

  return false;
}

int main() {
  while (true) {
    cin >> W >> H;
    zeroCnt = 0;
    if (W == 0 && H == 0) {
      break;
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> im[i][j];
        visited[i][j] = false;
        if (im[i][j] == 0) {
          si = i;
          sj = j;
          ++zeroCnt;
        }
      }
    }
    if (dfs(0, si, sj, -1, -1)) {
      cout << "Yes" << endl;
      // for (int i = 0; i < H; ++i){
      //	for (int j = 0; j < W; ++j){
      //		cout << setw(3) << path[i][j];
      //		//<< ' ';
      //	}
      //	cout << endl;
      // }
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}