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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> vector<T> &sort(vector<T> &a) {
  sort(all(a));
  return a;
}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int W, H;

int a[12][12];
int sy, sx;
int nei_count = 0;
int house_count = 0;

int dfs(int y, int x, int zan) {
  if (zan == 0) {
    return y == sy || x == sx;
  }

  int res = 0;

  for (int nowy = y - 1; nowy >= 1; --nowy) {
    if (a[nowy][x] == 1) {
      a[nowy][x] = -1;

      res += dfs(nowy, x, zan - 1);

      a[nowy][x] = 1;
      break;
    }
  }

  for (int nowy = y + 1; nowy <= H; ++nowy) {
    if (a[nowy][x] == 1) {
      a[nowy][x] = -1;

      res += dfs(nowy, x, zan - 1);

      a[nowy][x] = 1;
      break;
    }
  }

  for (int nowx = x - 1; nowx >= 1; --nowx) {
    if (a[y][nowx] == 1) {
      a[y][nowx] = -1;

      res += dfs(y, nowx, zan - 1);

      a[y][nowx] = 1;
      break;
    }
  }

  for (int nowx = x + 1; nowx <= W; ++nowx) {
    if (a[y][nowx] == 1) {
      a[y][nowx] = -1;

      res += dfs(y, nowx, zan - 1);

      a[y][nowx] = 1;
      break;
    }
  }

  return res;
}

signed main() {
  while (cin >> W >> H, W) {
    house_count = 0;

    rep1(i, H) rep1(j, W) {
      cin >> a[i][j];
      if (a[i][j] == 1) {
        house_count += 1;
      } else if (a[i][j] == 2) {
        sy = i, sx = j;
      }
    }

    cout << dfs(sy, sx, house_count) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> vector<T> &sort(vector<T> &a) {
  sort(all(a));
  return a;
}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int W, H;

int a[12][12];
int sy, sx;
int nei_count = 0;
int house_count = 0;
bool is_nei[12][12];

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

int dfs(int y, int x, int zan) {
  if (zan == 0) {
    return nei_count == 1;
  }

  if (a[y][x] == -1 && is_nei[y][x])
    nei_count -= 1;

  bool f = false;

  int res = 0;

  for (int nowy = y - 1; nowy >= 1; --nowy) {
    if (a[nowy][x] == -1)
      break;
    if (a[nowy][x] == 1) {
      f = true;
      a[nowy][x] = -1;

      res += dfs(nowy, x, zan - 1);

      a[nowy][x] = 1;
    }
  }

  for (int nowy = y + 1; nowy <= H; ++nowy) {
    if (a[nowy][x] == -1)
      break;
    if (a[nowy][x] == 1) {
      f = true;
      a[nowy][x] = -1;

      res += dfs(nowy, x, zan - 1);

      a[nowy][x] = 1;
    }
  }

  for (int nowx = x - 1; nowx >= 1; --nowx) {
    if (a[y][nowx] == -1)
      break;
    if (a[y][nowx] == 1) {
      f = true;
      a[y][nowx] = -1;

      res += dfs(y, nowx, zan - 1);

      a[y][nowx] = 1;
    }
  }

  for (int nowx = x + 1; nowx <= W; ++nowx) {
    if (a[y][nowx] == -1)
      break;
    if (a[y][nowx] == 1) {
      f = true;
      a[y][nowx] = -1;

      res += dfs(y, nowx, zan - 1);

      a[y][nowx] = 1;
    }
  }

  if (a[y][x] == -1 && is_nei[y][x])
    nei_count += 1;

  if (!f) {
    return 0;
  }

  return res;
}

signed main() {
  while (cin >> W >> H, W) {
    memset(a, -1, sizeof(a));
    memset(is_nei, false, sizeof(is_nei));
    nei_count = 0;
    house_count = 0;

    rep1(i, H) rep1(j, W) {
      cin >> a[i][j];
      if (a[i][j] == 1) {
        house_count += 1;
      } else if (a[i][j] == 2) {
        sy = i, sx = j;
      }
    }

    rep(i, 4) {
      int ny = sy + dy[i], nx = sx + dx[i];
      while (a[ny][nx] == 0)
        ny += dy[i], nx += dx[i];

      if (a[ny][nx] == 1) {
        nei_count += 1;
        is_nei[ny][nx] = true;
      }
    }

    cout << dfs(sy, sx, house_count) << endl;
  }
}