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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

typedef long long LL;
const int MaxN = 15;
const int Inf = 0x3F3F3F3F;

struct Node {
  int x1, x2, y1, y2;
  void init() {
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 > x2)
      swap(x1, x2);
    if (y1 > y2)
      swap(y1, y2);
  }
} node[MaxN];
int ret[MaxN];
int mx, my;
int N, K;

void dfs(int x1, int y1, int x2, int y2) {
  int idx;
  int nx1, nx2, ny2;

  while (K < N) {
    idx = -1;
    for (int i = 0; i < N; ++i) {
      if (ret[i] == 0) {
        if (node[i].x1 >= x1 && node[i].x2 <= x2 && node[i].y1 >= y1 &&
            node[i].y2 <= y2) {
          if (idx == -1 || node[i].x1 < node[idx].x1 ||
              (node[i].x1 == node[idx].x1 && node[i].y1 <= node[idx].y1))
            idx = i;
        }
      }
    }
    if (idx == -1)
      break;
    ret[idx] = ++K;
    nx1 = node[idx].x1;
    nx2 = mx;
    ny2 = node[idx].y2;
    for (int i = 0; i < N; ++i) {
      if (node[i].x1 >= node[idx].x2 && node[i].y1 < ny2 && node[i].y2 > ny2)
        nx2 = min(nx2, node[i].x1);
    }
    dfs(nx1, 0, nx2, ny2);
  }
}

int main() {
  bool ok = false;
  while (scanf("%d", &N) == 1 && N) {
    if (ok)
      puts("");
    else
      ok = true;
    K = mx = my = 0;
    for (int i = 0; i < N; ++i) {
      node[i].init();
      ret[i] = 0;
      mx = max(mx, node[i].x2);
      my = max(my, node[i].y2);
    }
    dfs(0, 0, mx, my);
    for (int i = 0; i < N; ++i)
      printf("%d\\n", ret[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

struct nd {
  bool gg;
  int s, x, z, y, i;
} q[10];

bool cmp(nd a, nd b) { return a.y < b.y || (a.y == b.y && a.s < b.s); }
int zz;

void f(int n, nd q[10]) {
  int i, trp, n0 = n;
  nd q1[10];
  q1[0].y = 1000;
  for (i = 1, zz = 0; i <= n; i++) {
    q[i].gg = 0;
    if (zz < q[i].z)
      zz = q[i].z;
  }
  while (n0 > 0) {
    trp = 0;
    for (i = 1; i <= n; i++)
      if (!q[i].gg && cmp(q[i], q[trp]))
        trp = i;
    printf("%d\\n", q[trp].i);
    q[trp].gg = 1;
    int t = 0;
    int x = q[trp].x, z = q[trp].z;
    bool yy = 1;
    while (yy) {
      for (i = 1; i <= n; i++) {
        if (q[i].y == z && q[i].s < x && q[i].x > x) {
          if (q[i].gg) {
            yy = 0;
            break;
          }
          printf("%d\\n", q[i].i);
          q[i].gg = 1;
          n0--;
          yy = 0;
          break;
        } else if (!q[i].gg && q[i].y == z && q[i].x == x) {
          z = q[i].z;
          break;
        }
      }
      if (z == zz)
        break;
    }
    for (i = 1; i <= n; i++)
      if (!q[i].gg && q[i].x <= q[trp].x && q[i].z <= z) {
        q1[++t] = q[i];
        q[i].gg = 1;
      }
    if (t > 0)
      f(t, q1);
    n0 -= t + 1;
  }
}

int main() {
  int n, i;

  // freopen("1.txt", "r", stdin);
  while (scanf("%d", &n) && n) {
    zz = 0;
    for (i = 1; i <= n; i++) {
      scanf("%d%d%d%d", &q[i].y, &q[i].s, &q[i].z, &q[i].x);
      q[i].gg = 0;
      q[i].i = i;
    }
    q[0].y = 1000;
    f(n, q);
    puts("");
  }

  return 0;
}