Problem:
Problem G: Laser Beam Reflections

A laser beam generator, a target object and some mirrors are
placed on a plane.
The mirrors stand upright on the plane,
and both sides of the mirrors are flat and can reflect beams.
To point the beam at the target,
you may set the beam to several directions because of different reflections.
Your job is to find the shortest beam path
from the generator to the target and answer the length of the path.

Figure G-1 shows examples of possible beam paths,
where the bold line represents the shortest one.

Figure G-1: Examples of possible paths

Input

The input consists of a number of datasets. The end of the input is indicated by a line containing a zero.

Each dataset is formatted as follows.
Each value in the dataset except n is an integer no less than 0 and no more than 100.

n

PX1 PY1 QX1 QY1

...

PXn PYn QXn QYn

TX TY

LX LY

The first line of a dataset contains an integer n (1 ≤ n ≤ 5),
representing the number of mirrors.
The following n lines list the arrangement of mirrors on the plane.
The coordinates
(PXi, PYi) and
(QXi, QYi) represent
the positions of both ends of the mirror.
Mirrors are apart from each other.
The last two lines represent the target position (TX, TY)
and the position of the beam generator (LX, LY).
The positions of the target and the generator are apart from each other,
and these positions are also apart from mirrors.

The sizes of the target object and the beam generator are small enough to be ignored.
You can also ignore the thicknesses of mirrors.

In addition, you can assume the following conditions on the given datasets.

There is at least one possible path from the beam generator to the target.

The number of reflections along the shortest path is less than 6.

The shortest path does not cross or touch a plane containing a mirror
surface at any points within 0.001 unit distance from either end of the mirror.

Even if the beam could arbitrarily select reflection or passage
when it reaches each plane containing a mirror surface
at a point within 0.001 unit distance from one of the mirror ends,
any paths from the generator to the target would not be
shorter than the shortest path.

When the beam is shot from the generator in an arbitrary direction and
it reflects on a mirror or passes the mirror within 0.001 unit distance from the mirror,
the angle θ formed by the beam and the mirror
satisfies sin(θ) > 0.1,
until the beam reaches the 6th reflection point (exclusive).

Figure G-1 corresponds to the first dataset of the Sample Input below.
Figure G-2 shows the shortest paths for the subsequent datasets of the Sample Input.

Figure G-2: Examples of the shortest paths

Output

For each dataset, output a single line containing the length of the shortest path from
the beam generator to the target.
The value should not have an error greater than 0.001.
No extra characters should appear in the output.

Sample Input

2
30 10 30 75
60 30 60 95
90 0
0 100
1
20 81 90 90
10 90
90 10
2
10 0 10 58
20 20 20 58
0 70
30 0
4
8 0 8 60
16 16 16 48
16 10 28 30
16 52 28 34
24 0
24 64
5
8 0 8 60
16 16 16 48
16 10 28 30
16 52 28 34
100 0 100 50
24 0
24 64
0

Output for the Sample Input

180.27756377319946
113.13708498984761
98.99494936611666
90.50966799187809
90.50966799187809

