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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)

vector<pii> ansa, ansb, ansc;

int cc = 0;

bool allZero(vector<vector<int>> v) {
  rep(i, 10) {
    rep(j, 10) {
      if (v[i][j] != 0)
        return false;
    }
  }
  return true;
}

bool add1(int y, int x, vector<vector<int>> &v, int val) {
  bool neg = false;
  rep(dx, 3) {
    rep(dy, 3) {
      v[y + dy][x + dx] += val;
      if (v[y + dy][x + dx] < 0)
        neg = true;
    }
  }
  return neg;
}

bool add2(int y, int x, vector<vector<int>> &v, int val) {
  bool neg = false;
  int dx[] = {-1, 0, 0, 0, 1};
  int dy[] = {1, 0, 1, 2, 1};
  rep(i, 5) {
    v[y + dy[i]][x + dx[i]] += val;
    if (v[y + dy[i]][x + dx[i]] < 0)
      neg = true;
  }
  return neg;
}

bool add3(int y, int x, vector<vector<int>> &v, int val) {
  bool neg = false;
  int dx[] = {-2, 0, 0, 2};
  int dy[] = {2, 0, 4, 2};
  rep(i, 4) {
    v[y + dy[i]][x + dx[i]] += val;
    if (v[y + dy[i]][x + dx[i]] < 0)
      neg = true;
  }
  rep(dx, 3) {
    rep(dy, 3) {
      v[y + dy + 1][x + dx - 1] += val;
      if (v[y + dy + 1][x + dx - 1] < 0)
        neg = true;
    }
  }
  return neg;
}

void dfs(vector<vector<int>> &v, vector<pii> &a, vector<pii> &b, vector<pii> &c,
         int less) {
  cc++;
  //    if( ansa.size() || ansb.size() || ansc.size() )return ;
  if (less <= 0) {
    if (!allZero(v))
      return;
    ansa = a;
    ansb = b;
    ansc = c;

    return;
  }
  [&]() {
    rep(i, 10) {
      rep(j, 10) {
        if (v[i][j] != 0) {
          if (i + 2 < 10 && j + 2 < 10) { // 3*3
            bool hasNeg = add1(i, j, v, -1);
            a.push_back(pii(i + 1, j + 1));
            if (!hasNeg)
              dfs(v, a, b, c, less - 1);
            a.pop_back();
            add1(i, j, v, 1);
          }
          if (i + 2 < 10 && 0 < j && j + 1 < 10) { //+
            bool hasNeg = add2(i, j, v, -1);
            b.push_back(pii(i + 1, j));
            if (!hasNeg)
              dfs(v, a, b, c, less - 1);
            b.pop_back();
            add2(i, j, v, 1);
          }
          if (i + 4 < 10 && 1 < j && j + 2 < 10) { //++
            bool hasNeg = add3(i, j, v, -1);
            c.push_back(pii(i + 2, j));
            if (!hasNeg)
              dfs(v, a, b, c, less - 1);
            c.pop_back();
            add3(i, j, v, 1);
          }
          return;
        }
      }
    }
  }();
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> v(10, vector<int>(10));

  rep(i, 10) rep(j, 10) cin >> v[i][j];

  vector<pii> a, b, c; // 3*3, +, ++

  dfs(v, a, b, c, n);

  rep(i, ansa.size()) {
    cout << ansa[i].second << " " << ansa[i].first << " " << 2 << endl;
  }
  rep(i, ansb.size()) {
    cout << ansb[i].second << " " << ansb[i].first << " " << 1 << endl;
  }
  rep(i, ansc.size()) {
    cout << ansc[i].second << " " << ansc[i].first << " " << 3 << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)

vector<pii> ansa, ansb, ansc;

int cc = 0;

bool allZero(vector<vector<int>> v) {
  rep(i, 10) {
    rep(j, 10) {
      if (v[i][j] != 0)
        return false;
    }
  }
  return true;
}

void dfs(vector<vector<int>> &v, vector<pii> &a, vector<pii> &b, vector<pii> &c,
         int less) {
  cc++;
  //    if( ansa.size() || ansb.size() || ansc.size() )return ;
  if (less <= 0) {
    if (!allZero(v))
      return;
    ansa = a;
    ansb = b;
    ansc = c;

    return;
  }
  rep(i, 10) {
    rep(j, 10) {
      if (v[i][j] != 0) {
        if (i + 2 >= 10)
          continue;
        if (j == 0 || v[i + 1][j - 1] == 0) {
          if (j + 2 >= 10)
            continue;
          bool ng = false;
          rep(k, 3) {
            rep(l, 3) {
              v[i + k][j + l]--;
              if (v[i + k][j + l] < 0)
                ng = true;
            }
          }
          a.push_back(pii(i + 1, j + 1));
          if (!ng)
            dfs(v, a, b, c, less - 1);
          a.pop_back();
          rep(k, 3) {
            rep(l, 3) { v[i + k][j + l]++; }
          }
        } else {
          if (i + 2 >= 10 || j + 1 >= 10)
            continue;

          int dx1[] = {0, 0, 0, -1, 1};
          int dy1[] = {0, 1, 2, 1, 1};

          bool ng = false;
          rep(k, 5) {
            int ddy = i + dy1[k];
            int ddx = j + dx1[k];
            v[ddy][ddx]--;
            if (v[ddy][ddx] < 0)
              ng = true;
          }
          b.push_back(pii(i + 1, j));
          if (!ng)
            dfs(v, a, b, c, less - 1);
          b.pop_back();
          rep(k, 5) {
            int ddy = i + dy1[k];
            int ddx = j + dx1[k];
            v[ddy][ddx]++;
          }

          if (j + 2 >= 10 || i + 4 >= 10)
            continue;

          int dx2[] = {0, 0, -2, 2};
          int dy2[] = {0, 4, 2, 2};
          ng = false;
          rep(k, 4) {
            int ddy = i + dy2[k];
            int ddx = j + dx2[k];
            v[ddy][ddx]--;
            if (v[ddy][ddx] < 0)
              ng = true;
          }
          rep(k, 3) {
            rep(l, 3) {
              v[i + k + 1][j + l - 1]--;
              if (v[i + k + 1][j + l - 1] < 0)
                ng = true;
            }
          }
          c.push_back(pii(i + 2, j));
          if (!ng)
            dfs(v, a, b, c, less - 1);
          c.pop_back();
          rep(k, 4) {
            int ddy = i + dy2[k];
            int ddx = j + dx2[k];
            v[ddy][ddx]++;
          }
          rep(k, 3) {
            rep(l, 3) { v[i + k + 1][j + l - 1]++; }
          }
        }
        goto end;
      }
    }
  end:;
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> v(10, vector<int>(10));

  rep(i, 10) rep(j, 10) cin >> v[i][j];

  vector<pii> a, b, c; // 3*3, +, ++

  dfs(v, a, b, c, n);

  rep(i, ansa.size()) {
    cout << ansa[i].second << " " << ansa[i].first << " " << 2 << endl;
  }
  rep(i, ansb.size()) {
    cout << ansb[i].second << " " << ansb[i].first << " " << 1 << endl;
  }
  rep(i, ansc.size()) {
    cout << ansc[i].second << " " << ansc[i].first << " " << 3 << endl;
  }
}