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

#include <iostream>
using namespace std;
struct State {
  int x[12][12];
};
int H, W, n, b[20], p[20];
State S, ans;
int area(State V, int q) {
  int mx = 100000, my = 100000, nx = -1, ny = -1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (V.x[i][j] != q)
        continue;
      mx = min(mx, i);
      my = min(my, j);
      nx = max(nx, i);
      ny = max(ny, j);
    }
  }
  for (int i = mx; i <= nx; i++) {
    for (int j = my; j <= ny; j++) {
      if (V.x[i][j] != q && V.x[i][j] != 0)
        return 99999999;
    }
  }
  return (nx - mx + 1) * (ny - my + 1);
}
int solve(State T, int r1, int r2) {
  if (r2 == W) {
    r1++;
    r2 = 0;
  }
  if (r1 == H) {
    for (int i = 1; i <= n; i++) {
      if (area(T, i) != p[i])
        return 0;
    }
    ans = T;
    return 1;
  }
  if (T.x[r1][r2] >= 1) {
    return solve(T, r1, r2 + 1);
  } else {
    int ret = 0;
    for (int i = 1; i <= n; i++) {
      State U = T;
      U.x[r1][r2] = i;
      if (area(U, i) > p[i])
        continue;
      ret += solve(U, r1, r2 + 1);
    }
    return ret;
  }
}
int main() {
  while (true) {
    cin >> W >> H >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> b[i];
      cin >> p[b[i]];
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> S.x[i][j];
    }
    int ret = solve(S, 0, 0);
    if (ret == 1) {
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          if (j)
            cout << ' ';
          cout << ans.x[i][j];
        }
        cout << endl;
      }
    } else
      cout << "NA" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
queue<pair<int, vector<vector<int>>>> Q;
vector<vector<int>> X, P;
int H, W, N, V1[20], V2[20];
pair<bool, vector<vector<int>>> hantei(vector<vector<int>> a1, int X1, int Y1,
                                       int H1, int W1, int N1) {
  bool OK = true;
  int G[30][30];
  for (int i = 0; i < 900; i++)
    G[i / 30][i % 30] = 0;
  for (int i = X1; i < X1 + H1; i++) {
    for (int j = Y1; j < Y1 + W1; j++) {
      if (i >= H || j >= W) {
        OK = false;
        continue;
      }
      if (a1[i][j] != 0 || (X[i][j] != V1[N1] && X[i][j] >= 1))
        OK = false;
      a1[i][j] = V1[N1];
      G[i][j] = 1;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (X[i][j] == V1[N1] && G[i][j] == 0)
        OK = false;
    }
  }
  return make_pair(OK, a1);
}
int main() {
  while (true) {
    X.clear();
    P.clear();
    H = 0;
    W = 0;
    N = 0;
    cin >> W >> H >> N;
    int D1 = 0;
    if (W == 0 && H == 0 && N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> V1[i] >> V2[i];
      D1 += V2[i];
    }
    for (int i = 0; i < H; i++) {
      vector<int> G, I;
      int a;
      for (int j = 0; j < W; j++) {
        cin >> a;
        G.push_back(a);
        I.push_back(0);
      }
      X.push_back(G);
      P.push_back(I);
    }
    if (D1 != W * H) {
      cout << "NA" << endl;
      continue;
    }
    Q.push(make_pair(0, P));
    vector<vector<int>> res;
    int cnt = 0;
    while (!Q.empty()) {
      vector<vector<int>> K = Q.front().second;
      int L = Q.front().first;
      Q.pop();
      if (Q.size() >= 200) {
        cout << "NA" << endl;
        while (!Q.empty())
          Q.pop();
        goto E;
      }
      if (L == N) {
        cnt++;
        res = K;
        continue;
      }
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          for (int k = 1; k <= V2[L]; k++) {
            if (V2[L] % k != 0)
              continue;
            pair<bool, vector<vector<int>>> res1 =
                hantei(K, i, j, k, V2[L] / k, L);
            if (res1.first == true) {
              Q.push(make_pair(L + 1, res1.second));
            }
          }
        }
      }
    }
    if (cnt == 0 || cnt >= 2)
      cout << "NA" << endl;
    else {
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          if (j >= 1) {
            cout << ' ';
          }
          cout << res[i][j];
        }
        cout << endl;
      }
    }
  E:;
  }
  return 0;
}