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

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

#define pb push_back

int X, Y, N;
int b[100], k[100];
int c[10][10], f[10][10], ans[10][10];
int cnt;

vector<int> bx[101], by[101];

bool isputs(int x, int y, int n, int a, int b) {
  if (x + bx[a][b] > X || y + by[a][b] > Y)
    return false;

  for (int i = y; i < y + by[a][b]; i++)
    for (int j = x; j < x + bx[a][b]; j++) {
      if (c[j][i] != 0 && c[j][i] != n)
        return false;
      if (f[j][i] != 0)
        return false;

      f[j][i] = n;
    }

  return true;
}

void dfs(int n) {
  if (cnt >= 2)
    return;

  if (n == N) {
    cnt++;
    memcpy(ans, f, sizeof(ans));
    return;
  }

  int p = b[n], a = k[n];
  int t[10][10];
  memcpy(t, f, sizeof(t));

  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++) {
      if (f[j][i] != 0)
        continue;
      for (int l = 0; l < bx[a].size(); l++) {
        if (isputs(j, i, p, a, l))
          dfs(n + 1);

        memcpy(f, t, sizeof(f));
      }
    }
}

int main() {
  for (int x = 1; x <= 10; x++)
    for (int y = 1; y <= 10; y++) {
      int n = x * y;
      bx[n].pb(x);
      by[n].pb(y);
    }

  while (cin >> X >> Y >> N, (X || Y || N)) {
    vector<pair<int, int>> data;

    cnt = 0;
    for (int i = 0; i < N; i++) {
      cin >> b[i] >> k[i];
      data.push_back(make_pair(k[i], b[i]));
    }

    sort(data.begin(), data.end(), greater<pair<int, int>>());
    for (int i = 0; i < N; i++) {
      b[i] = data[i].second;
      k[i] = data[i].first;
    }

    for (int i = 0; i < Y; i++)
      for (int j = 0; j < X; j++)
        cin >> c[j][i];

    memset(f, 0, sizeof(f));

    dfs(0);
    if (cnt == 1) {
      for (int i = 0; i < Y; i++) {
        for (int j = 0; j < X; j++)
          cout << ans[j][i] << (j == X - 1 ? "" : " ");
        cout << endl;
      }
    } else
      cout << "NA" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define pb push_back

int X, Y, N;
int b[100], k[100];
int c[10][10], f[10][10], ans[10][10];
int cnt;

vector<int> bx[101], by[101];

bool isputs(int x, int y, int n, int a, int b) {
  if (x + bx[a][b] > X || y + by[a][b] > Y)
    return false;

  for (int i = y; i < y + by[a][b]; i++)
    for (int j = x; j < x + bx[a][b]; j++) {
      if (c[j][i] != 0 && c[j][i] != n)
        return false;
      if (f[j][i] != 0)
        return false;

      f[j][i] = n;
    }

  return true;
}

void dfs(int n) {
  if (cnt >= 2)
    return;

  if (n == N) {
    cnt++;
    memcpy(ans, f, sizeof(ans));
    return;
  }

  int p = b[n], a = k[n];
  int t[10][10];
  memcpy(t, f, sizeof(t));

  for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++) {
      if (f[j][i] != 0)
        continue;
      for (int l = 0; l < bx[a].size(); l++) {
        if (isputs(j, i, p, a, l))
          dfs(n + 1);

        memcpy(f, t, sizeof(f));
      }
    }
}

int main() {
  for (int x = 1; x <= 10; x++)
    for (int y = 1; y <= 10; y++) {
      int n = x * y;
      bx[n].pb(x);
      by[n].pb(y);
    }

  while (cin >> X >> Y >> N, (X || Y || N)) {
    vector<pair<int, int>> data;

    cnt = 0;
    for (int i = 0; i < N; i++) {
      cin >> b[i] >> k[i];
      data.push_back(make_pair(k[i], b[i]));
    }

    sort(data.begin(), data.end());
    for (int i = 0; i < N; i++) {
      b[i] = data[i].second;
      k[i] = data[i].first;
    }

    for (int i = 0; i < Y; i++)
      for (int j = 0; j < X; j++)
        cin >> c[j][i];

    memset(f, 0, sizeof(f));

    dfs(0);
    if (cnt == 1) {
      for (int i = 0; i < Y; i++) {
        for (int j = 0; j < X; j++)
          cout << ans[j][i] << " ";
        cout << endl;
      }
    } else
      cout << "NA" << endl;
  }
}