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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <queue>
#include <vector>
using namespace std;

int d[13] = {
    -14, -1, 0, 1, 14, -15, -13, 13, 15, -28, -2, 2, 28,
};
int to[3] = {5, 9, 13};

int c[196];
int nmb[196];
vector<vector<int>> lumps;
vector<int> sum;
vector<int> ans;

inline void draw(int x, int t, int dif) {
  for (int i = 0; i < t; ++i) {
    c[x + d[i]] += dif;
  }
}

inline int check(int t, int x) {
  int m;
  for (m = 0; m < 13 && c[x + d[m]] > 0; ++m)
    ;
  return m;
}

void dfs(int, int, int);

inline void apply(int n, int t, int u, int x, int a) {
  draw(x, to[a], -1);
  sum[t] -= to[a];
  ans.push_back(x << 2 | a);
  dfs(n - 1, t, u);
  ans.pop_back();
  sum[t] += to[a];
  draw(x, to[a], 1);
}

void dfs(int n, int t, int u) {
  if (sum[t] == 0) {
    ++t;
    u = 0;
  }

  if (t == lumps.size()) {
    if (n == 0) {
      for (int i = 0; i < ans.size(); ++i) {
        int y = (ans[i] >> 2) / 14 - 2;
        int x = (ans[i] >> 2) % 14 - 2;
        int z = (ans[i] & 3) + 1;
        printf("%d %d %d\\n", x, y, z);
      }
      exit(0);
    }
    return;
  }

  const vector<int> &lm = lumps[t];
  while (u < lm.size() && c[lm[u]] == 0) {
    ++u;
  }
  if (u >= lm.size()) {
    return;
  }

  bool end = false;
  for (int i = 0; i < lm.size(); ++i) {
    if (c[lm[i]] && !c[lm[i] - 14] && !c[lm[i] + 14]) {
      end = true;
      for (int sg = -1; sg < 2; sg += 2) {
        if (check(t, lm[i] + 2 * sg) >= 13) {
          apply(n, t, u, lm[i] + 2 * sg, 2);
        }
        if (check(t, lm[i] + sg) >= 5) {
          apply(n, t, u, lm[i] + sg, 0);
        }
      }
    }

    if (c[lm[i]] && !c[lm[i] - 1] && !c[lm[i] + 1]) {
      end = true;
      for (int sg = -1; sg < 2; sg += 2) {
        if (check(t, lm[i] + 28 * sg) >= 13) {
          apply(n, t, u, lm[i] + 28 * sg, 2);
        }
        if (check(t, lm[i] + 14 * sg) >= 5) {
          apply(n, t, u, lm[i] + 14 * sg, 0);
        }
      }
    }
  }
  if (end) {
    return;
  }

  int x = lm[u];
  int m = check(t, x);
  for (int i = 2; i >= 0; --i) {
    if (m >= to[i]) {
      apply(n, t, u, x, i);
    }
  }

  dfs(n, t, u + 1);
}

int main() {
  srand(time(0));
  memset(nmb, -1, sizeof nmb);

  int n;
  scanf("%d", &n);
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      scanf("%d", &c[(i + 2) * 14 + (j + 2)]);
    }
  }

  for (int i = 0; i < 196; ++i) {
    if (c[i] != 0 && nmb[i] == -1) {
      int s = 0;
      vector<int> lm;
      queue<int> q;
      q.push(i);
      nmb[i] = lumps.size();
      while (!q.empty()) {
        lm.push_back(q.front());
        s += c[q.front()];
        for (int j = 0; j < 5; ++j) {
          int x = q.front() + d[j];
          if (c[x] != 0 && nmb[x] == -1) {
            q.push(x);
            nmb[x] = lumps.size();
          }
        }
        q.pop();
      }
      random_shuffle(lm.begin(), lm.end());
      lumps.push_back(lm);
      sum.push_back(s);
    }
  }

  dfs(n, 0, 0);
  return 1;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <queue>
