当前位置: 代码迷 >> 综合 >> LA 2797 Monster Trap 平面直线图(PSLG) -
  详细解决方案

LA 2797 Monster Trap 平面直线图(PSLG) -

热度:77   发布时间:2023-09-23 03:54:06.0

题目地址:https://vjudge.net/problem/UVALive-2797

有一个函数参数 Point p,一点要改成Point& p 才对,不知道为什么


#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b)  for(int i=a;i<=(int)(b);++i)
#define REPD(i,a,b) for(int i=a;i>=(int)(b);--i)
const double PI=acos(-1);
const double EPS=1e-12;   
int dcmp(double x){if(fabs(x)<EPS) return 0;return x > 0 ? 1 : -1;
}
struct Point{double x,y;Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
bool operator < (const Point& p1, const Point& p2){	return p1.x<p2.x || (p1.x==p2.x && p1.y<p2.y) ;}
Vector operator / (const Point& A, double x){	return Vector{A.x/x, A.y/x};}
Vector operator * (const Vector& A, double x){	return Vector{A.x*x, A.y*x};}
Vector operator - (const Vector& A, const Vector& B){	return Vector{A.x-B.x,A.y-B.y};}
Vector operator + (const Point& A, const Vector& v){	return Point{A.x+v.x,A.y+v.y};}
Vector Rotate(const Point& p,double ang){	return Vector{p.x*cos(ang)-p.y*sin(ang),p.x*sin(ang)+p.y*cos(ang)};}
double Cross(const Vector& A, const Vector& B){	return A.x*B.y-A.y*B.x;}
double Dot(Vector A, Vector B){	return A.x*B.x+A.y*B.y;}
double Length(Vector v){	return sqrt(fabs(Dot(v,v)));}
Vector Unit(Vector v){	return v/Length(v);}const int maxn=100+5;
Point p1[maxn],p2[maxn],points[maxn*2];
bool G[maxn*2][maxn*2];
int n;
bool OnSegment(Point p, Point A, Point B){return dcmp(Cross(A-p, B-p))==0 && dcmp(Dot(A-p, B-p))<0 ;
}
bool OnAngSegment(Point p){REP(i,0,n-1) if(OnSegment(p,p1[i],p2[i])) return true;return false;
}                             //不加&引用答案就不对...
bool SegmentProperIntersection(Point& a1, Point& a2, Point& b1, Point& b2){double c1=Cross(a2-a1, b1-a1),c2=Cross(a2-a1, b2-a1),c3=Cross(b2-b1, a1-b1),c4=Cross(b2-b1, a2-b1);return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0 ;
}
bool IntersectWithAngSegment(Point A, Point B){REP(i,0,n-1) if(SegmentProperIntersection(A, B, p1[i], p2[i])) return true;return false;
}
int pCnt;
bool vis[maxn*2];
bool DFS(int u){if(u==1) return true;vis[u]=true;REP(v,0,pCnt-1) if(G[u][v] && !vis[v] && DFS(v)) return true;return false;
}
int main(int argc, char const *argv[])
{// freopen("input.in","r",stdin);while(scanf("%d",&n)==1&&n){REP(i,0,n-1) {double x1,x2,y1,y2;scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);Point a=Point(x1,y1),b=Point(x2,y2);Vector v=Unit(b-a);p1[i]=a-v*1e-6;p2[i]=b+v*1e-6;}pCnt=0;points[pCnt++]=Point(0,0);points[pCnt++]=Point(1e5,1e5);REP(i,0,n-1) {if(!OnAngSegment(p1[i])) points[pCnt++]=p1[i];if(!OnAngSegment(p2[i])) points[pCnt++]=p2[i];}memset(G,false,sizeof(G));REP(i,0,pCnt-1) REP(j,i+1,pCnt-1) if(!IntersectWithAngSegment(points[i], points[j]))G[i][j]=G[j][i]=true;memset(vis,false,sizeof(vis));printf("%s\n", !DFS(0)?"yes":"no");}return 0;
}