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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const double EPS = 1e-10;
const int INF = 1001001001;

const int dy[] = {0, 1, 0, -1};
const int dx[] = {1, 0, -1, 0};

int w, h;
int board[7][7];
int conn[7][7];
int gy, gx, white;

bool isok(int y, int x) {
  int able[7][7] = {{0}};
  stack<pii> S;
  S.push(pii(y, x));
  while (!S.empty()) {
    pii pos = S.top();
    S.pop();
    able[pos.first][pos.second] = 1;
    rep(i, 4) {
      int py = pos.first + dy[i];
      int px = pos.second + dx[i];
      if (py < 0 || h <= py || px < 0 || w <= px)
        continue;

      if (py == gy && px == gx)
        able[gy][gx] = 1;
      if (board[py][px] == 0 && able[py][px] == 0)
        S.push(pii(py, px));
    }
  }

  if (able[gy][gx] == 0)
    return false;

  rep(i, h) rep(j, w) {
    if (board[i][j] == 0 && able[i][j] == 0)
      return false;
  }
  return true;
}

bool dfs(int y, int x, int cnt) {
  if (!isok(y, x))
    return false;

  /*
  rep(i, h){
          rep(j, w)
                  printf("%d ", board[i][j]);
          puts("");
  }
  getchar();
  */

  int two = -1;
  rep(i, 4) {
    int py = y + dy[i];
    int px = x + dx[i];
    if (py < 0 || h <= py || px < 0 || w <= px || board[py][px])
      continue;
    if (conn[py][px] == 1)
      return false;
    if (conn[py][px] == 2) {
      if (two == -1)
        two = i;
      else
        return false;
    }
  }

  board[y][x] = 2;
  rep(i, 4) {
    int py = y + dy[i];
    int px = x + dx[i];
    if (py < 0 || h <= py || px < 0 || w <= px || board[py][px])
      continue;
    conn[py][px]--;
  }

  int index[4] = {0, 1, 2, 3};
  if (two != -1) {
    index[0] = two;
    index[1] = -1;
  }
  rep(i, 4) {
    if (index[i] == -1)
      break;

    int py = y + dy[index[i]];
    int px = x + dx[index[i]];
    if (py < 0 || h <= py || px < 0 || w <= px)
      continue;

    if (cnt == white && py == gy && px == gx)
      return true;

    conn[py][px]++;
    if (board[py][px] == 0 && dfs(py, px, cnt + 1))
      return true;
    conn[py][px]--;
  }

  rep(i, 4) {
    int py = y + dy[i];
    int px = x + dx[i];
    if (py < 0 || h <= py || px < 0 || w <= px || board[py][px])
      continue;
    conn[py][px]++;
  }
  board[y][x] = 0;

  return false;
}

int main() {
  while (scanf("%d%d", &w, &h), w) {
    rep(i, h) rep(j, w) scanf("%d", &board[i][j]);

    gy = -1;
    rep(i, h) rep(j, w) {
      if (board[i][j] == 0) {
        if (j < w - 1 && board[i][j + 1] == 0) {
          gy = i;
          gx = j;
        }
        goto next;
      }
    }
  next:
    white = 0;
    rep(i, h) rep(j, w) white += board[i][j] == 0;

    rep(i, h) rep(j, w) {
      conn[i][j] = 0;
      rep(k, 4) {
        int py = i + dy[k];
        int px = j + dx[k];
        if (py < 0 || h <= py || px < 0 || w <= px || board[py][px])
          continue;
        ++conn[i][j];
      }
    }

    bool ok = false;
    if (gy != -1) {
      board[gy][gx] = 2;
      conn[gy][gx]--;
      ok = dfs(gy, gx + 1, 2);
    }
    puts(ok ? "Yes" : "No");
    ;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const double EPS = 1e-10;
const int INF = 1001001001;

const int dy[] = {0, 1, 0, -1};
const int dx[] = {1, 0, -1, 0};

int w, h;
int board[7][7];
int gy, gx, white;

bool isok(int y, int x) {
  int able[7][7] = {{0}};
  stack<pii> S;
  S.push(pii(y, x));
  while (!S.empty()) {
    pii pos = S.top();
    S.pop();
    able[pos.first][pos.second] = 1;
    rep(i, 4) {
      int py = pos.first + dy[i];
      int px = pos.second + dx[i];
      if (py < 0 || h <= py || px < 0 || w <= px)
        continue;

      if (py == gy && px == gx)
        able[gy][gx] = 1;
      if (board[py][px] == 0 && able[py][px] == 0)
        S.push(pii(py, px));
    }
  }

  if (able[gy][gx] == 0)
    return false;

  rep(i, h) rep(j, w) {
    if (board[i][j] == 0 && able[i][j] == 0)
      return false;
  }
  return true;
}

bool dfs(int y, int x, int cnt) {
  if (!isok(y, x))
    return false;

  /*
  rep(i, h){
          rep(j, w)
                  printf("%d ", board[i][j]);
          puts("");
  }
  getchar();
  */

  board[y][x] = 2;
  rep(i, 4) {
    int py = y + dy[i];
    int px = x + dx[i];
    if (py < 0 || h <= py || px < 0 || w <= px)
      continue;

    if (cnt == white && py == gy && px == gx)
      return true;
    if (board[py][px] == 0 && dfs(py, px, cnt + 1))
      return true;
  }
  board[y][x] = 0;
  return false;
}

int main() {
  while (scanf("%d%d", &w, &h), w) {
    rep(i, h) rep(j, w) scanf("%d", &board[i][j]);

    gy = -1;
    rep(i, h) rep(j, w) {
      if (board[i][j] == 0) {
        if (j < w - 1 && board[i][j + 1] == 0) {
          gy = i;
          gx = j;
        }
        goto next;
      }
    }
  next:
    white = 0;
    rep(i, h) rep(j, w) white += board[i][j] == 0;

    bool ok = false;
    if (gy != -1) {
      board[gy][gx] = 2;
      ok = dfs(gy, gx + 1, 2);
    }
    puts(ok ? "Yes" : "No");
    ;
  }
  return 0;
}