#include #include #include #include using namespace std; #define N 200 + 5 #define eps 1e-9 const int Fx[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int Case, n, szx, szy, X1[N], Y1[N], X2[N], Y2[N], X[N << 2], Y[N << 2], Cnt[N << 2][N << 2][2]; bool Flag[N << 2][N << 2]; double Dis[N << 2][N << 2]; struct Node { int x, y; double dis; Node(){} Node(int x, int y, double dis) : x(x), y(y), dis(dis) {} bool operator < (const Node& obj) const { return dis > obj.dis; } }; priority_queue Q; int Count(int x_1, int y_1, int x_2, int y_2) { if (x_1 > x_2) swap(x_1, x_2); if (y_1 > y_2) swap(y_1, y_2); int res = 0; for (int i = 1; i <= n; i ++) res += (X1[i] <= x_1 && x_2 <= X2[i] && Y1[i] <= y_1 && y_2 <= Y2[i]); return res; } void Dijkstra(int sx, int sy) { for (int i = 1; i <= szx; i ++) for (int j = 1; j <= szy; j ++) Dis[i][j] = 1e18, Flag[i][j] = 0; Dis[sx][sy] = 0; Q.push(Node(sx, sy, 0)); while (!Q.empty()) { Node x; do { x = Q.top(); Q.pop(); } while (!Q.empty() && (Flag[x.x][x.y] || x.dis > Dis[x.x][x.y])); if (Q.empty() && (Flag[x.x][x.y] || x.dis > Dis[x.x][x.y])) break ; Flag[x.x][x.y] = 1; for (int k = 0; k < 4; k ++) { int tx = x.x + Fx[k][0], ty = x.y + Fx[k][1]; if (tx && ty && tx <= szx && ty <= szy) { double _dis = Dis[x.x][x.y] + double(abs(X[tx] - X[x.x]) + abs(Y[ty] - Y[x.y])) / (Cnt[min(x.x, tx)][min(x.y, ty)][k & 1] + 1); if (_dis < Dis[tx][ty]) { Dis[tx][ty] = _dis; Q.push(Node(tx, ty, _dis)); } } } } } int main() { for (scanf("%d", &Case); Case; Case --) { scanf("%d", &n); for (int i = 1; i <= n + 1; i ++) { scanf("%d%d%d%d", X1 + i, Y1 + i, X2 + i, Y2 + i); X[i] = X1[i], X[i + n + 1] = X2[i], X[i + (n + 1) * 2] = X2[i] + 1; Y[i] = Y1[i], Y[i + n + 1] = Y2[i], Y[i + (n + 1) * 2] = Y2[i] + 1; } sort(X + 1, X + (n + 1) * 3 + 1); szx = unique(X + 1, X + (n + 1) * 3 + 1) - X - 1; sort(Y + 1, Y + (n + 1) * 3 + 1); szy = unique(Y + 1, Y + (n + 1) * 3 + 1) - Y - 1; for (int i = 1; i <= szx; i ++) for (int j = 1; j <= szy; j ++) for (int k = 0; k < 2; k ++) Cnt[i][j][k] = 0; for (int i = 1; i <= n + 1; i ++) { X1[i] = lower_bound(X + 1, X + szx + 1, X1[i]) - X; X2[i] = lower_bound(X + 1, X + szx + 1, X2[i]) - X; Y1[i] = lower_bound(Y + 1, Y + szy + 1, Y1[i]) - Y; Y2[i] = lower_bound(Y + 1, Y + szy + 1, Y2[i]) - Y; if (i <= n) { Cnt[X1[i]][Y1[i]][0] ++, Cnt[X2[i] + 1][Y2[i]][0] ++; Cnt[X1[i]][Y2[i]][0] --, Cnt[X2[i] + 1][Y1[i]][0] --; Cnt[X1[i]][Y1[i]][1] ++, Cnt[X2[i]][Y2[i] + 1][1] ++; Cnt[X2[i]][Y1[i]][1] --, Cnt[X1[i]][Y2[i] + 1][1] --; } } for (int i = 1; i <= szx; i ++) for (int j = 1; j <= szy; j ++) for (int k = 0; k < 2; k ++) Cnt[i][j][k] += Cnt[i - 1][j][k] + Cnt[i][j - 1][k] - Cnt[i - 1][j - 1][k]; Dijkstra(X1[n + 1], Y1[n + 1]); printf("%.5f\n", Dis[X2[n + 1]][Y2[n + 1]]); } return 0; }