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++ (judge verdict: Accepted)

#include <iostream>
#include <algorithm>
#include <vector>
#include <complex>
#include <cmath>
#include <cstdio>
using namespace std;
 
typedef complex<double> P;
 
const double eps = 1e-3;
const double inf = 1e8;
 
bool equals(double a, double b) {
  return abs(a-b) < eps;
}
 
bool equals(P a, P b) {
  return equals(a.real(), b.real()) && equals(a.imag(), b.imag());
}
 
double dot(P a, P b) {
  return a.real()*b.real() + a.imag()*b.imag();
}
 
double cross(P a, P b) {
  return a.real()*b.imag() - a.imag()*b.real();
}
 
int ccw(P p, P a, P b) {
  a -= p;
  b -= p;
  if(cross(a,b) > eps) return 1;
  if(cross(a,b) < -eps) return -1;
  if(dot(a,b) < -eps) return 2;
  if(norm(b)-norm(a) > eps) return -2;
  return 0;
}
 
P project(P s1, P s2, P p) {
  P base = s2 - s1;
  double t = dot(p - s1, base)/norm(base);
  return s1 + base*t;
}

P reflect(P s1, P s2, P p) {
  return p + (project(s1, s2, p) - p)*2.0;
}

bool isIntersect(P a1, P a2, P b1, P b2) {
  return ( ccw(a1,a2,b1) * ccw(a1,a2,b2) <= 0 &&
           ccw(b1,b2,a1) * ccw(b1,b2,a2) <= 0 );
}
 
P getCrossP(P a1, P a2, P b1, P b2) {
  P a = a2 - a1;
  P b = b2 - b1;
  return a1 + a * cross(b, b1 - a1)/cross(b, a);
}

int N;
vector<P> s1, s2;
P src, dst;

double test(P v1, P v2) {
  double sum = 0;
  for(int t = 0; t <= 5; ++t) {
    int k;
    P p;
    double mini = inf;
    P a = v1;
    P b = v1+(v2-v1)*inf;
    for(int i = 0; i < N; ++i) {
      if(!equals(abs(cross(v2-v1,s2[i]-s1[i])), 0.0) &&
         isIntersect(a, b, s1[i], s2[i]) &&
         ccw(a,b,s1[i]) != 0 && ccw(a,b,s2[i]) != 0) {
        P q = getCrossP(a, b, s1[i], s2[i]);
        double dist = norm(q-v1);
        if(equals(dist, 0.0)) continue;
        if(dist < mini) {
          mini = dist;
          p = q;
          k = i;
        }
      }
    }

    if(ccw(a,b,dst) == 0 &&
       ( equals(norm(dst-v1), mini) || norm(dst-v1) < mini )) {
      return sum + abs(dst-v1);
    }

    if(mini == inf) return inf;
 
    if(t < 5) {
      sum += abs(p-v1);
      P base = s2[k] - s1[k];
      P n(-base.imag(), base.real());
      v2 = reflect(p, p+n, v1);
      v1 = p;
    }
  }
  return inf;
}
 
int main() {
  while(cin >> N && N) {
    s1.resize(N);
    s2.resize(N);
    for(int i = 0; i < N; ++i) {
      cin >> s1[i].real() >> s1[i].imag()
          >> s2[i].real() >> s2[i].imag();
    }
    cin >> dst.real() >> dst.imag() >> src.real() >> src.imag();
 
    vector<P> target;
    target.push_back(dst);
    for(int t = 0; t < 5; ++t) {
      int size = target.size();
      for(int i = 0; i < size; ++i) {
        for(int j = 0; j < N; ++j) {
          P p = reflect(s1[j], s2[j], target[i]);
          target.push_back(p);
        }
      }
    }
 
    double res = inf;
    for(int i = 0; i < target.size(); ++i) {
      if(equals(src, target[i])) continue;
      res = min(res, test(src, target[i]));
    }
    printf("%.10f\n", res);
  }
  return 0;
}