The Doors POJ - 1556(几何+图)

Posted vampire6

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Doors POJ - 1556(几何+图)相关的知识,希望对你有一定的参考价值。

The Doors

题目链接:https://vjudge.net/problem/POJ-1556

题目:

技术图片

 

 技术图片

 

 思路:就是判断线段与线段是否相交,符合条件就加入图中,建完图后跑dij就行了,求最短路,,看了大神的。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#define Max 105
#define eps 1e-8
#define Inf 0x7fffffff
using namespace std;
int head[Max],cnt;
struct
{
    int e;
    double w;
    int next;
}edge[Max*Max];
struct Point
{
    double x,y;
}point[Max];
struct Seg
{
    Point a,b;
}seg[Max];
void add(int s,int e,double w)
{
    edge[cnt].e=e;
    edge[cnt].w=w;
    edge[cnt].next=head[s];
    head[s]=cnt++;
}
double distance(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int dblcmp(double d)
{
    if(fabs(d)<eps) return 0;
    return d>0?1:-1;
}
double det(double x1,double y1,double x2,double y2)
{
    return x1*y2-x2*y1;
}
double cross(Point a,Point b,Point c)
{
    return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
int segcross(Point a,Point b,Point c,Point d)
{
    return (dblcmp(cross(a,c,d))^dblcmp(cross(b,c,d)))==-2&&
           (dblcmp(cross(c,a,b))^dblcmp(cross(d,a,b)))==-2;
}
double spfa(int n)
{
    int i,st,ed;
    bool visit[Max];
    double dis[Max];
    queue<int> q;
    memset(visit,false,sizeof(visit));
    visit[0]=true;
    for(i=1;i<=n;i++)
        dis[i]=Inf*1.0;
    dis[0]=0;
    q.push(0);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        visit[st]=false;
        for(i=head[st];i!=-1;i=edge[i].next)
        {
            ed=edge[i].e;
            if(dis[ed]>dis[st]+edge[i].w)
            {
                dis[ed]=dis[st]+edge[i].w;
                if(!visit[ed])
                {
                    visit[ed]=true;
                    q.push(ed);
                }
            }
        }
    }
    return dis[n];
}
int main()
{
    //freopen("text","r",stdin);
    int i,j,k,n,t,p;
    bool flag;
    double x,y[6];
    while(~scanf("%d",&n))
    {
        if(n==-1)
            break;
        cnt=p=0;
        memset(head,-1,sizeof(head));
        t=1;
        point[0].x=0.0;point[0].y=5.0;
        for(i=1;i<=n;i++)
        {
            scanf("%lf",&x);
            y[0]=0.0;y[5]=10.0;
            for(j=1;j<=4;j++)
            {
                scanf("%lf",&y[j]);
                point[++p].x=x;
                point[p].y=y[j];
            }
            seg[t].a.x=x;seg[t].a.y=y[0];
            seg[t].b.x=x;seg[t++].b.y=y[1];
            seg[t].a.x=x;seg[t].a.y=y[2];
            seg[t].b.x=x;seg[t++].b.y=y[3];
            seg[t].a.x=x;seg[t].a.y=y[4];
            seg[t].b.x=x;seg[t++].b.y=y[5];
        }
        point[++p].x=10.0;point[p].y=5.0;
        for(i=0;i<=p;i++)
            for(j=i+1;j<=p;j++)
            {
                flag=true;
                for(k=1;k<t;k++)
                {
                    if(point[i].x!=seg[k].a.x&&point[j].x!=seg[k].a.x&&segcross(point[i],point[j],seg[k].a,seg[k].b))
                    {
                        flag=false;
                        break;
                    }
                }
                if(flag)
                    add(i,j,distance(point[i].x,point[i].y,point[j].x,point[j].y));
            }
        printf("%.2f
",spfa(p));
    }
    return 0;
}

以上是关于The Doors POJ - 1556(几何+图)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1556 The Doors 线段交 dijkstra

poj1556 The Doors 2012-01-11

POJ1556 The Doors 叉积+最短路

POJ-1556 The Doors

POJ 1556 The Doors

POJ_1556_The Doors_判断线段相交+最短路