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

#include <cstdio>
#include <vector>

#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vpii;

vector<vpii> rect;
int w, h, n, land[50][50], test[50][50], ans[50][50];

bool search(int i0, int j0) {
  // printf("i0=%d, j0=%d\\n",i0,j0);
  // for(int a=0;a<h;a++){for(int b=0;b<w;b++)printf("%d
  // ",test[a][b]);puts("");}puts("");

  for (int i = i0; i < h; i++) {
    for (int j = (i == i0 ? j0 : 0); j < w; j++) {
      if (i < 0 || h <= i || j < 0 || w <= j)
        while (1)
          ;
      int id = land[i][j];
      if (id == 0)
        continue;
      if (id < 0 || n < id)
        continue;
      // try to put a rectangle
      int cnt = 0;
      for (int k = 0; k < rect[id].size(); k++) {
        int rh = rect[id][k].first, rw = rect[id][k].second;
        for (int y = i - rh + 1; y <= i; y++) {
          for (int x = j - rw + 1; x <= j; x++) {
            if (!(0 <= y && y + rh <= h && 0 <= x && x + rw <= w))
              continue;
            bool canput = true;
            for (int yy = y; yy < y + rh; yy++)
              for (int xx = x; xx < x + rw; xx++) {
                if (yy < 0 || h <= yy || xx < 0 || w <= xx)
                  while (1)
                    ;
                if (test[yy][xx] != 0 ||
                    (land[yy][xx] != 0 && land[yy][xx] != id))
                  canput = false;
              }
            if (!canput)
              continue;
            for (int yy = y; yy < y + rh; yy++)
              for (int xx = x; xx < x + rw; xx++) {
                if (yy < 0 || h <= yy || xx < 0 || w <= xx)
                  while (1)
                    ;
                test[yy][xx] = id;
              }
            cnt += search(i, j + 1);
            for (int yy = y; yy < y + rh; yy++)
              for (int xx = x; xx < x + rw; xx++) {
                if (yy < 0 || h <= yy || xx < 0 || w <= xx)
                  while (1)
                    ;
                test[yy][xx] = 0;
              }
            if (cnt >= 2)
              return false;
          }
        }
      }

      return (cnt == 1) ? true : false;
    }
  }

  bool suc = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (test[i][j] == 0)
        suc = false;
    }
  }
  if (suc) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
        ans[i][j] = test[i][j];
    }
    return true;
  }
  return false;
}

int main() {
  for (; scanf("%d%d%d", &w, &h, &n), w;) {
    rect = vector<vpii>(n + 1);

    for (int i = 0; i < n; i++) {
      int id, area;
      scanf("%d%d", &id, &area);
      for (int j = 1; j <= area; j++) {
        if (area % j == 0)
          rect[id].pb(mp(j, area / j));
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
        scanf("%d", &land[i][j]);
    }

    if (search(0, 0)) {
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          printf("%s%d", (j ? " " : ""), ans[i][j]);
        }
        putchar('\\n');
      }
    } else
      puts("NA");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <vector>

#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vpii;

vector<vpii> rect;
int w, h, n, land[50][50], test[50][50], ans[50][50];

bool search(int i0, int j0) {
  // printf("i0=%d, j0=%d\\n",i0,j0);
  // for(int a=0;a<h;a++){for(int b=0;b<w;b++)printf("%d
  // ",test[a][b]);puts("");}puts("");

  for (int i = i0; i < h; i++) {
    for (int j = (i == i0 ? j0 : 0); j < w; j++) {
      if (i < 0 || h <= i || j < 0 || w <= j)
        exit(0);
      int id = land[i][j];
      if (id == 0)
        continue;
      if (id < 0 || n < id)
        while (1)
          puts("@@@@@@");
      // try to put a rectangle
      int cnt = 0;
      for (int k = 0; k < rect[id].size(); k++) {
        int rh = rect[id][k].first, rw = rect[id][k].second;
        for (int y = i - rh + 1; y <= i; y++) {
          for (int x = j - rw + 1; x <= j; x++) {
            if (!(0 <= y && y + rh <= h && 0 <= x && x + rw <= w))
              continue;
            bool canput = true;
            for (int yy = y; yy < y + rh; yy++)
              for (int xx = x; xx < x + rw; xx++) {
                if (yy < 0 || h <= yy || xx < 0 || w <= xx)
                  while (1)
                    ;
                if (test[yy][xx] != 0 ||
                    (land[yy][xx] != 0 && land[yy][xx] != id))
                  canput = false;
              }
            if (!canput)
              continue;
            for (int yy = y; yy < y + rh; yy++)
              for (int xx = x; xx < x + rw; xx++) {
                if (yy < 0 || h <= yy || xx < 0 || w <= xx)
                  while (1)
                    ;
                test[yy][xx] = id;
              }
            cnt += search(i, j + 1);
            for (int yy = y; yy < y + rh; yy++)
              for (int xx = x; xx < x + rw; xx++) {
                if (yy < 0 || h <= yy || xx < 0 || w <= xx)
                  while (1)
                    ;
                test[yy][xx] = 0;
              }
            if (cnt >= 2)
              return false;
          }
        }
      }

      return (cnt == 1) ? true : false;
    }
  }

  bool suc = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (test[i][j] == 0)
        suc = false;
    }
  }
  if (suc) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
        ans[i][j] = test[i][j];
    }
    return true;
  }
  return false;
}

int main() {
  for (; scanf("%d%d%d", &w, &h, &n), w;) {
    rect = vector<vpii>(n + 1);

    for (int i = 0; i < n; i++) {
      int id, area;
      scanf("%d%d", &id, &area);
      for (int j = 1; j <= area; j++) {
        if (area % j == 0)
          rect[id].pb(mp(j, area / j));
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
        scanf("%d", &land[i][j]);
    }

    if (search(0, 0)) {
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          printf("%s%d", (j ? " " : ""), ans[i][j]);
        }
        putchar('\\n');
      }
    } else
      puts("NA");
  }

  return 0;
}