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

#include <algorithm>
#include <stdio.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int m[8][8];
int tmp[8][8];
int H, W;
void bfs(int a, int b) {
  tmp[a][b] = 1;
  for (int i = 0; i < 4; i++) {
    if (a + dx[i] < 0 || a + dx[i] >= H || b + dy[i] < 0 || b + dy[i] >= W)
      continue;
    if (!tmp[a + dx[i]][b + dy[i]])
      bfs(a + dx[i], b + dy[i]);
  }
}
int tr, tc;
int rem;
int ABS(int a) { return max(a, -a); }
int deg[8][8];
int kp[8][8];
int dfs(int a, int b) {
  if (ABS(tr - a) + ABS(tc - b) > rem)
    return 0;
  m[a][b] = 1;
  rem--;
  if (rem == 0) {
    for (int i = 0; i < 4; i++)
      if (a + dx[i] == tr && b + dy[i] == tc)
        return 1;
    return 0;
  }
  bool yet = true;
  for (int i = 0; i < 4; i++) {
    if (0 <= a + dx[i] && a + dx[i] < H && 0 <= b + dy[i] && b + dy[i] < W &&
        !m[a + dx[i]][b + dy[i]]) {
      deg[a + dx[i]][b + dy[i]]--;
    }
  }
  for (int i = 0; i < 4; i++) {
    if (0 <= a + dx[i] && a + dx[i] < H && 0 <= b + dy[i] && b + dy[i] < W &&
        !m[a + dx[i]][b + dy[i]]) {
      if (!kp[a + dx[i]][b + dy[i]] && deg[a + dx[i]][b + dy[i]] == 1) {
        if (dfs(a + dx[i], b + dy[i]))
          return 1;
        yet = false;
        break;
      }
    }
  }
  if (yet)
    for (int i = 0; i < 4; i++) {
      if (0 <= a + dx[i] && a + dx[i] < H && 0 <= b + dy[i] && b + dy[i] < W &&
          !m[a + dx[i]][b + dy[i]]) {
        if (dfs(a + dx[i], b + dy[i]))
          return 1;
      }
    }
  m[a][b] = 0;
  rem++;
  for (int i = 0; i < 4; i++) {
    if (0 <= a + dx[i] && a + dx[i] < H && 0 <= b + dy[i] && b + dy[i] < W &&
        !m[a + dx[i]][b + dy[i]]) {
      deg[a + dx[i]][b + dy[i]]++;
    }
  }
  return 0;
}
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    H = a;
    W = b;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        scanf("%d", &m[i][j]);
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        tmp[i][j] = m[i][j];
    int rk = 0;
    int sa = 0;
    rem = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        deg[i][j] = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (!m[i][j]) {
          for (int k = 0; k < 4; k++)
            if (0 <= i + dx[k] && i + dx[k] < a && 0 <= j + dy[k] &&
                j + dy[k] < b && !m[i + dx[k]][j + dy[k]])
              deg[i][j]++;
          if ((i + j) % 2)
            sa++;
          else
            sa--;
          rem++;
        }
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) {
        if (!tmp[i][j]) {
          tr = i;
          tc = j;
          bfs(i, j);
          rk++;
        }
      }
    if (rk != 1 || sa) {
      printf("No\\n");
      continue;
    }
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        kp[i][j] = 0;
    for (int i = 0; i < 4; i++)
      if (0 <= tr + dx[i] && tr + dx[i] < a && 0 <= tc + dy[i] &&
          tc + dy[i] < b)
        kp[tr + dx[i]][tc + dy[i]] = 1;
    if (dfs(tr, tc))
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int m[8][8];
int tmp[8][8];
int H, W;
void bfs(int a, int b) {
  tmp[a][b] = 1;
  for (int i = 0; i < 4; i++) {
    if (a + dx[i] < 0 || a + dx[i] >= H || b + dy[i] < 0 || b + dy[i] >= W)
      continue;
    if (!tmp[a + dx[i]][b + dy[i]])
      bfs(a + dx[i], b + dy[i]);
  }
}
int tr, tc;
int rem;
int ABS(int a) { return max(a, -a); }
int dfs(int a, int b) {
  if (ABS(tr - a) + ABS(tc - b) > rem)
    return 0;
  m[a][b] = 1;
  rem--;
  if (rem == 0) {
    for (int i = 0; i < 4; i++)
      if (a + dx[i] == tr && b + dy[i] == tc)
        return 1;
    return 0;
  }
  for (int i = 0; i < 4; i++) {
    if (0 <= a + dx[i] && a + dx[i] < H && 0 <= b + dy[i] && b + dy[i] < W &&
        !m[a + dx[i]][b + dy[i]]) {
      if (dfs(a + dx[i], b + dy[i]))
        return 1;
    }
  }
  m[a][b] = 0;
  rem++;
  return 0;
}
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    H = a;
    W = b;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        scanf("%d", &m[i][j]);
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        tmp[i][j] = m[i][j];
    int rk = 0;
    int sa = 0;
    rem = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (!m[i][j]) {
          if ((i + j) % 2)
            sa++;
          else
            sa--;
          rem++;
        }
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) {
        if (!tmp[i][j]) {
          tr = i;
          tc = j;
          bfs(i, j);
          rk++;
        }
      }
    if (rk != 1 || sa) {
      printf("No\\n");
      continue;
    }
    if (dfs(tr, tc))
      printf("Yes\\n");
    else
      printf("No\\n");
  }
}