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

#include <bits/stdc++.h>
using namespace std;

int q, mas[10][10];
int draw[][5][5] = {
    /*Small*/
    {{0, 0, 1, 0, 0}, {0, 1, 1, 1, 0}, {0, 0, 1, 0, 0}},
    //*midium*/
    {{0, 0, 1, 1, 1}, {0, 0, 1, 1, 1}, {0, 0, 1, 1, 1}},
    /*large*/
    {{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}},
};

bool check(int y, int x, int w) {
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      if (mas[y + i][x + j - 2] - draw[w][i][j] < 0)
        return false;
    }
  }
  return true;
}

void paint(int y, int x, int w) {
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      mas[y + i][x + j - 2] -= draw[w][i][j];
    }
  }
}

void repaint(int y, int x, int w) {
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      mas[y + i][x + j - 2] += draw[w][i][j];
    }
  }
}

bool rec(int y, int x, int n) {
  if (n < 0)
    return false;
  if (y == 10)
    return n == 0;
  if (mas[y][x] == 0)
    return rec(y + (x == 9), (x + 1) % 10, n);
  for (int i = 0; i < 3; i++) {
    if (!check(y, x, i))
      continue;
    paint(y, x, i);
    if (rec(y, x, n - 1)) {
      cout << x + (i == 1) << " " << y + 1 + (i == 2) << " " << i + 1 << endl;
      return true;
    }
    repaint(y, x, i);
  }
  return false;
}

int main() {
  cin >> q;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      cin >> mas[i][j];
    }
  }
  rec(0, 0, q);
}

Error: TLE

Fixed submission:

#include <algorithm>
#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;

int mid = (PSIZE - 1) / 2;
int p[3][5][5]{
    {0, 0, 0, 0, 0, 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, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 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}};

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++) {
      T[y + i - mid][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++) {
      T[y + i - mid][x + j - mid] -= point[i][j];
    }
  }
}

bool validT() {
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < R; j++) {
      if (T[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 cnt) {
  for (int i = 1; i < R - 1; i++) {
    for (int j = 1; j < R - 1; j++) {
      if (!T[j][i])
        continue;
      for (int k = 0; k < 3; k++) {
        delete_point(i, j, p[k]);
        if (validT()) {
          points.push_back(make_pair(make_pair(i, j), k));
          if (checkT())
            return true;
          if (cnt == N)
            return false;
          if (rec(cnt + 1))
            return true;
          points.pop_back();
        }
        add_point(i, j, p[k]);
      }
    }
  }
  return false;
}

void q91() {
  scanf("%d\\n", &N);
  for (int i = 0; i < R; i++) {
    string line;
    getline(cin, line);
    istringstream ss(line);
    string str;
    int j = 0;
    for (; getline(ss, str, ' ');) {
      T[i][j++] = atoi(str.c_str());
    }
  }
  bool ans = rec(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;
}