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

#include <iostream>
#include <map>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef map<int, P> M;
typedef vector<P> V;

struct F {
  int a[10][10];
};

void acopy(int a[10][10], int b[10][10], int w, int h) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      b[i][j] = a[i][j];
    }
  }

  return;
}

vector<F> res;
void func(F r, V vec, M mpp, int w, int h, int c) {
  if (c == vec.size()) {
    res.push_back(r);
  } else {
    int t = vec[c].second, u = vec[c].first;
    int p = mpp[u].first, q = mpp[u].second;
    for (int m = 1; m <= t; m++) {
      if (t % m == 0) {
        F b;
        int s = t / m;
        for (int i = 0; i <= h - s; i++) {
          for (int j = 0; j <= w - m; j++) {
            if (i > p || i + s <= p || j > q || j + m <= q) {
              continue;
            }
            acopy(r.a, b.a, w, h);
            bool f1 = true, f2 = false;
            for (int k = 0; k < s && f1; k++) {
              for (int l = 0; l < m && f1; l++) {
                if (!(b.a[i + k][j + l] == 0 || b.a[i + k][j + l] == u)) {
                  f1 = false;
                }
                if (b.a[i + k][j + l] == u) {
                  f2 = true;
                }
                b.a[i + k][j + l] = u;
              }
            }

            if (f1 && f2) {
              func(b, vec, mpp, w, h, c + 1);
            }
          }
        }
      }
    }
  }

  return;
}

int main() {
  int w, h, n, s, t;
  F r;

  while (cin >> w >> h >> n && (w + h + n)) {
    V vec;
    M mpp;
    for (int i = 0; i < n; i++) {
      cin >> s >> t;
      vec.push_back(P(s, t));
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> r.a[i][j];
        if (r.a[i][j] != 0) {
          mpp[r.a[i][j]] = P(i, j);
        }
      }
    }

    func(r, vec, mpp, w, h, 0);
    if (res.size() == 1) {
      r = res[0];
      for (int i = 0; i < h; i++) {
        cout << r.a[i][0];
        for (int j = 1; j < w; j++) {
          cout << " " << r.a[i][j];
        }
        cout << endl;
      }
    } else {
      cout << "NA" << endl;
    }

    res.clear();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef vector<P> V;

struct F {
  int a[10][10];
};

void acopy(int a[10][10], int b[10][10], int w, int h) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      b[i][j] = a[i][j];
    }
  }

  return;
}

vector<F> res;
void func(F r, V vec, int w, int h, int p) {
  if (p == vec.size()) {
    res.push_back(r);
  } else {
    int t = vec[p].second, u = vec[p].first;
    for (int m = 1; m <= t; m++) {
      if (t % m == 0) {
        F b;
        int s = t / m;
        for (int i = 0; i <= h - s; i++) {
          for (int j = 0; j <= w - m; j++) {
            acopy(r.a, b.a, w, h);
            bool f1 = true, f2 = false;
            for (int k = 0; k < s && f1; k++) {
              for (int l = 0; l < m && f1; l++) {
                if (!(b.a[i + k][j + l] == 0 || b.a[i + k][j + l] == u)) {
                  f1 = false;
                }
                if (b.a[i + k][j + l] == u) {
                  f2 = true;
                }
                b.a[i + k][j + l] = u;
              }
            }

            if (f1 && f2) {
              func(b, vec, w, h, p + 1);
            }
          }
        }
      }
    }
  }

  return;
}

int main() {
  int w, h, n, s, t;
  F r;

  while (cin >> w >> h >> n && (w + h + n)) {
    V vec;

    for (int i = 0; i < n; i++) {
      cin >> s >> t;
      vec.push_back(P(s, t));
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> r.a[i][j];
      }
    }

    func(r, vec, w, h, 0);
    if (res.size() == 1) {
      r = res[0];
      for (int i = 0; i < h; i++) {
        cout << r.a[i][0];
        for (int j = 1; j < w; j++) {
          cout << " " << r.a[i][j];
        }
        cout << endl;
      }
    } else {
      cout << "NA" << endl;
    }

    res.clear();
  }

  return 0;
}