To increase the difficulty and fun, XadillaX build some walls on the racing ground. The racing ground is a rectangle and all the walls are on the ground with negligible thick. That means the thick of the wall can be ignored. So on the small map, it look like that:
In fact, everyone's speed in Minecraft is the same. We assume that the ground is N * N miles, there're W walls and the speed of each player is 1 mile per second. The starting point is (0, 0) and the destination is (N, N).
10 3 1.0 1.0 1.0 5.0 9.0 5.0 9.0 9.0 5.0 2.5 5.0 7.5 10 1 1.0 1.0 9.0 9.0
14.64 14.14
double EPS=1e-10; // 考虑误差的加法运算 double add(double a,double b) { if(fabs(a+b)<EPS*(fabs(a)+fabs(b))) return 0; return a+b; } struct Point{ double x,y; Point(){} Point(double x,double y):x(x),y(y){} // 构造函数,方便代码编写 Point operator +(Point p){ return Point(add(x,p.x), add(y,p.y)); } Point operator-(Point p){ return Point(add(x,-p.x),add(y,-p.y)); } Point operator*(double d){ return Point(x*d,y*d); } double operator*(Point p){ // 内积 点乘 return add(x*p.x, y*p.y); } double operator^(Point p){// 外积 叉乘 return add(x*p.y,-y*p.x); } double dist(Point p){ return sqrt(add((x-p.x)*(x-p.x) , (y-p.y)*(y-p.y))) ; } }; //判断点p0是否在线段p1p2内 int on_segment(Point p1, Point p2, Point p0) { if (((p1-p0).x * (p2-p0).x <=0 )&& ((p1-p0).y * (p2-p0).y <=0)) // 中间是 && return 1; return 0; } // 判断线段p1p2与q1是否相交 int intersection(Point p1,Point p2, Point q1,Point q2) { double d1=(p2-p1)^(q1-p1); // 计算p1p2 到q 点的转向 d1>0 左转, d1 <0 右转 double d2=(p2-p1)^(q2-p1); double d3=(q2-q1)^(p1-q1); double d4=(q2-q1)^(p2-q1); if(d1*d2<0 && d3*d4 <0) // 中间是 && return 1; return 0; } struct Line{ Point s , t ; void read(){ scanf("%lf%lf%lf%lf" ,&s.x,&s.y,&t.x,&t.y) ; } }line[58] ; int m ; vector<pair<int , double> > List[120] ; int ok(Point a , Point b){ for(int i = 1 ; i <= m ; i++){ if(intersection(a,b,line[i].s , line[i].t)) return 0 ; } return 1 ; } queue<int> qq ; double dist[200] ; bool in[200] ; double spfa(){ int i ; while(! qq.empty()) qq.pop() ; memset(in , 0 , sizeof(in)) ; for(i = 0 ; i <= 2*m+1 ; i++) dist[i] = 1000000000 ; dist[0] = 0 ; in[0] = 1 ; qq.push(0) ; while(! qq.empty()){ int u = qq.front() ; qq.pop() ; in[u] = 0 ; for(int i = 0 ; i < List[u].size() ; i++){ int v = List[u][i].first ; double w = List[u][i].second ; if(dist[u] + w < dist[v]){ dist[v] = dist[u] + w ; if(! in[v]){ in[v] = 1 ; qq.push(v) ; } } } } return dist[2*m+1] ; } int main(){ double n ; int i , j; while(cin>>n>>m){ Point S(0 , 0) , T(n , n) ; for(i = 1 ; i <= m ; i++) line[i].read() ; for(i = 0 ; i <= m*2+1 ; i++) List[i].clear() ; for(i = 1 ; i <= m ; i++){ List[i*2-1].push_back(make_pair(i*2 , line[i].s.dist(line[i].t))) ; List[i*2].push_back(make_pair(i*2-1 , line[i].s.dist(line[i].t))) ; } for(i = 1 ; i <= m ; i++){ if(ok(S , line[i].s)) List[0].push_back(make_pair(i*2-1 , S.dist(line[i].s))) ; if(ok(S , line[i].t)) List[0].push_back(make_pair(i*2 , S.dist(line[i].t))) ; } for(i = 1 ; i <= m ; i++){ if(ok(T , line[i].s)) List[i*2-1].push_back(make_pair(m*2+1 , T.dist(line[i].s))) ; if(ok(T , line[i].t)) List[i*2].push_back(make_pair(m*2+1 , T.dist(line[i].t))) ; } if(ok(S , T)) List[0].push_back(make_pair(2*m+1 , S.dist(T))) ; for(i = 1 ; i <= m ; i++){ for(j = i+1 ; j <= m ; j++){ if(ok(line[i].s , line[j].s)){ List[i*2-1].push_back(make_pair(j*2-1 , line[i].s.dist(line[j].s))) ; List[j*2-1].push_back(make_pair(i*2-1 , line[i].s.dist(line[j].s))) ; } if(ok(line[i].s , line[j].t)){ List[i*2-1].push_back(make_pair(j*2 , line[i].s.dist(line[j].t))) ; List[j*2].push_back(make_pair(i*2-1 , line[i].s.dist(line[j].t))) ; } if(ok(line[i].t , line[j].s)){ List[i*2].push_back(make_pair(j*2-1 , line[i].t.dist(line[j].s))) ; List[j*2-1].push_back(make_pair(i*2 , line[i].t.dist(line[j].s))) ; } if(ok(line[i].t , line[j].t)){ List[i*2].push_back(make_pair(j*2 , line[i].t.dist(line[j].t))) ; List[j*2].push_back(make_pair(i*2 , line[i].t.dist(line[j].t))) ; } } } printf("%.2lf\n" , spfa()) ; } return 0 ; }