【跨立实验-非严格相交】poj1556

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length. 

Input

The input data for the illustrated chamber would appear as follows. 


4 2 7 8 9 
7 3 4.5 6 7 

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1. 

Output

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input

1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1

Sample Output

10.00
10.06

https://www.cnblogs.com/tuyang1129/p/9390376.html

由于文中如果擦边而过的话也是可以的,所以在跨立实验中写成

if (cross(le[i].x1-a.x,le[i].y1-a.y,b.x-a.x,b.y-a.y) *
            cross(b.x-a.x,b.y-a.y,le[i].x2-a.x,le[i].y2-a.y) > eps
            && cross(a.x-le[i].x2,a.y-le[i].y2,le[i].x1-le[i].x2,le[i].y1-le[i].y2) *
            cross(le[i].x1-le[i].x2,le[i].y1-le[i].y2,b.x-le[i].x2,b.y-le[i].y2) > eps)
                return false;

当两者都异号的时候肯定是不行的,当两者有一个为0,一个为异号的时候,代表两线段中有一个是如同T字形状的

这个时候这个题目是可以的


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int mx = 105;
int n;
double mat[mx][mx];

struct Point {
    double x, y;
    Point () {}
    Point (double _x, double _y) {
        x = _x; y = _y;
    }
}pt[mx];

struct Line {
    double x1, y1, x2, y2;
    Line () {}
    Line (double _x1, double _y1, double _x2, double _y2) {
        x1 = _x1; x2 = _x2; y1 = _y1; y2 = _y2;
    }
}le[mx];

double dis(Point a, Point b) {
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}

double cross(double x1, double y1, double x2, double y2) {
    return x1*y2 - x2*y1;
}

bool judge(Point a, Point b) {
    for (int i = 1; i <= n*3; i++) {
        if (cross(le[i].x1-a.x,le[i].y1-a.y,b.x-a.x,b.y-a.y) *
            cross(b.x-a.x,b.y-a.y,le[i].x2-a.x,le[i].y2-a.y) > eps
            && cross(a.x-le[i].x2,a.y-le[i].y2,le[i].x1-le[i].x2,le[i].y1-le[i].y2) *
            cross(le[i].x1-le[i].x2,le[i].y1-le[i].y2,b.x-le[i].x2,b.y-le[i].y2) > eps)
                return false;
    }
    return true;
}

void init() {
    for (int i = 0; i < mx; i++)
        for (int j = 0; j < mx; j++)
            mat[i][j] = INF;
}

int main() {
    while (scanf("%d",&n) != EOF && n != -1) {
        init();
        double a, b, c, d, e;
        for (int i = 1; i <= n; i++) {
            scanf("%lf%lf%lf%lf%lf",&a, &b, &c, &d, &e);
            pt[i*4-3] = Point(a, b);
            pt[i*4-2] = Point(a, c);
            pt[i*4-1] = Point(a, d);
            pt[i*4-0] = Point(a, e);
            le[i*3-2] = Line(a, 0, a, b);
            le[i*3-1] = Line(a, c, a, d);
            le[i*3-0] = Line(a, e, a, 10);
        }
        pt[n*4+1] = Point(0, 5); pt[n*4+2] = Point(10, 5);
        for (int i = 1; i <= n*4+2; i++) {
            for (int j = i+1; j <= n*4+2; j++) {
                if (abs(pt[i].x-pt[j].x) < eps) continue;
                if (judge(pt[i], pt[j])) {
                    //printf("%f %f %f %f --- %f\n",pt[i].x, pt[i].y, pt[j].x, pt[j].y,abs(pt[i].x-pt[j].x));
                    mat[i][j] = mat[j][i] = dis(pt[i],pt[j]);
                }
            }
        }
        for (int k = 1; k <= 4*n+2; k++) {
            for (int i = 1; i <= 4*n+2; i++) {
                for (int j = 1; j <= 4*n+2; j++) {
                    mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);
                }
            }
        }
        printf("%.2f\n", mat[4*n+1][4*n+2]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值