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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;
int const MOD = 1000000007;

vector<int> patx[3], paty[3];
int movex[] = {1, 1, 2}, movey[] = {0, 1, 0};
struct Elem {
  int x, y, blur;
};

bool answerFlag = false;

void solve(vector<vector<int>> &board, vector<Elem> &rec, int restcnt,
           int restblur) {
  if (answerFlag)
    return;
  if (restcnt * 5 > restblur)
    return;
  if (restcnt == 0 && restblur == 0) {
    rep(i, 0, rec.size()) {
      printf("%d %d %d\\n", rec[i].x, rec[i].y, rec[i].blur);
    }
    answerFlag = true;
    exit(0);
  }
  if (restcnt == 0)
    return;
  rep(i, 0, 10) rep(j, 0, 10) {
    if (board[i][j] != 0) {
      rep(k, 0, 3) {
        int nx = i + movex[k], ny = j + movey[k];
        if (nx < 0 || nx >= 10 || ny < 0 || ny >= 10)
          continue;

        bool ok = true;
        rep(x, 0, patx[k].size()) {
          int sx = nx + patx[k][x], sy = ny + paty[k][x];
          if (sx < 0 || sx >= 10 || sy < 0 || sy >= 10 || !board[sx][sy])
            ok = false;
        }

        if (ok) {
          rep(x, 0, patx[k].size()) {
            int sx = nx + patx[k][x], sy = ny + paty[k][x];
            board[sx][sy]--;
          }
          rec.push_back(Elem{ny, nx, k + 1});

          solve(board, rec, restcnt - 1, restblur - patx[k].size());

          rep(x, 0, patx[k].size()) {
            int sx = nx + patx[k][x], sy = ny + paty[k][x];
            board[sx][sy]++;
          }
          rec.pop_back();
        }
      }
      return;
    }
  }
}

signed main() {
  patx[0] = {1, -1, 0, 0, 0};
  patx[1] = {0, -1, 1, 0, -1, 1, 0, -1, 1};
  patx[2] = {0, 0, 0, 0, 0, -2, -1, 1, 2, 1, 1, -1, -1};

  paty[0] = {0, 0, 1, -1, 0};
  paty[1] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
  paty[2] = {-2, -1, 0, 1, 2, 0, 0, 0, 0, 1, -1, 1, -1};
  int n;
  cin >> n;
  vector<vector<int>> v(10, vector<int>(10, 0));
  int blurcnt = 0;
  rep(i, 0, 10) rep(j, 0, 10) {
    cin >> v[i][j];
    blurcnt += v[i][j];
  }
  vector<Elem> init;
  solve(v, init, n, blurcnt);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;
int const MOD = 1000000007;

vector<int> patx[3], paty[3];
struct Elem {
  int x, y, blur;
};

bool answerFlag = false;

void solve(vector<vector<int>> &board, vector<Elem> &rec, int restcnt,
           int restblur, int lastp = 0) {
  // printf("restcnt = %d, restblur = %d, lastp = %d\\n", restcnt, restblur,
  // lastp);
  if (answerFlag)
    return;
  if (restcnt * 5 > restblur)
    return;
  if (restcnt == 0 && restblur == 0) {
    rep(i, 0, rec.size()) {
      printf("%d %d %d\\n", rec[i].x, rec[i].y, rec[i].blur);
    }
    answerFlag = true;
    exit(0);
  }
  if (restcnt == 0)
    return;
  rep(i, 0, 10) rep(j, 0, 10) {
    if (10 * i + j < lastp)
      continue;
    if (board[i][j] != 0) {
      rep(k, 0, 3) {
        if (restblur - (int)patx[k].size() < 0)
          continue;
        bool ok = true;
        rep(l, 0, patx[k].size()) {
          int x = i + patx[k][l], y = j + paty[k][l];
          if (x < 0 || x >= 10 || y < 0 || y >= 10 || board[x][y] == 0)
            ok = false;
        }
        if (ok) {
          rep(l, 0, patx[k].size()) {
            int x = i + patx[k][l], y = j + paty[k][l];
            board[x][y]--;
          }
          rec.push_back(Elem{j, i, 3 - k});

          solve(board, rec, restcnt - 1, restblur - (int)patx[k].size(),
                10 * i + j);

          rep(l, 0, patx[k].size()) {
            int x = i + patx[k][l], y = j + paty[k][l];
            board[x][y]++;
          }
          rec.pop_back();
        }
      }
    }
  }
}

signed main() {
  patx[2] = {1, -1, 0, 0, 0};
  patx[1] = {0, -1, 1, 0, -1, 1, 0, -1, 1};
  patx[0] = {0, 0, 0, 0, 0, -2, -1, 1, 2, 1, 1, -1, -1};

  paty[2] = {0, 0, 1, -1, 0};
  paty[1] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
  paty[0] = {-2, -1, 0, 1, 2, 0, 0, 0, 0, 1, -1, 1, -1};
  int n;
  cin >> n;
  vector<vector<int>> v(10, vector<int>(10, 0));
  int blurcnt = 0;
  rep(i, 0, 10) rep(j, 0, 10) {
    cin >> v[i][j];
    blurcnt += v[i][j];
  }
  vector<Elem> init;
  solve(v, init, n, blurcnt);
}