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

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 2, 0, -2, 0};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0, 2, 0, -2};
const int INF = 1 << 30;
const double EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
typedef pair<int, int> Pii;
typedef pair<Pii, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
int Y[100];
int X[100];
int T[100];
int SZ;
int n;
bool F;
bool checkS(int stage[10][10], int y, int x, int type) {
  int cy = y + 1;
  int cx = x;
  if (type < 0) {
    if (stage[cy][cx] == 0)
      return false;
    rep(i, 4) {
      if (stage[cy + dy[i]][cx + dx[i]] == 0)
        return false;
    }
  }
  stage[cy][cx] += type;
  rep(i, 4) { stage[cy + dy[i]][cx + dx[i]] += type; }
  return true;
}
bool checkM(int stage[10][10], int y, int x, int type) {
  int cy = y + 1;
  int cx = x + 1;
  if (type < 0) {
    if (stage[cy][cx] == 0)
      return false;
    rep(i, 8) {
      if (stage[cy + dy[i]][cx + dx[i]] == 0)
        return false;
    }
  }
  stage[cy][cx] += type;
  rep(i, 8) { stage[cy + dy[i]][cx + dx[i]] += type; }
  return true;
}
bool checkL(int stage[10][10], int y, int x, int type) {
  int cy = y + 2;
  int cx = x;
  if (type < 0) {
    if (stage[cy][cx] == 0)
      return false;
    rep(i, 12) {
      if (stage[cy + dy[i]][cx + dx[i]] == 0)
        return false;
    }
  }
  stage[cy][cx] += type;
  rep(i, 12) { stage[cy + dy[i]][cx + dx[i]] += type; }
  return true;
}
void solve(int y, int x, int len, int stage[10][10]) {
  if (F)
    return;
  if (x == 10) {
    y++;
    x = 0;
  }
  if (y == 10) {
    if (len == n) {
      SZ = len;
      rep(i, SZ) { printf("%d %d %d\\n", X[i], Y[i], T[i]); }
      F = true;
    }
    return;
  }
  if (stage[y][x] == 0) {
    solve(y, x + 1, len, stage);
  }
  if (checkS(stage, y, x, -1)) {
    Y[len] = y + 1;
    X[len] = x;
    T[len] = 1;
    solve(y, x + 1, len + 1, stage);
    solve(y, x, len + 1, stage);
    checkS(stage, y, x, 1);
    Y[len] = 0;
    X[len] = 0;
    T[len] = 0;
  }
  if (checkM(stage, y, x, -1)) {
    Y[len] = y + 1;
    X[len] = x + 1;
    T[len] = 2;
    solve(y, x + 1, len + 1, stage);
    solve(y, x, len + 1, stage);
    checkM(stage, y, x, 1);
    Y[len] = 0;
    X[len] = 0;
    T[len] = 0;
  }
  if (checkL(stage, y, x, -1)) {
    Y[len] = y + 2;
    X[len] = x;
    T[len] = 3;
    solve(y, x + 1, len + 1, stage);
    solve(y, x, len + 1, stage);
    checkL(stage, y, x, 1);
    Y[len] = 0;
    X[len] = 0;
    T[len] = 0;
  }
  return;
}
int main() {
  int stage[10][10] = {{0}};
  scanf("%d", &n);
  rep(i, 10) {
    rep(j, 10) { scanf("%d", &stage[i][j]); }
  }
  solve(0, 0, 0, stage);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 2, 0, -2, 0};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0, 2, 0, -2};
#define INF 1e+8
#define EPS 1e-7
#define PB push_back
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
typedef pair<int, int> Pii;
typedef pair<Pii, int> Ppi;
typedef vector<int> vi;
int n;
bool F;
int ans[10][10];
bool check(int stage[10][10]) {
  if (F)
    return false;
  rep(i, 10) {
    rep(j, 10) {
      if (stage[i][j] > 0)
        return false;
    }
  }
  F = true;
  return true;
}
void solve(int index, int dep, int stage[10][10], int D[10][10]) {
  int y = index / 10;
  int x = index % 10;
  if (y == 10) {
    if (check(stage) && dep == n) {
      rep(i, 10) {
        rep(j, 10) { ans[i][j] = D[i][j]; }
      }
    }
    return;
  }
  if (stage[y][x]) {
    bool f = false;
    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx > 9 || ny < 0 || ny > 9) {
        f = true;
        break;
      }
      if (stage[ny][nx] == 0) {
        f = true;
        break;
      }
    }
    if (!f) {
      stage[y][x]--;
      D[y][x] = 1;
      rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        stage[ny][nx]--;
      }
      solve(index, dep + 1, stage, D);
      solve(index + 1, dep + 1, stage, D);
      D[y][x] = 0;
      stage[y][x]++;
      rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        stage[ny][nx]++;
      }

      reps(i, 4, 8) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || nx > 9 || ny < 0 || ny > 9) {
          f = true;
          break;
        }
        if (stage[ny][nx] == 0) {
          f = true;
          break;
        }
      }
      if (!f) {
        stage[y][x]--;
        D[y][x] = 2;
        rep(i, 8) {
          int nx = x + dx[i];
          int ny = y + dy[i];
          stage[ny][nx]--;
        }
        solve(index, dep + 1, stage, D);
        solve(index + 1, dep + 1, stage, D);
        D[y][x] = 0;
        stage[y][x]++;
        rep(i, 8) {
          int nx = x + dx[i];
          int ny = y + dy[i];
          stage[ny][nx]++;
        }
        reps(i, 8, 12) {
          int nx = x + dx[i];
          int ny = y + dy[i];
          if (nx < 0 || nx > 9 || ny < 0 || ny > 9) {
            f = true;
            break;
          }
          if (stage[ny][nx] == 0) {
            f = true;
            break;
          }
        }
        if (!f) {
          stage[y][x]--;
          D[y][x] = 3;
          rep(i, 12) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            stage[ny][nx]--;
          }
          solve(index, dep + 1, stage, D);
          solve(index + 1, dep + 1, stage, D);
          D[y][x] = 0;
          stage[y][x]++;
          rep(i, 12) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            stage[ny][nx]++;
          }
        }
      }
    }
  }
  solve(index + 1, dep, stage, D);
  return;
}
int main() {
  int stage[10][10] = {{0}};
  scanf("%d", &n);
  rep(i, 10) {
    rep(j, 10) { scanf("%d", &stage[i][j]); }
  }
  int D[10][10] = {{0}};
  F = false;
  solve(0, 0, stage, D);
  rep(i, 10) {
    rep(j, 10) {
      if (ans[i][j] > 0) {
        printf("%d %d %d\\n", j, i, ans[i][j]);
      }
    }
  }
  return 0;
}