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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n)                                                         \\
  for (i = 0; i < n; ++i)                                                      \\
    for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d%d", &x, &y)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  scanf("%d%d%d", &x, &y, &z)
#define sc(x) cin >> x
#define sc2(x, y) cin >> x >> y
#define sc3(x, y, z) cin >> x >> y >> z
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\\n"
#define pri2(x, y) cout << x << " " << y << "\\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define endl "\\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\\n", x)

int f[10][10], head = 0, sum;
int dh[13] = {0, 1, 0, -1, 0, 1, -1, -1, 1, 2, 0, -2, 0};
int dw[13] = {0, 0, 1, 0, -1, 1, 1, -1, -1, 0, 2, 0, -2};
pair<i_i, int> res[12];

bool dfs(int n, int s, int m, int l, int h) {
  bool st = (n * 3 == s + m + l);
  if (n == 0 && sum == 0)
    return true;
  if (n == 0 && sum != 0)
    return false;
  if (n != 0 && sum == 0)
    return false;

  if (sum == 5 * n && st)
    return dfs(n, n, 0, 0, h);
  if (sum == 13 * n && st)
    return dfs(n, 0, 0, n, h);
  if (sum == 5 * n + 4 && st)
    return dfs(n, n - 1, 1, 0, h);
  if (sum == 13 * n - 4 && st)
    return dfs(n, 0, 1, n - 1, h);

  int i, j, k;
  for (; h < 8; h++) {
    if (m)
      rep1(j, 9) if (f[h][j - 1] && f[h][j] && f[h][j + 1]) {
        rep(k, 9) if (!f[h + 1 + dh[k]][j + dw[k]]) break;
        if (k == 9) {
          rep(k, 9) f[h + 1 + dh[k]][j + dw[k]]--;
          sum -= 9;
          res[head++] = mp(mp(j, h + 1), 2);
          if (st) {
            bool fl = dfs(n - 1, s - 1, m - 1, l - 1, h);
            if (fl)
              return true;
            else {
              head--;
              rep(k, 9) f[h + 1 + dh[k]][j + dw[k]]++;
              sum += 9;
            }
          } else {
            bool fl = dfs(n - 1, s, m - 1, l, h);
            if (fl)
              return true;
            else {
              head--;
              rep(k, 9) f[h + 1 + dh[k]][j + dw[k]]++;
              sum += 9;
            }
          }
        }
      }

    rep1(j, 9) if (f[h][j]) {
      if (l)
        rep(k, 13) if (0 > h + 2 + dh[k] || h + 2 + dh[k] > 9 ||
                       0 > j + dw[k] || j + dw[k] > 9 ||
                       !f[h + 2 + dh[k]][j + dw[k]]) break;
      if (l)
        if (k == 13) {
          sum -= 13;
          rep(k, 13) f[h + 2 + dh[k]][j + dw[k]]--;
          res[head++] = mp(mp(j, h + 2), 3);
          if (st) {
            bool fl = dfs(n - 1, s - 1, m - 1, l - 1, h);
            if (fl)
              return true;
            else {
              head--;
              rep(k, 13) f[h + 2 + dh[k]][j + dw[k]]++;
              sum += 13;
            }
          } else {
            bool fl = dfs(n - 1, s, m, l - 1, h);
            if (fl)
              return true;
            else {
              head--;
              rep(k, 13) f[h + 2 + dh[k]][j + dw[k]]++;
              sum += 13;
            }
          }
        }
      if (s)
        rep(k, 5) if (!f[h + 1 + dh[k]][j + dw[k]]) break;
      if (s)
        if (k == 5) {
          sum -= 5;
          rep(k, 5) f[h + 1 + dh[k]][j + dw[k]]--;
          res[head++] = mp(mp(j, h + 1), 1);
          if (st) {
            bool fl = dfs(n - 1, s - 1, m - 1, l - 1, h);
            if (fl)
              return true;
            else {
              head--;
              rep(k, 5) f[h + 1 + dh[k]][j + dw[k]]++;
              sum += 5;
            }
          } else {
            bool fl = dfs(n - 1, s - 1, m, l, h);
            if (fl)
              return true;
            else {
              head--;
              rep(k, 5) f[h + 1 + dh[k]][j + dw[k]]++;
              sum += 5;
            }
          }
        }
    }
    rep(j, 10) if (f[h][j]) return false;
  }

  return false;
}

