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

时间:2024-01-15 09:50:02

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

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

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
const int N=,M=N*N;
const double INF=0x3f3f3f3f;
const double eps=1e-;
int sgn(double x){
if(fabs(x)<eps) return ;
if(x>) return ;
return -;
}
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))< &&
sgn(cal(C,A,B))*sgn(cal(D,A,B))<=;
}
//最短路部分
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=;
memset(p,,sizeof(p));
for(int i=;i<=n;i++) d[i]=INF;
q[++r]=;d[]=;
int u,v,i;
double w;
while(l<r){
p[u=q[++l]]=;
for(i=head[u];i!=-;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]=;
}
}
}
}
printf("%.2f\n",d[n]);
} int main(){
int t,i,j,k,js;
double x,y;
while(scanf("%d",&t)!=EOF&&t!=-){
cnt=js=n=;po[]=point(,);
memset(head,-,sizeof(head));
//¹¹½¨µãºÍÏß
while(t--){
scanf("%lf",&x);
for(i=;i<=;i++){
scanf("%lf",&y);
po[++n]=point(x,y);
if(i==) li[++js]=line(point(x,),po[n]);
else if(i==) li[++js]=line(po[n],point(x,)) ;
else if(i==) li[++js]=line(po[n-],po[n]);
}
}
po[++n]=point(,);
//½¨Í¼
for(i=;i<=n;i++){
for(j=i+;j<=n;j++){
int f=;
for(k=;k<=js;k++){
if(xj(li[k],line(po[i],po[j]))){
f=;
break;
}
}
if(!f){
double tmp=dis(po[i],po[j]);
add(i,j,tmp);
add(j,i,tmp);
}
}
}
spfa();
}
return ;
}