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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define R 10
#define PSIZE 5
//????????????????????????14*14????????????????????£????????????????????????
//??¢?´¢?????§????????????????????§?????????
//??¨?????£????????????????????????????????????????????????????????????????????£??????
int T[10][10];
int N;

//????????????????????¨???????????????????????????????????¶??????????????????????????????????????´
//?????´????????????center??§???????????´
int mid = (PSIZE - 1) / 2;
int p[3][5][5] = {
    {0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

    {0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

    {0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0}

};
//{2,0}???????????????????????????
int center[3][2] = {{0, 1}, {1, 1}, {0, 2}};
int psize[] = {3, 3, 5};

void add_point(int x, int y, int point[5][5]) {
  for (int i = 0; i < PSIZE; i++) {
    for (int j = 0; j < PSIZE; j++) {
      if (point[i][j])
        T[y + i][x + j - mid] += point[i][j];
    }
  }
}

void delete_point(int x, int y, int point[5][5]) {
  for (int i = 0; i < PSIZE; i++) {
    for (int j = 0; j < PSIZE; j++) {
      if (point[i][j])
        T[y + i][x + j - mid] -= point[i][j];
    }
  }
}

bool can_delete(int x, int y, int point[5][5]) {
  for (int i = 0; i < PSIZE; i++) {
    for (int j = 0; j < PSIZE; j++) {
      if (y + i < 0 || y + i >= R || x + j - mid < 0 || x + j - mid >= R)
        if (point[i][j])
          return false;
      if (T[y + i][x + j - mid] - point[i][j] < 0)
        return false;
    }
  }
  return true;
}

bool checkT() {
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < R; j++) {
      if (T[i][j] != 0)
        return false;
    }
  }
  return true;
}

vector<pair<pair<int, int>, int>> points;
bool rec(int x, int y, int cnt) {
  if (checkT())
    return true;
  if (cnt == N)
    return false;

  for (; !T[y][x];) {
    y += (x == 9 ? 1 : 0);
    x = (x + 1) % 10;
    if (y == 10)
      return false;
  }

  for (int i = 0; i < 3; i++) {
    if (!can_delete(x, y, p[i]))
      continue;
    delete_point(x, y, p[i]);
    points.push_back(
        make_pair(make_pair(x + center[i][0], y + center[i][1]), i));
    if (rec(x, y, cnt + 1))
      return true;
    points.pop_back();
    add_point(x, y, p[i]);
  }
  return false;
}

void q91() {
  cin >> N;
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < R; j++) {
      cin >> T[i][j];
    }
  }
  bool ans = rec(0, 0, 0);
  for (auto it = points.begin(); it != points.end(); it++) {
    cout << (*it).first.first << " " << (*it).first.second << " "
         << (*it).second + 1 << endl;
  }
}
int main() {
  q91();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>

#include <cmath>
#include <cstdlib>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

int small[][5]{0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1,
               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
pair<int, int> smallCenter = {0, 1};

int middle[][5]{0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1,
                1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
pair<int, int> middleCenter = {1, 1};

int big[][5]{0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1,
             1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0};
pair<int, int> bigCenter = {0, 2};

pair<pair<int, int>, int> point[12];
int table[10][10];

bool clearCheck() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (table[i][j] != 0)
        return false;
    }
  }
  return true;
}
bool removeCheck(int y, int x, int drop[][5]) {
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      if (!drop[i][j])
        continue;
      if (x + j - 2 < 0 || x + j - 2 >= 10)
        return false;
      if (y + i < 0 || y + i >= 10)
        return false;
      if (!table[y + i][x + j - 2])
        return false;
    }
  }
  return true;
}

void remove(int y, int x, int drop[][5]) {
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      table[y + i][x + j - 2] -= drop[i][j];
    }
  }
}

void add(int y, int x, int drop[][5]) {
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      table[y + i][x + j - 2] += drop[i][j];
    }
  }
}

bool rec(int i, int j, int c) {
  if (c < 0)
    return clearCheck();
  while (!(i == 10)) {
    if (table[i][j]) {
      if (removeCheck(i, j, small)) {
        point[c] = make_pair(
            make_pair(i + smallCenter.second, j + smallCenter.first), 1);
        remove(i, j, small);
        if (rec(i, j, c - 1))
          return true;
        add(i, j, small);
      }

      if (removeCheck(i, j, middle)) {
        point[c] = make_pair(
            make_pair(i + middleCenter.second, j + middleCenter.first), 2);
        remove(i, j, middle);
        if (rec(i, j, c - 1))
          return true;
        add(i, j, middle);
      }

      if (removeCheck(i, j, big)) {
        point[c] =
            make_pair(make_pair(i + bigCenter.second, j + bigCenter.first), 3);
        remove(i, j, big);
        if (rec(i, j, c - 1))
          return true;
        add(i, j, big);
      }
    }
    j++;
    i += j / 10;
    j = j % 10;
  }
  return false;
}
void q91() {
  int n;
  for (; cin >> n;) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        cin >> table[i][j];
      }
    }
    rec(0, 0, n - 1);
    for (int i = 0; i < n; i++) {
      cout << point[i].first.second << " " << point[i].first.first << " "
           << point[i].second << endl;
    }
  }
}

int main() {
  q91();
  return 0;
}