POJ-1556

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7105   Accepted: 2808

Description

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

Source

/**
    题意:给一个10*10的矩阵,里面有一些门,即线段没有覆盖的地方
        问从(0,5)到(10,5)最短的距离
    做法:计算机和 + floyd
        将每个x坐标中的门进行统计,然后首先看是否能直线通过,然后floyd然后求两点之间的最短路
**/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <string.h>
#define maxn 121
#define INF 0x3f3f3f3f
using namespace std;
const double eps = 1e-8;
double  path[maxn][maxn];
double  dist[maxn][maxn];
double mmap[maxn][maxn];
const double pi = acos(-1.0);
int n;
int sgn(double x)
{
    if(fabs(x) < eps) return 0;
    if(x <0) return -1;
    else return 1;
}
struct Point
{
    double x;
    double y;
    Point() {}
    Point(double _x,double _y)
    {
        x = _x;
        y = _y;
    }
} p[maxn];
double xx[maxn];
double yy[maxn][maxn];
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, double x3, double y3)
{
    return (x2 - x1)*(y3 - y1) - (y2 - y1)*(x3 - x1);
}
bool judge(Point p1, Point p2)
{
    if(p1.x >= p2.x)return 0;
    int i = 0;
    while(xx[i] <= p1.x && i < n)i++;
    while(xx[i] < p2.x && i < n)
    {
        if(cross(p1.x, p1.y, p2.x, p2.y, xx[i], 0)*cross(p1.x, p1.y, p2.x, p2.y, xx[i], yy[i][1])<0
                ||cross(p1.x, p1.y, p2.x, p2.y, xx[i], yy[i][2])*cross(p1.x, p1.y, p2.x, p2.y, xx[i], yy[i][3])<0
                ||cross(p1.x, p1.y, p2.x, p2.y, xx[i], yy[i][4])*cross(p1.x, p1.y, p2.x, p2.y, xx[i], 10)<0)
            return 0;
        i++;
    }
    return 1;
}
int temp = 0;
void floyd()
{
    for(int k=0; k<temp; k++)
    {
        for(int i=0; i<temp; i++)
        {
            for(int j=0; j<temp; j++)
            {
                if(k!=i && k!= j)
                    mmap[i][j] = min(mmap[i][j],mmap[i][k] +mmap[k][j]);
            }
        }
    }
    printf("%.2f
",mmap[0][temp-1]);
}
int main()
{
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    while(~scanf("%d",&n))
    {
        if(n == -1) break;
        p[0].x = 0;
        p[0].y = 5;
        temp = 1;
        memset(path,0,sizeof(path));
        for(int i=0; i<n; i++)
        {
            scanf("%lf",&xx[i]);
            for(int j=1; j<=4; j++)
            {
                p[temp].x = xx[i];
                scanf("%lf",&p[temp].y);
                yy[i][j] = p[temp].y;
                temp++;
            }
        }
        p[temp].x = 10;
        p[temp].y = 5;
        temp++;
        for(int i=0; i<temp; i++)
        {
            for(int j=0; j<temp; j++)
            {
                mmap[i][j] = INF;
            }
        }
        for(int i=0; i<temp; i++)
        {
            for(int j=i+1; j<temp; j++)
            {
                if(judge(p[i],p[j]))
                {
                    //cout<<p[i].x<<"  "<<p[i].y<<"   "<<p[j].x<<"  "<<p[j].y<<endl;
                    mmap[i][j] = dis(p[i],p[j]);
                }
            }
        }
        floyd();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chenyang920/p/4733170.html