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

#include <stdio.h>
int c[10][10];
int w, h;
int sx, sy;
bool done[10][10];
int nh;
int solve(int x, int y, int nd) {
  int res = 0;
  int i, j;
  for (i = x + 1; i < w; i++) {
    if (nd == nh && i == sx && y == sy)
      return 1;
    if (done[i][y] && c[i][y] == 1) {
      done[i][y] = false;
      res += solve(i, y, nd + 1);
      done[i][y] = true;
      break;
    }
  }
  for (i = x - 1; i >= 0; i--) {
    if (nd == nh && i == sx && y == sy)
      return 1;
    if (done[i][y] && c[i][y] == 1) {
      done[i][y] = false;
      res += solve(i, y, nd + 1);
      done[i][y] = true;
      break;
    }
  }
  for (i = y + 1; i < h; i++) {
    if (nd == nh && x == sx && i == sy)
      return 1;
    if (done[x][i] && c[x][i] == 1) {
      done[x][i] = false;
      res += solve(x, i, nd + 1);
      done[x][i] = true;
      break;
    }
  }
  for (i = y - 1; i >= 0; i--) {
    if (nd == nh && x == sx && i == sy)
      return 1;
    if (done[x][i] && c[x][i] == 1) {
      done[x][i] = false;
      res += solve(x, i, nd + 1);
      done[x][i] = true;
      break;
    }
  }
  return res;
}

int main() {
  int i, j;
  while (1) {
    nh = 0;
    scanf("%d %d", &w, &h);
    if (w == 0)
      return 0;
    for (i = 0; i < w; i++)
      for (j = 0; j < h; j++)
        done[i][j] = true;
    for (i = 0; i < h; i++)
      for (j = 0; j < w; j++) {
        scanf("%d", &c[j][i]);
        if (c[j][i] == 1)
          nh++;
        if (c[j][i] == 2) {
          sx = j;
          sy = i;
        }
      }
    int ans = solve(sx, sy, 0);
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
int c[10][10];
int w, h;
int sx, sy;
bool done[10][10];
int nh;
int solve(int x, int y, int nd) {
  int res = 0;
  int i, j;
  for (i = x + 1; i < w; i++) {
    if (done[i][y])
      break;
    if (nd == nh && i == sx && y == sy)
      return 1;
    if (!done[i][y] && c[i][y] == 1) {
      done[i][y] = true;
      res += solve(i, y, nd + 1);
      done[i][y] = false;
    }
  }
  for (i = x - 1; i >= 0; i--) {
    if (done[i][y])
      break;
    if (nd == nh && i == sx && y == sy)
      return 1;
    if (!done[i][y] && c[i][y] == 1) {
      done[i][y] = true;
      res += solve(i, y, nd + 1);
      done[i][y] = false;
    }
  }
  for (i = y + 1; i < h; i++) {
    if (done[x][i])
      break;
    if (nd == nh && x == sx && i == sy)
      return 1;
    if (!done[x][i] && c[x][i] == 1) {
      done[x][i] = true;
      res += solve(x, i, nd + 1);
      done[x][i] = false;
    }
  }
  for (i = y - 1; i >= 0; i--) {
    if (done[x][i])
      break;
    if (nd == nh && x == sx && i == sy)
      return 1;
    if (!done[x][i] && c[x][i] == 1) {
      done[x][i] = true;
      res += solve(x, i, nd + 1);
      done[x][i] = false;
    }
  }
  return res;
}

int main() {
  int i, j;
  while (1) {
    nh = 0;
    scanf("%d %d", &w, &h);
    if (w == 0)
      return 0;
    for (i = 0; i < w; i++)
      for (j = 0; j < h; j++)
        done[i][j] = false;
    for (i = 0; i < h; i++)
      for (j = 0; j < w; j++) {
        scanf("%d", &c[j][i]);
        if (c[j][i] == 1)
          nh++;
        if (c[j][i] == 2) {
          sx = j;
          sy = i;
        }
      }
    int ans = solve(sx, sy, 0);
    printf("%d\\n", ans);
  }
}