Solution in C++14 (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-7;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

typedef long double ld;
const ld PI = acos(-1.0);
bool eq(ld a, ld b) { return abs(a - b) < EPS; }
typedef complex<ld> Point;
typedef vector<Point> Polygon;

namespace std
{
	bool operator < (const Point& a, const Point& b)
	{
		return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
	}
}

struct Line
{
	Point a, b;
	Line() {};
	Line(Point p, Point q) :a(p), b(q) {};
	Line(ld x1, ld y1, ld x2, ld y2) :a(Point(x1, y1)), b(Point(x2, y2)) {};
};

ld dot(Point a, Point b)
{
	return real(conj(a)*b);
}

ld cross(Point a, Point b)
{
	return imag(conj(a)*b);
}

int ccw(Point a, Point b, Point c)
{
	b -= a; c -= a;
	if (cross(b, c) > EPS) return 1;    //counter cloclwise
	if (cross(b, c) < -EPS) return -1;  //cloclwise
	if (dot(b, c) < 0) return 2;        //c--a--b on line 
	if (norm(b) < norm(c)) return -2;   //a--b--c on line
	return 0;                           //a--c--b on line
}

bool isis_ll(Line l, Line m)
{
	return abs(cross(l.b - l.a, m.b - m.a)) > EPS;
}

bool isis_ls(Line l, Line s)
{
	return cross(l.b - l.a, s.a - l.a)*cross(l.b - l.a, s.b - l.a) < EPS;
}

bool isis_ss(Line s, Line t)
{
	return (ccw(s.a, s.b, t.a)*ccw(s.a, s.b, t.b) <= 0 &&
		ccw(t.a, t.b, s.a)*ccw(t.a, t.b, s.b) <= 0);
}

bool isis_sp(Line s, Point p)
{
	return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;
}

Point projection(Line l, Point p)
{
	Point base = l.b - l.a;
	ld r = dot(p - l.a, base) / norm(base);
	return l.a + base*r;
}

Point mirror(Line l, Point p)
{
	return Point(2, 0)*projection(l, p) - p;
}

Point is_ll(Line s, Line t)
{
	ld a = cross(s.b - s.a, t.b - t.a);
	ld b = cross(s.b - s.a, s.b - t.a);
	return t.a + b / a*(t.b - t.a);
}

vector<vi> make_per(int use, int n)
{
	vi v = {};
	queue<vi> que;
	que.push(v);
	vector<vi> res;
	while (que.size())
	{
		vi t = que.front(); que.pop();
		if (t.size() == use)
		{
			res.push_back(t);
		}
		else
		{
			REP(i, n)
			{
				vi nx = t;
				nx.push_back(i);
				que.push(nx);
			}
		}
	}
	return res;
}

bool isis_ss_edge(Line s, Line t)
{
	if (!isis_ss(s, t)) return false;
	if (isis_sp(t, s.a) || isis_sp(t, s.b) || isis_sp(s, t.a) || isis_sp(s, t.b)) return false;
	return true;
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	int n;
	while (cin >> n, n)
	{
		vector<Line> mirrors(n);
		REP(i, n)
		{
			ld a, b, c, d; cin >> a >> b >> c >> d;
			mirrors[i] = Line(a, b, c, d);
		}
		ld sx, sy, gx, gy;
		cin >> sx >> sy >> gx >> gy;
		Point st(sx, sy), go(gx, gy);
		ld ans = INF;
		REP(use, 6)
		{
			vector<vi> per = make_per(use, n);
			for (auto tper : per)
			{
				Point tmpg = go;
				for (int i = tper.size() - 1; i >= 0; i--)
				{
					tmpg = mirror(mirrors[tper[i]], tmpg);
				}
				vector<Line> tmp_mirrors = mirrors;
				bool f = true;
				Line light(st, tmpg);
				ld len = abs(light.a - light.b);
				if (len >= 133.5437 && len <= 133.5439)
				{
					REP(i, tper.size()) cout << tper[i] << " ";
					cout << endl;
				}
				REP(i, tper.size() + 1)
				{
					int col = -1;
					Point p(go);
					REP(j, n)
					{
						if (isis_ss_edge(light, tmp_mirrors[j]))
						{
							Point tp = is_ll(light, tmp_mirrors[j]);
							if (abs(light.a - p) - abs(light.a - tp) > EPS)
							{
								col = j;
								p = tp;
							}
						}
					}
					if (i != tper.size())
					{
						if (col != tper[i])
						{
							f = false;
							break;
						}
						REP(j, tmp_mirrors.size())
						{
							Line tmpl(tmp_mirrors[j]);
							Line mir_tmpl(mirror(tmp_mirrors[tper[i]], tmpl.a), mirror(tmp_mirrors[tper[i]], tmpl.b));
							tmp_mirrors[j] = mir_tmpl;
						}
						light.a = p;
					}
					else
					{
						if (col != -1)
						{
							f = false;
							break;
						}
					}
				}
				if (f) chmin(ans, len);
			}
		}
		cout << ans << endl;
	}
	return 0;
}