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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

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

int n;
vector<vector<vector<int>>> to;
vector<bool> isToZero;

int dfs(int curr, bitset<24> bs, int toZeroNum) {
  if (bs.count() == n) {
    if (isToZero[curr])
      return 1;
    else
      return 0;
  }
  if (isToZero[curr]) {
    --toZeroNum;
    if (toZeroNum == 0)
      return 0;
  }

  int ret = 0;
  for (int i = 0; i < 4; ++i) {
    unsigned j = 0;
    while (j < to[curr][i].size() && bs[to[curr][i][j]])
      ++j;
    if (j < to[curr][i].size()) {
      j = to[curr][i][j];
      bs[j] = true;
      ret += dfs(j, bs, toZeroNum);
      bs[j] = false;
    }
  }

  return ret;
}

int main() {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (w == 0)
      return 0;

    n = 1;
    vector<int> y(1), x(1);
    vector<vector<int>> grid(h, vector<int>(w));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> grid[i][j];
        if (grid[i][j] == 1) {
          y.push_back(i);
          x.push_back(j);
          grid[i][j] = n;
          ++n;
        } else if (grid[i][j] == 2) {
          y[0] = i;
          x[0] = j;
          grid[i][j] = 0;
        } else {
          grid[i][j] = -1;
        }
      }
    }

    to.assign(n, vector<vector<int>>(4, vector<int>(0)));
    isToZero.assign(n, false);
    int toZeroNum = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 4; ++j) {
        int y1 = y[i] + dy[j];
        int x1 = x[i] + dx[j];
        while (0 <= y1 && y1 < h && 0 <= x1 && x1 < w) {
          if (grid[y1][x1] != -1) {
            int k = grid[y1][x1];
            if (k == 0) {
              isToZero[i] = true;
              ++toZeroNum;
            } else
              to[i][j].push_back(k);
          }
          y1 += dy[j];
          x1 += dx[j];
        }
      }
    }

    cout << dfs(0, 1, toZeroNum) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

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

int n;
vector<vector<vector<int>>> to;
vector<bool> isToZero;

int dfs(int curr, bitset<24> bs) {
  if (bs.count() == n) {
    if (isToZero[curr])
      return 1;
    else
      return 0;
  }

  int ret = 0;
  for (int i = 0; i < 4; ++i) {
    unsigned j = 0;
    while (j < to[curr][i].size() && bs[to[curr][i][j]])
      ++j;
    if (j < to[curr][i].size()) {
      j = to[curr][i][j];
      bs[j] = true;
      ret += dfs(j, bs);
      bs[j] = false;
    }
  }

  return ret;
}

int main() {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (w == 0)
      return 0;

    n = 1;
    vector<int> y(1), x(1);
    vector<vector<int>> grid(h, vector<int>(w));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> grid[i][j];
        if (grid[i][j] == 1) {
          y.push_back(i);
          x.push_back(j);
          grid[i][j] = n;
          ++n;
        } else if (grid[i][j] == 2) {
          y[0] = i;
          x[0] = j;
          grid[i][j] = 0;
        } else {
          grid[i][j] = -1;
        }
      }
    }

    to.assign(n, vector<vector<int>>(4, vector<int>(0)));
    isToZero.assign(n, false);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 4; ++j) {
        int y1 = y[i] + dy[j];
        int x1 = x[i] + dx[j];
        while (0 <= y1 && y1 < h && 0 <= x1 && x1 < w) {
          if (grid[y1][x1] != -1) {
            int k = grid[y1][x1];
            if (k == 0)
              isToZero[i] = true;
            else
              to[i][j].push_back(k);
          }
          y1 += dy[j];
          x1 += dx[j];
        }
      }
    }

    cout << dfs(0, 1) << endl;
  }
}