signed main(void) {
  int i, j, k;
  for (int testcase = 0; testcase < 1234567; testcase++) {
    int(n);
    sum = 0;
    rep(i, 10) rep(j, 10) {
      sc(f[i][j]);
      sum += f[i][j];
    }

    dfs(n, n, n, n, 0);
    rep(i, n) pri3(res[i].X.X, res[i].X.Y, res[i].Y);

    /*/

    //*/
    break;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n)                                                         \\
  for (i = 0; i < n; ++i)                                                      \\
    for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d%d", &x, &y)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  scanf("%d%d%d", &x, &y, &z)
#define sc(x) cin >> x
#define sc2(x, y) cin >> x >> y
#define sc3(x, y, z) cin >> x >> y >> z
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\\n"
#define pri2(x, y) cout << x << " " << y << "\\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define endl "\\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\\n", x)

int f[10][10], head = 0, sum;
int dh[13] = {0, 1, 0, -1, 0, 1, -1, -1, 1, 2, 0, -2, 0};
int dw[13] = {0, 0, 1, 0, -1, 1, 1, -1, -1, 0, 2, 0, -2};
pair<i_i, int> res[12];

bool dfs(int n, int s, int m, int l, int h) {
  if (n == 0 && sum == 0)
    return true;
  if (n == 0 && sum != 0)
    return false;
  if (n != 0 && sum == 0)
    return false;

  if (sum == 5 * n && m != 0 && l != 0)
    return dfs(n, n, 0, 0, h);
  if (sum == 13 * n && s != 0 && m != 0)
    return dfs(n, 0, 0, n, h);

  int i, j, k;
  for (; h < 8; h++) {
    if (m)
      rep1(j, 9) if (f[h][j - 1] && f[h][j] && f[h][j + 1]) {
        rep(k, 9) if (!f[h + 1 + dh[k]][j + dw[k]]) break;
        if (k == 9) {
          rep(k, 9) f[h + 1 + dh[k]][j + dw[k]]--;
          sum -= 9;
          res[head++] = mp(mp(j, h + 1), 2);
          bool fl = dfs(n - 1, max(s - 1, 0), m - 1, max(l - 1, 0), h);
          if (fl)
            return true;
          else {
            head--;
            rep(k, 9) f[h + 1 + dh[k]][j + dw[k]]++;
            sum += 9;
          }
        }
      }

    rep1(j, 9) if (f[h][j]) {
      if (l)
        rep(k, 13) if (0 > h + 2 + dh[k] || h + 2 + dh[k] > 9 ||
                       0 > j + dw[k] || j + dw[k] > 9 ||
                       !f[h + 2 + dh[k]][j + dw[k]]) break;
      if (l)
        if (k == 13) {
          sum -= 13;
          rep(k, 13) f[h + 2 + dh[k]][j + dw[k]]--;
          res[head++] = mp(mp(j, h + 2), 3);
          bool fl = dfs(n - 1, max(s - 1, 0), max(m - 1, 0), max(l - 1, 0), h);
          if (fl)
            return true;
          else {
            head--;
            rep(k, 13) f[h + 2 + dh[k]][j + dw[k]]++;
            sum += 13;
          }
        }
      if (s)
        rep(k, 5) if (!f[h + 1 + dh[k]][j + dw[k]]) break;
      if (s)
        if (k == 5) {
          sum -= 5;
          rep(k, 5) f[h + 1 + dh[k]][j + dw[k]]--;
          res[head++] = mp(mp(j, h + 1), 1);
          bool fl = dfs(n - 1, max(s - 1, 0), max(m - 1, 0), max(l - 1, 0), h);
          if (fl)
            return true;
          else {
            head--;
            rep(k, 5) f[h + 1 + dh[k]][j + dw[k]]++;
            sum += 5;
          }
        }
    }
  }

  return false;
}

signed main(void) {
  int i, j, k;
  for (int testcase = 0; testcase < 1234567; testcase++) {
    int(n);
    sum = 0;
    rep(i, 10) rep(j, 10) {
      sc(f[i][j]);
      sum += f[i][j];
    }

    dfs(n, n, n, n, 0);
    rep(i, n) pri3(res[i].X.X, res[i].X.Y, res[i].Y);

    /*/

    //*/
    break;
  }
  return 0;
}