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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>

using Pair = std::pair<int, int>;

int w, h;
Pair ch;

std::vector<int> px[10], py[10];
int map[11][11];
std::vector<Pair> houses;
unsigned int mask;

int dfs(int y, int x, unsigned int used) {
  used |= 1u << map[y][x];

  if ((used & mask) == mask) {
    if (ch.first == y) {
      return 1;
    } else if (ch.second == x) {
      return 1;
    } else
      return 0;
  }

  int ans = 0;
  auto pxit = std::lower_bound(px[x].begin(), px[x].end(), y);
  auto pyit = std::lower_bound(py[y].begin(), py[y].end(), x);

  if (pxit != px[x].begin()) {
    auto it = pxit - 1;
    while (1) {
      if (!(used & (1 << map[*it][x]))) {
        ans += dfs(*it, x, used);
        break;
      }

      if (it == px[x].begin())
        break;

      --it;
    }
  }

  {
    auto it = pxit;

    if (pxit != px[x].end()) {
      if (*it == y)
        ++it;

      for (; it != px[x].end(); ++it) {
        if (!(used & (1 << map[*it][x]))) {
          ans += dfs(*it, x, used);
          break;
        }
      }
    }
  }

  if (pyit != py[y].begin()) {
    auto it = pyit - 1;
    while (1) {
      if (!(used & (1 << map[y][*it]))) {
        ans += dfs(y, *it, used);
        break;
      }

      if (it == py[y].begin())
        break;

      --it;
    }
  }

  {
    auto it = pyit;

    if (pyit != py[y].end()) {
      if (*it == x)
        ++it;

      for (; it != py[y].end(); ++it) {
        if (!(used & (1 << map[y][*it]))) {
          ans += dfs(y, *it, used);
          break;
        }
      }
    }
  }

  return ans;
}

int main() {
  while (std::cin >> w >> h, w + h) {
    houses.clear();
    for (auto &&v : px)
      v.clear();
    for (auto &&v : py)
      v.clear();

    memset(map, -1, sizeof(map));

    int in;

    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        std::cin >> in;

        if (in == 1) {
          px[j].push_back(i);
          py[i].push_back(j);
          map[i][j] = houses.size();
          houses.push_back({i, j});
        } else if (in == 2) {
          ch = {i, j};
        }
      }
    }

    for (int i = 0; i < houses.size(); ++i) {
      mask |= 1 << i;
    }

    std::cout << dfs(ch.first, ch.second, 0) << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>

using Pair = std::pair<int, int>;

int w, h;
Pair ch;

std::vector<int> px[10], py[10];
int map[11][11];
std::vector<Pair> houses;
unsigned int mask;

int dfs(int y, int x, unsigned int used) {
  used |= 1u << map[y][x];

  if ((used & mask) == mask) {
    if (ch.first == y) {
      auto st =
          std::upper_bound(py[y].begin(), py[y].end(), std::min(x, ch.second));
      auto en =
          std::lower_bound(py[y].begin(), py[y].end(), std::max(x, ch.second));

      if (en - st > 0) {
        return 0;
      } else {
        return 1;
      }
    } else if (ch.second == x) {
      auto st =
          std::upper_bound(px[x].begin(), px[x].end(), std::min(y, ch.first));
      auto en =
          std::lower_bound(px[x].begin(), px[x].end(), std::max(y, ch.first));

      if (en - st > 0) {
        return 0;
      } else {
        return 1;
      }
    } else
      return 0;
  }

  int ans = 0;
  auto pxit = std::lower_bound(px[x].begin(), px[x].end(), y);
  auto pyit = std::lower_bound(py[y].begin(), py[y].end(), x);

  if (pxit != px[x].begin()) {
    auto it = pxit - 1;
    while (1) {
      if (used & (1 << map[*it][x])) {
        break;
      }

      ans += dfs(*it, x, used);

      if (it == px[x].begin())
        break;

      --it;
    }
  }

  if (pxit != px[x].end()) {
    for (auto it = pxit + 1; it != px[x].end(); ++it) {
      if (used & (1 << map[*it][x])) {
        break;
      }

      ans += dfs(*it, x, used);
    }
  }

  if (pyit != py[y].begin()) {
    auto it = pyit - 1;
    while (1) {
      if (used & (1 << map[y][*it])) {
        break;
      }

      ans += dfs(y, *it, used);

      if (it == py[y].begin())
        break;

      --it;
    }
  }

  if (pyit != py[y].end()) {
    for (auto it = pyit + 1; it != py[y].end(); ++it) {
      if (used & (1 << map[y][*it])) {
        break;
      }

      ans += dfs(y, *it, used);
    }
  }

  return ans;
}

int main() {
  while (std::cin >> w >> h, w + h) {
    houses.clear();
    for (auto &&v : px)
      v.clear();
    for (auto &&v : py)
      v.clear();

    memset(map, -1, sizeof(map));

    int in;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        std::cin >> in;

        if (in == 1) {
          px[j].push_back(i);
          py[i].push_back(j);
          map[i][j] = houses.size();
          houses.push_back({i, j});
        } else if (in == 2) {
          ch = {i, j};
        }
      }
    }

    for (int i = 0; i < houses.size(); ++i) {
      mask |= 1 << i;
    }

    std::cout << dfs(ch.first, ch.second, 0) << std::endl;
  }
}