#include <vector>
using namespace std;

int d[13] = {
    -14, -1, 0, 1, 14, -15, -13, 13, 15, -28, -2, 2, 28,
};
int to[3] = {5, 9, 13};

int c[196];
int nmb[196];
vector<vector<int>> lumps;
vector<int> sum;
vector<int> ans;

inline void draw(int x, int t, int dif) {
  for (int i = 0; i < t; ++i) {
    c[x + d[i]] += dif;
  }
}

inline int check(int t, int x) {
  int m;
  for (m = 0; m < 13 && c[x + d[m]] > 0; ++m)
    ;
  return m;
}

void dfs(int, int, int);

inline void apply(int n, int t, int u, int x, int a) {
  draw(x, to[a], -1);
  sum[t] -= to[a];
  ans.push_back(x << 2 | a);
  dfs(n - 1, t, u);
  ans.pop_back();
  sum[t] += to[a];
  draw(x, to[a], 1);
}

void dfs(int n, int t, int u) {
  if (sum[t] == 0) {
    ++t;
    u = 0;
  }

  if (t == lumps.size()) {
    if (n == 0) {
      for (int i = 0; i < ans.size(); ++i) {
        int y = (ans[i] >> 2) / 14 - 2;
        int x = (ans[i] >> 2) % 14 - 2;
        int z = (ans[i] & 3) + 1;
        printf("%d %d %d\\n", x, y, z);
      }
      exit(0);
    }
    return;
  }

  const vector<int> &lm = lumps[t];
  while (u < lm.size() && c[lm[u]] == 0) {
    ++u;
  }
  if (u >= lm.size()) {
    return;
  }
  /*
          for(int i = 0; i < lm.size(); ++i){
                  if(c[lm[i]] && !c[lm[i] - 14] && !c[lm[i] + 14]){
                          for(int sg = -1; sg < 2; sg += 2){
                                  if(check(t, lm[i] + 2 * sg) >= 13){
                                          apply(n, t, u, lm[i] + 2 * sg, 2);
                                  }
                                  if(check(t, lm[i] + sg) >= 5){
                                          apply(n, t, u, lm[i] + sg, 0);
                                  }
                          }
                  }

                  if(c[lm[i]] && !c[lm[i] - 1] && !c[lm[i] + 1]){
                          for(int sg = -1; sg < 2; sg += 2){
                                  if(check(t, lm[i] + 28 * sg) >= 13){
                                          apply(n, t, u, lm[i] + 28 * sg, 2);
                                  }
                                  if(check(t, lm[i] + 14 * sg) >= 5){
                                          apply(n, t, u, lm[i] + 14 * sg, 0);
                                  }
                          }
                  }
          }
  */
  int x = lm[u];
  int m = check(t, x);
  for (int i = 2; i >= 0; --i) {
    if (m >= to[i]) {
      apply(n, t, u, x, i);
    }
  }

  dfs(n, t, u + 1);
}

int main() {
  srand(time(0));
  memset(nmb, -1, sizeof nmb);

  int n;
  scanf("%d", &n);
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      scanf("%d", &c[(i + 2) * 14 + (j + 2)]);
    }
  }

  for (int i = 0; i < 196; ++i) {
    if (c[i] != 0 && nmb[i] == -1) {
      int s = 0;
      vector<int> lm;
      queue<int> q;
      q.push(i);
      nmb[i] = lumps.size();
      while (!q.empty()) {
        lm.push_back(q.front());
        s += c[q.front()];
        for (int j = 0; j < 5; ++j) {
          int x = q.front() + d[j];
          if (c[x] != 0 && nmb[x] == -1) {
            q.push(x);
            nmb[x] = lumps.size();
          }
        }
        q.pop();
      }
      random_shuffle(lm.begin(), lm.end());
      lumps.push_back(lm);
      sum.push_back(s);
    }
  }

  dfs(n, 0, 0);
  return 1;
}