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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<ll, ll> P;
typedef double db;
struct T {
  ll first, second, third;
  bool operator<(const T &t) const {
    return first != t.first
               ? (first < t.first)
               : (second != t.second ? second < t.second : third < t.third);
  }
};
ll w, h, g[10][10];
ll cx, cy;
inline vector<P> doko(ll y, ll x) {
  vector<P> v;
  for (ll nx = x + 1; nx < w; nx++) {
    if (g[y][nx] > 0)
      v.push_back(P(y, nx));
    if (g[y][nx] == 1)
      break;
  }
  for (ll nx = x - 1; nx >= 0; nx--) {
    if (g[y][nx] > 0)
      v.push_back(P(y, nx));
    if (g[y][nx] == 1)
      break;
  }
  for (ll ny = y + 1; ny < h; ny++) {
    if (g[ny][x] > 0)
      v.push_back(P(ny, x));
    if (g[ny][x] == 1)
      break;
  }
  for (ll ny = y - 1; ny >= 0; --ny) {
    if (g[ny][x] > 0)
      v.push_back(P(ny, x));
    if (g[ny][x] == 1)
      break;
  }
  return v;
}
ll n;
ll id[10][10];
set<T> t;
ll dfs(ll y, ll x, ll s) {
  if (t.count((T){y, x, s}))
    return 0;
  ll res = 0;
  vector<P> v = doko(y, x);
  if (s == (1 << n) - 1) {
    for (ll i = 0; i < v.size(); i++) {
      if (g[v[i].first][v[i].second] == 2)
        return 1;
    }
    t.insert((T){y, x, s});
    return 0;
  }
  if (s)
    g[y][x] = -1;
  for (ll i = 0; i < v.size(); i++) {
    if (g[v[i].first][v[i].second] == 2)
      continue;
    res += dfs(v[i].first, v[i].second, s | (1 << id[v[i].first][v[i].second]));
  }
  g[y][x] = 1;
  if (res == 0)
    t.insert((T){y, x, s});
  return res;
}

int main() {
  while (1) {
    scanf("%d%d", &w, &h);
    n = 0;
    t.clear();
    memset(id, -1, sizeof(id));
    if (!w)
      return 0;
    for (ll i = 0; i < h; i++)
      for (ll j = 0; j < w; j++) {
        scanf("%d", &g[i][j]);
        if (g[i][j] == 2) {
          cx = j, cy = i;
        } else if (g[i][j]) {
          id[i][j] = n;
          n++;
        }
      }
    printf("%d\\n", dfs(cy, cx, 0));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<ll, ll> P;
typedef double db;
struct T {
  ll first, second, third;
  bool operator<(const T &t) const {
    return first != t.first
               ? (first < t.first)
               : (second != t.second ? second < t.second : third < t.third);
  }
};
ll w, h, g[10][10];
ll cx, cy;
inline vector<P> doko(ll y, ll x) {
  vector<P> v;
  for (ll nx = x + 1; nx < w; nx++) {
    if (g[y][nx] > 0)
      v.push_back(P(y, nx));
    if (g[y][nx] == 1)
      break;
  }
  for (ll nx = x - 1; nx >= 0; nx--) {
    if (g[y][nx] > 0)
      v.push_back(P(y, nx));
    if (g[y][nx] == 1)
      break;
  }
  for (ll ny = y + 1; ny < h; ny++) {
    if (g[ny][x] > 0)
      v.push_back(P(ny, x));
    if (g[ny][x] == 1)
      break;
  }
  for (ll ny = y - 1; ny >= 0; --ny) {
    if (g[ny][x] > 0)
      v.push_back(P(ny, x));
    if (g[ny][x] == 1)
      break;
  }
  return v;
}
ll n;
ll id[10][10];
// set<T> t;
ll dfs(ll y, ll x, ll s) {
  // if(t.count((T){y,x,s})) return 0;
  ll res = 0;
  vector<P> v = doko(y, x);
  if (s == (1 << n) - 1) {
    for (ll i = 0; i < v.size(); i++) {
      if (g[v[i].first][v[i].second] == 2)
        return 1;
    }
    // t.insert((T){y,x,s});
    return 0;
  }
  if (s)
    g[y][x] = -1;
  for (ll i = 0; i < v.size(); i++) {
    if (g[v[i].first][v[i].second] == 2)
      continue;
    res += dfs(v[i].first, v[i].second, s | (1 << id[v[i].first][v[i].second]));
  }
  g[y][x] = 1;
  // if(res==0) t.insert((T){y,x,s});
  return res;
}

int main() {
  while (1) {
    scanf("%d%d", &w, &h);
    n = 0;
    // t.clear();
    memset(id, -1, sizeof(id));
    if (!w)
      return 0;
    for (ll i = 0; i < h; i++)
      for (ll j = 0; j < w; j++) {
        scanf("%d", &g[i][j]);
        if (g[i][j] == 2) {
          cx = j, cy = i;
        } else if (g[i][j]) {
          id[i][j] = n;
          n++;
        }
      }
    printf("%d\\n", dfs(cy, cx, 0));
  }
}