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

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

int w, h, n, b[20], k[20], px[20], py[20], ans;
int res[20][20];

bool check(int id, const vector<vector<int>> &s, int sx, int sy, int gx,
           int gy) {
  if (sx <= px[id] && px[id] <= gx && sy <= py[id] && py[id] <= gy) {
    bool flag = false;

    for (int y = sy; y <= gy; y++) {
      for (int x = sx; x <= gx; x++) {
        if (s[y][x] == id + 1) {
          flag = true;
        } else if (s[y][x] != 0) {
          return false;
        }
      }
    }
    return flag;
  }
  return false;
}

void fill_(int id, vector<vector<int>> &next, int sx, int sy, int gx, int gy) {
  for (int y = sy; y <= gy; y++) {
    for (int x = sx; x <= gx; x++) {
      next[y][x] = id + 1;
    }
  }
}

void debug(const vector<vector<int>> &s) {
  cout << "debug:" << endl;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (x)
        cout << " ";
      cout << s[y][x];
    }
    cout << endl;
  }
  cout << endl;
}

// id := 購入者番号, id = [0,n)
void dfs(int id, const vector<vector<int>> &s) {

  if (id == n) { // ぜんぶ調べた
    ans++;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        res[y][x] = s[y][x];
      }
    }
  } else {

    for (int sy = 0; sy < h; sy++) {
      for (int sx = 0; sx < w; sx++) {
        for (int gy = sy; gy < h; gy++) {
          for (int gx = sx; gx < w; gx++) {
            int width = gx - sx + 1, height = gy - sy + 1;

            if (width * height == k[id] && check(id, s, sx, sy, gx, gy)) {
              vector<vector<int>> next = s;
              fill_(id, next, sx, sy, gx, gy);
              dfs(id + 1, next);
            }
          }
        }
      }
    }
  }
}

int main() {
  while (cin >> w >> h >> n, w || h || n) {
    for (int i = 0; i < n; i++)
      cin >> b[i] >> k[i];

    vector<vector<int>> s(h, vector<int>(w));
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> s[y][x];
        if (s[y][x] != 0) {
          px[s[y][x] - 1] = x;
          py[s[y][x] - 1] = y;
        }
      }
    }

    ans = 0;
    dfs(0, s);
    if (ans != 1) {
      cout << "NA" << endl;
    } else {
      for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
          if (x)
            cout << " ";
          cout << res[y][x];
        }
        cout << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

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

int w, h, n, b[20], k[20], px[20], py[20], ans;

bool check(int id, const vector<vector<int>> &s, int sx, int sy, int gx,
           int gy) {
  if (sx <= px[id] && px[id] <= gx && sy <= py[id] && py[id] <= gy) {
    bool flag = false;

    for (int y = sy; y <= gy; y++) {
      for (int x = sx; x <= gx; x++) {
        if (s[y][x] == id + 1) {
          flag = true;
        } else if (s[y][x] != 0) {
          return false;
        }
      }
    }
    return flag;
  }
  return false;
}

void fill_(int id, vector<vector<int>> &next, int sx, int sy, int gx, int gy) {
  for (int y = sy; y <= gy; y++) {
    for (int x = sx; x <= gx; x++) {
      next[y][x] = id + 1;
    }
  }
}

void debug(const vector<vector<int>> &s) {
  cout << "debug:" << endl;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (x)
        cout << " ";
      cout << s[y][x];
    }
    cout << endl;
  }
  cout << endl;
}

// id := 購入者番号, id = [0,n)
void dfs(int id, const vector<vector<int>> &s, bool out) {

  if (id == n) { // ぜんぶ調べた
    ans++;
    if (out) {
      for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
          if (x)
            cout << " ";
          cout << s[y][x];
        }
        cout << endl;
      }
    }
  } else {

    for (int sy = 0; sy < h; sy++) {
      for (int sx = 0; sx < w; sx++) {
        for (int gy = sy; gy < h; gy++) {
          for (int gx = sx; gx < w; gx++) {
            int width = gx - sx + 1, height = gy - sy + 1;

            if (width * height == k[id] && check(id, s, sx, sy, gx, gy)) {
              vector<vector<int>> next = s;
              fill_(id, next, sx, sy, gx, gy);
              dfs(id + 1, next, out);
            }
          }
        }
      }
    }
  }
}

int main() {
  while (cin >> w >> h >> n, w || h || n) {
    for (int i = 0; i < n; i++)
      cin >> b[i] >> k[i];

    vector<vector<int>> s(h, vector<int>(w));
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> s[y][x];
        if (s[y][x] != 0) {
          px[s[y][x] - 1] = x;
          py[s[y][x] - 1] = y;
        }
      }
    }

    ans = 0;
    dfs(0, s, false);
    if (ans != 1) {
      cout << "NA" << endl;
    } else {
      dfs(0, s, true);
    }
  }
}