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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int n;
vvi in;
vip out;
int dx[] = {-1, 0, 0, 0, 1, 1, 1, -1, -1, 2, 0, 0, -2};
int dy[] = {0, -1, 0, 1, 0, 1, -1, 1, -1, 0, 2, -2, 0};
int co[4] = {5, 9, 13};
bool f(int x, int y, int c) {
  if (c == 0) {
    bool h = true;
    rep(i, 10) rep(j, 10) if (in[i][j]) h = false;
    if (!h)
      return false;
    rep(i, n) cout << out[i].first << " " << out[i].second.first << " "
                   << out[i].second.second << endl;
    return true;
  }
  int lx = inf;
  rep(i, 10) rep(j, 10) if (in[i][j]) {
    lx = i;
    goto e;
  }
e:;
  rep(i, 10) rep(j, 10) {
    if (i + j == 0)
      i = x, j = y;
    if (lx + 2 < i)
      return false;
    if (in[i][j]) {
      rep(k, 3) {
        bool h = true;
        rep(l, co[k]) if (i + dx[l] < 0 || j + dy[l] < 0 || i + dx[l] >= 10 ||
                          j + dy[l] >= 10 || in[i + dx[l]][j + dy[l]] == 0) h =
            false;
        if (!h)
          break;
        out[c - 1] = pip(j, pii(i, k + 1));
        rep(l, co[k]) in[i + dx[l]][j + dy[l]]--;
        if (f(i, j, c - 1))
          return true;
        rep(l, co[k]) in[i + dx[l]][j + dy[l]]++;
      }
    }
  }
  return false;
}
int main() {
  cin >> n;
  in = vvi(10, vi(10));
  out = vip(n);
  rep(i, 10) rep(j, 10) cin >> in[i][j];
  f(0, 0, n);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int n;
vvi in;
vip out;
int dx[] = {-1, 0, 0, 0, 1, 1, 1, -1, -1, 2, 0, 0, -2};
int dy[] = {0, -1, 0, 1, 0, 1, -1, 1, -1, 0, 2, -2, 0};
int co[3] = {5, 9, 13};
bool f(int x, int y, int c) {
  if (c == 0) {
    bool h = true;
    rep(i, 10) rep(j, 10) if (in[i][j]) h = false;
    if (!h)
      return false;
    rep(i, n) cout << out[i].first << " " << out[i].second.first << " "
                   << out[i].second.second << endl;
    return true;
  }
  rep(i, 10) rep(j, 10) {
    if (i + j == 0)
      i = x, j = y;
    if (in[i][j]) {
      rep(k, 3) {
        bool h = true;
        rep(l, co[k]) if (i + dx[l] < 0 || j + dy[l] < 0 || i + dx[l] >= 10 ||
                          j + dy[l] >= 10 || in[i + dx[l]][j + dy[l]] == 0) h =
            false;
        if (!h)
          continue;
        out[c - 1] = pip(j, pii(i, k + 1));
        rep(l, co[k]) in[i + dx[l]][j + dy[l]]--;
        if (f(i, j, c - 1))
          return true;
        rep(l, co[k]) in[i + dx[l]][j + dy[l]]++;
      }
    }
  }
  return false;
}
int main() {
  cin >> n;
  in = vvi(10, vi(10));
  out = vip(n);
  rep(i, 10) rep(j, 10) cin >> in[i][j];
  f(0, 0, n);
}