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

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int n = 10;
int m, a[n][n];
const int sz[] = {5, 9, 13};
const int dy[][13] = {{-1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
                      {-1, -1, -1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
                      {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2}};
const int dx[][13] = {{0, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                      {-1, 0, 1, -1, 0, 1, -1, 0, 1, 0, 0, 0, 0},
                      {0, -1, 0, 1, -2, -1, 0, 1, 2, -1, 0, 1, 0}};

vector<pi> v;

inline bool can(int y, int x, int t, int nul = 0) {
  if (nul)
    rep(d, sz[t]) {
      int ny = y + dy[t][d], nx = x + dx[t][d];
      if (ny < 0 || nx < 0 || ny >= n || nx >= n)
        continue;
      a[ny][nx] += nul;
    }
  else
    rep(d, sz[t]) {
      int ny = y + dy[t][d], nx = x + dx[t][d];
      if (ny < 0 || nx < 0 || ny >= n || nx >= n)
        continue;
      if (!a[ny][nx])
        return 0;
    }
  return 1;
}
inline void fill(bool ok[n][n], int y, int x, int t) {
  rep(d, sz[t]) {
    int ny = y + dy[t][d], nx = x + dx[t][d];
    if (ny < 0 || nx < 0 || ny >= n || nx >= n)
      continue;
    ok[ny][nx] = 1;
  }
}

inline void rec(int m, int y, int x) {
  if (m == 0) {
    rep(i, n) rep(j, n) if (a[i][j]) return;
    rep(i, v.size()) cout << v[i].first / 10 << " " << v[i].first % 10 << " "
                          << v[i].second << endl;
    exit(0);
  }
  bool ok[n][n] = {};
  vector<pi> cand;

  rep(i, n)
      rep(j, n) if (i > y || i == y && j >= x) if (a[i][j]) for (int t = 2;
                                                                 i >= 0; t--) {
    if (can(i, j, t)) {
      fill(ok, i, j, t);
      cand.pb(mp(t, i * 10 + j));
      break;
    }
  }
  rep(i, n) rep(j, n) if (a[i][j] && !ok[i][j]) return;

  rep(ii, cand.size()) {
    int i = cand[ii].second / 10, j = cand[ii].second % 10;
    int t = cand[ii].first;

    for (; t >= 0; t--) {
      // if(i >= 3 && t < cand[ii].first) break;
      can(i, j, t, -1);
      v.pb(mp(j * 10 + i, t + 1));
      rec(m - 1, i, j);
      v.pop_back();
      can(i, j, t, 1);
    }
  }
}

int main() {
  cin >> m;
  rep(i, n) rep(j, n) cin >> a[i][j];
  rec(m, 0, 0);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int n = 10;
int m, a[n][n], sum;
const int sz[] = {5, 9, 13};
const int dy[][13] = {{-1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
                      {-1, -1, -1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
                      {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2}};
const int dx[][13] = {{0, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                      {-1, 0, 1, -1, 0, 1, -1, 0, 1, 0, 0, 0, 0},
                      {0, -1, 0, 1, -2, -1, 0, 1, 2, -1, 0, 1, 0}};

vector<pi> v;

inline bool can(int y, int x, int t, int nul = 0) {
  if (nul)
    rep(d, sz[t]) {
      int ny = y + dy[t][d], nx = x + dx[t][d];
      if (ny < 0 || nx < 0 || ny >= n || nx >= n)
        continue;
      a[ny][nx] += nul;
    }
  else
    rep(d, sz[t]) {
      int ny = y + dy[t][d], nx = x + dx[t][d];
      if (ny < 0 || nx < 0 || ny >= n || nx >= n)
        continue;
      if (!a[ny][nx])
        return 0;
    }
  return 1;
}
inline void fill(bool ok[n][n], int y, int x, int t) {
  rep(d, sz[t]) {
    int ny = y + dy[t][d], nx = x + dx[t][d];
    if (ny < 0 || nx < 0 || ny >= n || nx >= n)
      continue;
    ok[ny][nx] = 1;
  }
}

void rec(int m) {
  if (m == 0) {
    rep(i, n) rep(j, n) if (a[i][j]) return;
    rep(i, v.size()) cout << v[i].first / 10 << " " << v[i].first % 10 << " "
                          << v[i].second << endl;
    exit(0);
  }
  bool ok[n][n] = {};
  vector<pi> cand;
  vi sc;
  int s = 0;

  rep(i, n) rep(j, n) if (a[i][j]) for (int t = 2; i >= 0; t--) {
    if (can(i, j, t)) {
      fill(ok, i, j, t);
      sc.pb(sz[t]);
      for (; t >= 0; t--)
        cand.pb(mp(-t, i * 10 + j));
      break;
    }
  }
  if (sc.size() < m)
    return;
  sort(all(sc), greater<int>());
  rep(i, m) s += sc[i];
  if (s < sum)
    return;
  rep(i, n) rep(j, n) if (a[i][j] && !ok[i][j]) return;

  sort(all(cand));

  rep(ii, cand.size()) {
    int i = cand[ii].second / 10, j = cand[ii].second % 10;
    int t = -cand[ii].first;

    can(i, j, t, -1);
    v.pb(mp(j * 10 + i, t + 1));
    sum -= sz[t];
    rec(m - 1);
    sum += sz[t];
    v.pop_back();
    can(i, j, t, 1);
  }
}

int main() {
  cin >> m;
  rep(i, n) rep(j, n) {
    cin >> a[i][j];
    sum += a[i][j];
  }
  rec(m);
  return 0;
}