Problem p00649 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
const int INF = 100000000;

struct C {
  int x0, y0, x1, y1;
  int id;
  bool operator<(const C &s) const {
    if (x0 != s.x0)
      return x0 < s.x0;
    return y0 < s.y0;
  }
};
void rec(set<C> S, vector<int> &order) {
  if (S.empty())
    return;

  C target = *S.begin();
  S.erase(target);
  order.push_back(target.id);

  int extend = INF;
  for (auto c : S) {
    if (c.y0 < target.y1 && target.y1 < c.y1)
      extend = min(extend, c.x0);
  }
  assert(extend >= target.x1);

  set<C> upper, lower;
  for (auto c : S) {
    if (c.x1 <= extend && c.y1 <= target.y1)
      lower.insert(c);
    else
      upper.insert(c);
  }
  rec(lower, order);
  rec(upper, order);
}

int main() {
  bool first = true;
  for (int N; cin >> N && N;) {
    if (first)
      first = false;
    else
      cout << endl;

    set<C> S;
    REP(i, N) {
      C c;
      c.id = i;
      cin >> c.x0 >> c.y0 >> c.x1 >> c.y1;
      if (c.x0 > c.x1)
        swap(c.x0, c.x1);
      if (c.y0 > c.y1)
        swap(c.y0, c.y1);
      S.insert(c);
    }

    vector<int> order;
    rec(S, order);

    assert(order.size() == N);
    vector<int> ans(N, -1);
    REP(i, N) ans[order[i]] = i + 1;

    REP(i, ans.size()) {
      assert(ans[i] >= 1);
      cout << ans[i] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {0, 1, -1, 0};
int dy[8] = {1, 1, -1, -1};
int N;
int a[5][5];
int goal[5][5];
int goal_x[16];
int goal_y[16];
int dist[20][20];
int max_ans;
int hstar() {
  int max_dist = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i + 1; j++) {
      int x = goal_x[a[i][j]];
      int y = goal_y[a[i][j]];
      max_dist = max(max_dist, dist[y - i + 10][x - j + 10]);
    }
  }
  return max_dist;
}
int dfs(int turn) {
  int h = hstar();
  if (turn + h >= max_ans)
    return INF;
  if (h == 0)
    return max_ans = turn;
  int ans = INF;
  REP(i, N) REP(j, i + 1) if (goal[i][j] != a[i][j]) REP(r, 4) {
    int x = j + dx[r];
    int y = i + dy[r];
    if (y < N && x < y + 1 && y >= 0 && x >= 0) {
      swap(a[y][x], a[i][j]);
      ans = min(ans, dfs(turn + 1));
      swap(a[y][x], a[i][j]);
    }
  }
  return ans;
}

int main() {
  int tmp = 1;
  REP(i, 20) REP(j, 20) dist[i][j] = INF;
  dist[10][10] = 0;
  REP(iter, 20 * 20)
  REP(i, 20) REP(j, 20) if (dist[i][j] < INF)
      REP(r, 4) if (valid(i + dy[r], j + dx[r], 20, 20))
          dist[i + dy[r]][j + dx[r]] =
              min(dist[i + dy[r]][j + dx[r]], dist[i][j] + 1);
  REP(i, N) REP(j, i + 1) goal[i][j] = tmp++;
  REP(i, N) REP(j, i + 1) goal_x[goal[i][j]] = j;
  REP(i, N) REP(j, i + 1) goal_y[goal[i][j]] = i;
  while (cin >> N && N) {
    REP(i, N) REP(j, i + 1) cin >> a[i][j];
    max_ans = 46;
    cout << dfs(0) << endl;
  }
  return 0;
}