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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;

int m, n;
int f[11][11];
PII c;
int hn = 0;

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

ll dfs(int x, int y, int sum) {
  ll res = 0;
  if (x == c.fst && y == c.snd) {
    if (sum == hn) {
      return 1;
    } else if (sum > 0) {
      return 0;
    }
  }
  REP(i, 4) {
    int nx = dx[i] + x, ny = dy[i] + y;
    while (1) {
      if (nx < 0 || m <= nx || ny < 0 || n <= ny)
        break;
      if (f[nx][ny] == 2)
        res += dfs(nx, ny, sum + 1);
      if (f[nx][ny] == 1) {
        f[nx][ny] = -1;
        res += dfs(nx, ny, sum + 1);
        f[nx][ny] = 1;
        break;
      }
      nx += dx[i];
      ny += dy[i];
    }
  }
  return res;
}

int main(void) {
  while (cin >> m >> n && m && n) {
    hn = 0;
    REP(i, n) {
      REP(j, m) {
        int a;
        cin >> a;
        if (a == 1) {
          f[j][i] = 1;
          hn++;
        } else if (a == 2) {
          c = {j, i};
          f[j][i] = 2;
          hn++;
        } else {
          f[j][i] = -1;
        }
      }
    }
    cout << dfs(c.fst, c.snd, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;

int m, n;
vector<PII> px[11], py[11];
PII c;
int hn = 0;

ll dfs(int x, int y, int used) {
  ll res = 0;
  if (x == c.fst && y == c.snd) {
    if (used == (1 << hn) - 1) {
      return 1;
    }
  }
  REP(i, px[x].size()) {
    bool ok = true;
    REP(j, px[x].size()) {
      if (min(px[x][i].fst, y) < px[x][j].fst &&
          px[x][j].fst < max(px[x][i].fst, y)) {
        if ((used & (1 << px[x][j].snd)) == (1 << px[x][j].snd)) {
          ok = false;
        }
      }
    }
    if (ok && y != px[x][i].fst && (used & (1 << px[x][i].snd)) == 0) {
      res += dfs(x, px[x][i].fst, used + (1 << px[x][i].snd));
    }
  }
  REP(i, py[y].size()) {
    bool ok = true;
    REP(j, py[y].size()) {
      if (min(py[y][i].fst, x) < py[y][j].fst &&
          py[y][j].fst < max(py[y][i].fst, x)) {
        if ((used & (1 << py[y][j].snd)) == (1 << py[y][j].snd)) {
          ok = false;
        }
      }
    }
    if (ok && x != py[y][i].fst && (used & (1 << py[y][i].snd)) == 0) {
      res += dfs(py[y][i].fst, y, used + (1 << py[y][i].snd));
    }
  }
  return res;
}

int main(void) {
  while (cin >> m >> n && m && n) {
    hn = 0;
    REP(i, 11) {
      px[i] = vector<PII>();
      py[i] = vector<PII>();
    }
    REP(i, n) {
      REP(j, m) {
        int a;
        cin >> a;
        if (a == 1) {
          px[j].push_back({i, hn});
          py[i].push_back({j, hn});
          hn++;
        } else if (a == 2) {
          c = {j, i};
          px[j].push_back({i, hn});
          py[i].push_back({j, hn});
          hn++;
        }
      }
    }
    cout << dfs(c.fst, c.snd, 0) << endl;
  }
}