POJ 1556 The Doors【最短路+线段相交】

思路:暴力判断每个点连成的线段是否被墙挡住,构建图。求最短路。

思路很简单,但是实现比较复杂,模版一定要可靠。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
const int N=111,M=N*N;
const double INF=0x3f3f3f3f;
const double eps=1e-8;
int sgn(double x){
    if(fabs(x)<eps)    return 0;
    if(x>0)    return 1;
    return -1;
}
struct point{
    double x,y;
    point(){}
    point(double x_,double y_){
        x=x_,y=y_;
    }
    point operator -(const point &b)const{
        return point(x-b.x,y-b.y);
    }
    double operator *(const point &b)const{
        return x*b.x+y*b.y;
    }
    double operator ^(const point &b)const{
        return x*b.y-y*b.x;
    }
}po[N];
struct line{
    point s,e;
    line(){}
    line(point s_,point e_){
        s=s_,e=e_;
    }
}li[N];
double dis(point a,point b){//距离
    return sqrt((b-a)*(b-a));
}
double cal(point p0,point p1,point p2){//叉积
    return (p1-p0)^(p2-p0);
}
int xj(line a,line b){//判断线段相交
    point A=a.s,B=a.e,C=b.s,D=b.e;
    return 
    max(A.x,B.x)>=min(C.x,D.x) &&
    max(C.x,D.x)>=min(A.x,B.x) &&
    max(A.y,B.y)>=min(C.y,D.y) &&
    max(C.y,D.y)>=min(A.y,B.y) &&
    sgn(cal(A,C,D))*sgn(cal(B,C,D))<0 &&
    sgn(cal(C,A,B))*sgn(cal(D,A,B))<=0;
}
//最短路部分
struct node{
    int v,next;
    double w;
}e[M];
int p[N],head[N],cnt,q[M],l,r,n;
double d[N];
void add(int u,int v,double w){
    e[cnt].v=v,e[cnt].w=w;
    e[cnt].next=head[u],head[u]=cnt++;
}
void spfa(){
    l=r=0;
    memset(p,0,sizeof(p));
    for(int i=1;i<=n;i++)    d[i]=INF;
    q[++r]=0;d[0]=0;
    int u,v,i;
    double w;
    while(l<r){
        p[u=q[++l]]=0;
        for(i=head[u];i!=-1;i=e[i].next){
            w=e[i].w,v=e[i].v;
            if(d[v]>d[u]+w){
                d[v]=d[u]+w;
                if(!p[v]){
                    q[++r]=v;
                    p[v]=1;
                }
            }
        }
    }
    printf("%.2f
",d[n]);
}

int main(){
    int t,i,j,k,js;
    double x,y;
    while(scanf("%d",&t)!=EOF&&t!=-1){
        cnt=js=n=0;po[0]=point(0,5);
        memset(head,-1,sizeof(head));
        //¹¹½¨µãºÍÏß 
        while(t--){
            scanf("%lf",&x);
            for(i=1;i<=4;i++){
                scanf("%lf",&y);
                po[++n]=point(x,y);
                if(i==1)        li[++js]=line(point(x,0),po[n]);
                else if(i==4)    li[++js]=line(po[n],point(x,10))    ;
                else if(i==3)    li[++js]=line(po[n-1],po[n]);
            }
        }
        po[++n]=point(10,5);
        //½¨Í¼ 
        for(i=0;i<=n;i++){
            for(j=i+1;j<=n;j++){
                int f=0;
                for(k=1;k<=js;k++){
                    if(xj(li[k],line(po[i],po[j]))){
                        f=1;
                        break;
                    }
                }
                if(!f){
                    double tmp=dis(po[i],po[j]);
                    add(i,j,tmp);
                    add(j,i,tmp);
                }
            }
        }
        spfa();
    }
    return 0;
}
 
原文地址:https://www.cnblogs.com/L-King/p/5733219.html