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<bits/stdc++.h>
#define inf 1<<29
#define linf 1e16
#define eps (1e-11)
#define mod 1000000007
#define pi acos(-1)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define pd(a) printf("%.10f\n",(double)(a))
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)
#define equals(a,b) (fabs((a)-(b))<eps)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<int,double> pid;
typedef pair<double,int> pdi;
typedef vector<int> vi;
typedef vector<pii> vpi;
const int dx[8]={1,0,-1,0,1,1,-1,-1};
const int dy[8]={0,1,0,-1,1,-1,1,-1};

class Point{
public:
  double x,y;
  Point(double x=0,double y=0):x(x),y(y){}

  Point operator+(Point p){ return Point(x+p.x,y+p.y);}
  Point operator-(Point p){ return Point(x-p.x,y-p.y);}
  Point operator*(double k){ return Point(x*k,y*k);}
  Point operator/(double k){ return Point(x/k,y/k);}
  bool operator<(Point p)const{ return (x!=p.x ? x<p.x : y<p.y);}
  bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}

  double abs(){ return sqrt(norm());}
  double norm(){ return (x*x+y*y);}
};
typedef Point Vector;
typedef vector<Point> Polygon;

class Segment{
public:
  Point p1,p2;
  Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}
  bool operator==(Segment s)const{
    if(s.p1==p1 && s.p2==p2)return true;
    if(s.p2==p1 && s.p1==p2)return true;
    return false;
  }
};
typedef Segment Line;

double norm(Vector a){ return (a.x*a.x+a.y*a.y);}
double abs(Vector a){ return sqrt(norm(a));}
double dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}
double cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}

Point project(Segment s,Point p){
  Vector base=(s.p2-s.p1);
  double r=(dot(p-s.p1,base)/base.norm());
  return (s.p1+base*r);
}

Point reflect(Segment s,Point p){
  return p+(project(s,p)-p)*2;
}

Segment reflect(Segment s,Segment t){
  return Segment(reflect(s,t.p1),reflect(s,t.p2));
}

int ccw(Point p0,Point p1,Point p2){
  Vector a=p1-p0;
  Vector b=p2-p0;
  if(cross(a,b)>eps)return 1;
  if(cross(a,b)<-eps)return -1;
  if(dot(a,b)<-eps)return 2;
  if(a.norm()<b.norm())return -2;
  return 0;
}

bool intersect(Point p1,Point p2,Point p3,Point p4){
  return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 &&
          ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);
}

bool intersect(Segment s1,Segment s2){
  return intersect(s1.p1,s1.p2,s2.p1,s2.p2);
}

Point getCrossPointLL(Line a,Line b){
  double A=cross(a.p2-a.p1,b.p2-b.p1);
  double B=cross(a.p2-a.p1,a.p2-b.p1);
  if(abs(A)<eps || abs(B)<eps)return b.p1;
  return b.p1+(b.p2-b.p1)*(B/A);
}

int n;
Point s,t;

typedef vector<Segment> VS;

void p(VS v){
  FOR(i,0,v.size()){
    cout<<v[i].p1.x<<" "<<v[i].p1.y<<" ";
    cout<<v[i].p2.x<<" "<<v[i].p2.y<<endl;
  }
}

double check(VS used,vector<VS> vvs,Point rt){
  vector<Point> vp;
  vp.pb(s);
  FOR(i,0,used.size()){
    if(!intersect(used[i],Segment(s,rt)))return inf;
    vp.pb(getCrossPointLL(used[i],Segment(s,rt)));
  }
  vp.pb(rt);
  int c=0;
  FOR(i,2,vp.size())c+=ccw(vp[i-2],vp[i-1],vp[i]);
  if(abs(c)!=vp.size()*2-4)return inf;
  FOR(i,0,vvs.size()){
    FOR(j,0,vvs[i].size()){
      if(!intersect(Line(s,rt),vvs[i][j]))continue;
      Point m=getCrossPointLL(Line(s,rt),vvs[i][j]);
      if(m==vp[i] || m==vp[i+1])continue;
      if(ccw(vp[i+1],vp[i],m)==0)return inf;
    }
  }
  return abs(s-rt);
}

double rec(VS vs,VS used,vector<VS> vvs,Point rt){
  if(6<=used.size())return inf;
  vvs.pb(vs);
  double res = check(used,vvs,rt);
  FOR(i,0,vs.size()){
    if(used.size() && vs[i]==used.back())continue;
    VS rvs;
    FOR(j,0,vs.size())rvs.pb(reflect(vs[i],vs[j]));
    used.pb(vs[i]);
    res=min(res,rec(rvs,used,vvs,reflect(vs[i],rt)));
    used.pop_back();
  }
  return res;
}

int main()
{
  while(cin>>n && n){
    vector<Segment> vs,used;
    vector<vector<Segment> > vvs;
    FOR(i,0,n){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      vs.pb(Segment(Point(a,b),Point(c,d)));
    }
    cin>>s.x>>s.y>>t.x>>t.y;
    pd(rec(vs,used,vvs,t));
  }
  return 0;
}