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

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)

int dy[][5] = {{0, 1, 0, -1, 0}, {1, 1, -1, -1}, {-2, 0, 2, 0}};
int dx[][5] = {{0, 0, 1, 0, -1}, {1, -1, 1, -1}, {0, 2, 0, -2}};

void dump(int dye[10][10]) {
  rep(i, 10) {
    rep(j, 10) cout << dye[i][j];
    cout << endl;
  }
}

bool puttable(int dye[10][10], int y, int x, int s) {
  rep(i, s) {
    rep(j, i ? 4 : 5) {
      int py = y + dy[i][j];
      int px = x + dx[i][j];
      if (dye[py][px] == 0)
        return false;
    }
  }

  return true;
}

void put(int dye[10][10], int y, int x, int s, bool minus) {
  rep(i, s) {
    rep(j, i ? 4 : 5) {
      int py = y + dy[i][j];
      int px = x + dx[i][j];
      dye[py][px] += minus ? -1 : 1;
    }
  }
}

bool prune(int dye[10][10], int num[]) {
  int tmp[10][10];
  rep(i, 10) rep(j, 10) tmp[i][j] = dye[i][j] > 0;

  rep(i, 3) {
    if (num[i] > 0) {
      int s = 3 - i;
      int o = s == 3 ? 2 : 1;
      reps(y, o, 10 - o) {
        reps(x, o, 10 - o) {
          if (puttable(dye, y, x, s)) {
            put(tmp, y, x, s, true);
          }
        }
      }
    }
  }

  rep(i, 10) rep(j, 10) {
    if (tmp[i][j] > 0)
      return true;
  }

  return false;
}

bool search(int dye[10][10], int num[]) {
  if (prune(dye, num)) {
    return false;
  }

  rep(i, 3) {
    if (num[i] > 0) {
      num[i]--;
      int s = 3 - i;
      int o = s == 3 ? 2 : 1;
      reps(y, o, 10 - o) {
        reps(x, o, 10 - o) {
          if (puttable(dye, y, x, s)) {
            put(dye, y, x, s, true);
            if (search(dye, num)) {
              cout << x << ' ' << y << ' ' << s << endl;
              return true;
            }
            put(dye, y, x, s, false);
          }
        }
      }
      num[i]++;
      return false;
    }
  }

  return true;
}

int main() {
  int n;
  cin >> n;

  int dye[10][10];
  int a = 0;
  rep(i, 10) rep(j, 10) {
    cin >> dye[i][j];
    a += dye[i][j];
  }

  int m = (a - n) / 4;
  vector<array<int, 3>> data;
  for (int x = 0;; ++x) {
    int y = -a + 5 * m - 2 * x;
    int z = 2 * a - 9 * m + x;
    if (z < 0)
      continue;
    if (y < 0)
      break;
    data.push_back({{x, y, z}});
  }

  for (int i = data.size() - 1; i >= 0; --i) {
    int num[] = {data[i][0], data[i][1], data[i][2]};
    if (search(dye, num)) {
      break;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)

int dy[][5] = {{0, 1, 0, -1, 0}, {1, 1, -1, -1}, {-2, 0, 2, 0}};
int dx[][5] = {{0, 0, 1, 0, -1}, {1, -1, 1, -1}, {0, 2, 0, -2}};

int dye[10][10];

void dump() {
  rep(i, 10) {
    rep(j, 10) cout << dye[i][j];
    cout << endl;
  }
}

bool puttable(int y, int x, int s) {
  rep(i, s) {
    rep(j, i ? 4 : 5) {
      int py = y + dy[i][j];
      int px = x + dx[i][j];
      if (dye[py][px] == 0)
        return false;
    }
  }

  return true;
}

void put(int y, int x, int s, bool minus) {
  rep(i, s) {
    rep(j, i ? 4 : 5) {
      int py = y + dy[i][j];
      int px = x + dx[i][j];
      dye[py][px] += minus ? -1 : 1;
    }
  }
}

bool search(int num[]) {
  rep(i, 3) {
    if (num[i] > 0) {
      num[i]--;
      int s = 3 - i;
      int o = s == 3 ? 2 : 1;
      reps(y, o, 10 - o) {
        reps(x, o, 10 - o) {
          if (puttable(y, x, s)) {
            put(y, x, s, true);
            if (search(num)) {
              cout << x << ' ' << y << ' ' << s << endl;
              return true;
            }
            put(y, x, s, false);
          }
        }
      }
      num[i]++;
      return false;
    }
  }

  return true;
}

int main() {
  int n;
  cin >> n;

  int a = 0;
  rep(i, 10) rep(j, 10) {
    cin >> dye[i][j];
    a += dye[i][j];
  }

  int m = (a - n) / 4;
  vector<array<int, 3>> data;
  for (int x = 0;; ++x) {
    int y = -a + 5 * m - 2 * x;
    int z = 2 * a - 9 * m + x;
    if (z < 0)
      continue;
    if (y < 0)
      break;
    data.push_back({{x, y, z}});
  }

  for (int i = data.size() - 1; i >= 0; --i) {
    int num[] = {data[i][0], data[i][1], data[i][2]};
    if (search(num)) {
      break;
    }
  }
}