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

#include <cstdio>
#include <cstdlib>
#include <set>
#include <vector>
using namespace std;

const vector<pair<pair<int, int>, vector<vector<int>>>> pattern = {
    {{9, 2},
     {
         {1, 1, 1},
         {1, 1, 1},
         {1, 1, 1},
     }},
    {{13, 3},
     {
         {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},
     }},
    {{5, 1},
     {
         {0, 1, 0},
         {1, 1, 1},
         {0, 1, 0},
     }},
};

vector<vector<int>> m(10);
vector<pair<pair<int, int>, int>> v;
void dfs(int pat, int y, int x, int N, int d, int s) {
  if (d == N) {
    if (s == 0) {
      for (auto &e : v)
        printf("%d %d %d\\n", e.first.first, e.first.second, e.second);
      exit(0);
    }
  }
  if (s < (N - d) * 5 || (N - d) * 13 < s)
    return;
  for (; pat < pattern.size(); pat++, y = x = 0)
    if (pattern[pat].first.first <= s) {
      for (; y <= 10 - pattern[pat].second.size(); y++, x = 0) {
        for (; x <= 10 - pattern[pat].second[0].size(); x++) {
          for (int dy = 0; dy < pattern[pat].second.size(); dy++) {
            for (int dx = 0; dx < pattern[pat].second[0].size(); dx++) {
              if (m[y + dy][x + dx] < pattern[pat].second[dy][dx])
                goto fail;
            }
          }
          for (int dy = 0; dy < pattern[pat].second.size(); dy++) {
            for (int dx = 0; dx < pattern[pat].second[0].size(); dx++) {
              m[y + dy][x + dx] -= pattern[pat].second[dy][dx];
            }
          }
          v[d] = make_pair(make_pair(x + pattern[pat].second[0].size() / 2,
                                     y + pattern[pat].second.size() / 2),
                           pattern[pat].first.second);
          dfs(pat, y, x, N, d + 1, s - pattern[pat].first.first);
          for (int dy = 0; dy < pattern[pat].second.size(); dy++) {
            for (int dx = 0; dx < pattern[pat].second[0].size(); dx++) {
              m[y + dy][x + dx] += pattern[pat].second[dy][dx];
            }
          }
        fail:;
        }
      }
    }
}

int main() {
  int N, y, x, s = 0, z;
  scanf("%d", &N);
  for (y = 0; y < 10; y++) {
    m[y].resize(10);
    for (x = 0; x < 10; x++)
      scanf("%d", &m[y][x]), s += m[y][x];
  }
  v.resize(N);
  dfs(0, 0, 0, N, 0, s);
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <set>
#include <vector>
using namespace std;

const vector<pair<pair<int, int>, vector<vector<int>>>> pattern = {
    {{13, 3},
     {
         {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},
     }},
    {{9, 2},
     {
         {1, 1, 1},
         {1, 1, 1},
         {1, 1, 1},
     }},
    {{5, 1},
     {
         {0, 1, 0},
         {1, 1, 1},
         {0, 1, 0},
     }},
};

vector<vector<int>> m(10);
vector<pair<pair<int, int>, int>> v;
void dfs(int pat, int y, int x, int N, int d, int s) {
  if (d == N) {
    if (s == 0) {
      for (auto &e : v)
        printf("%d %d %d\\n", e.first.first, e.first.second, e.second);
      exit(0);
    }
  }
  if (s < (N - d) * 5 || (N - d) * 13 < s)
    return;
  for (; pat < pattern.size(); pat++, y = x = 0)
    if (pattern[pat].first.first <= s) {
      for (; y <= 10 - pattern[pat].second.size(); y++, x = 0) {
        for (; x <= 10 - pattern[pat].second[0].size(); x++) {
          for (int dy = 0; dy < pattern[pat].second.size(); dy++) {
            for (int dx = 0; dx < pattern[pat].second[0].size(); dx++) {
              if (m[y + dy][x + dx] < pattern[pat].second[dy][dx])
                goto fail;
            }
          }
          for (int dy = 0; dy < pattern[pat].second.size(); dy++) {
            for (int dx = 0; dx < pattern[pat].second[0].size(); dx++) {
              m[y + dy][x + dx] -= pattern[pat].second[dy][dx];
            }
          }
          v[d] = make_pair(make_pair(x + pattern[pat].second[0].size() / 2,
                                     y + pattern[pat].second.size() / 2),
                           pattern[pat].first.second);
          dfs(pat, y, x, N, d + 1, s - pattern[pat].first.first);
          for (int dy = 0; dy < pattern[pat].second.size(); dy++) {
            for (int dx = 0; dx < pattern[pat].second[0].size(); dx++) {
              m[y + dy][x + dx] += pattern[pat].second[dy][dx];
            }
          }
        fail:;
        }
      }
    }
}

int main() {
  int N, y, x, s = 0, z;
  scanf("%d", &N);
  for (y = 0; y < 10; y++) {
    m[y].resize(10);
    for (x = 0; x < 10; x++)
      scanf("%d", &m[y][x]), s += m[y][x];
  }
  v.resize(N);
  dfs(0, 0, 0, N, 0, s);
}