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

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

int q, mas[10][10], dy[] = {1, 1, 2}, dx[] = {0, 1, 0};
int draw[][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}},
};

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 dfs(int y = 0, int x = 0, int n = q) {
  if (n < 0)
    return false;
  if (y == 10)
    return n == 0;
  if (mas[y][x] == 0)
    return dfs(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 (dfs(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];
    }
  }

  dfs();
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n;
int b[10][10];

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

void debug() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      cout << b[i][j] << " ";
    }
    puts("");
  }
  puts("");
}

inline bool inside(int y, int x) {
  return 0 <= y && y < 10 && 0 <= x && x < 10;
}

inline bool izryt(int y, int x, int type) {
  if (type == 4)
    return false;
  for (int i = 4 * (type - 1) + 1; i <= 4 * type; ++i) {
    int ny = y + dy[i], nx = x + dx[i];
    if (!b[ny][nx] || !inside(ny, nx))
      return false;
  }
  return true;
}

inline void add(int y, int x, int type) {
  if (type == 0)
    return;
  for (int i = 0; i <= 4 * type; ++i) {
    b[y + dy[i]][x + dx[i]]++;
  }
}

inline void subtract(int y, int x, int type) {
  if (type == 1)
    b[y][x]--;
  for (int i = 4 * (type - 1) + 1; i <= 4 * type; ++i) {
    b[y + dy[i]][x + dx[i]]--;
  }
}

bool dfs(int sum, int v = 0, int y = 1, int x = 1) {
  if (y == 9)
    return false;

  if (v == n) {
    return sum == 0;
  }

  int nx = x % 8 + 1;
  int ny = y + (x == 8);
  if (!b[y][x])
    return dfs(sum, v, ny, nx);

  for (int i = 1; i <= 4; ++i) {
    if (izryt(y, x, i)) {
      subtract(y, x, i);
      if (dfs(sum - i * 4 - 1, v + 1, y, x)) {
        printf("%d %d %d\\n", x, y, i);
        return true;
      }
    } else {
      add(y, x, i - 1);
      break;
    }
  }

  return dfs(sum, v, ny, nx);
}

int main() {
  cin >> n;

  int sum = 0;
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      cin >> b[i][j];
      sum += b[i][j];
    }
  }

  dfs(sum);
}

inline bool latte(int y, int x, int type, int d = 0) {
  if (d == -1) {
    for (int i = 0; i <= 4 * type; ++i) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (!b[ny][nx] || !inside(ny, nx))
        return false;
    }
  }

  for (int i = 0; i <= 4 * type; ++i) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    b[ny][nx] += d;
  }

  return true;
}