POJ 1066 Treasure Hunt (线段相交)

题意:

从正方形外围任意一点出发,到达指定点最少通过几次线段?(每次只能从线段中点经过)

题解:思路一:枚举外围起点,连接该点与指定点,相交次数最少的即为答案。思路二:先求出所有线段的中点。以指定点位起点bfs,走到正方形外面的最小步数即是结果。搜索过程中,相邻点(u,v)满足条件:u,v的连线不与其他任何一条线段规范相交。

#include<cmath>
#include<cstdio>
#include<algorithm>
using std::sort;

#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
struct Point{double x,y;};
struct Line{Point a,b;};

//计算cross product (P1-P0)x(P2-P0)
double xmult(Point p1,Point p2,Point p0){
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double xmult(double x1,double y1,double x2,double y2,double x0,double y0){
	return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);
}

//计算dot product (P1-P0).(P2-P0)
double dmult(Point p1,Point p2,Point p0){
	return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}
double dmult(double x1,double y1,double x2,double y2,double x0,double y0){
	return (x1-x0)*(x2-x0)+(y1-y0)*(y2-y0);
}

//两点距离
double distance(Point p1,Point p2){
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double distance(double x1,double y1,double x2,double y2){
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

//判三点共线
int dots_inLine(Point p1,Point p2,Point p3){
	return zero(xmult(p1,p2,p3));
}
int dots_inLine(double x1,double y1,double x2,double y2,double x3,double y3){
	return zero(xmult(x1,y1,x2,y2,x3,y3));
}

//判点是否在线段上,包括端点
int dot_onLine_in(Point p,Line l){
	return zero(xmult(p,l.a,l.b))&&(l.a.x-p.x)*(l.b.x-p.x)<eps&&(l.a.y-p.y)*(l.b.y-p.y)<eps;
}
int dot_onLine_in(Point p,Point l1,Point l2){
	return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;
}
int dot_onLine_in(double x,double y,double x1,double y1,double x2,double y2){
	return zero(xmult(x,y,x1,y1,x2,y2))&&(x1-x)*(x2-x)<eps&&(y1-y)*(y2-y)<eps;
}

//判点是否在线段上,不包括端点
int dot_onLine_ex(Point p,Line l){
	return dot_onLine_in(p,l)&&(!zero(p.x-l.a.x)||!zero(p.y-l.a.y))&&(!zero(p.x-l.b.x)||!zero(p.y-l.b.y));
}
int dot_onLine_ex(Point p,Point l1,Point l2){
	return dot_onLine_in(p,l1,l2)&&(!zero(p.x-l1.x)||!zero(p.y-l1.y))&&(!zero(p.x-l2.x)||!zero(p.y-l2.y));
}
int dot_onLine_ex(double x,double y,double x1,double y1,double x2,double y2){
	return dot_onLine_in(x,y,x1,y1,x2,y2)&&(!zero(x-x1)||!zero(y-y1))&&(!zero(x-x2)||!zero(y-y2));
}

//判两点在线段同侧,点在线段上返回0
int same_side(Point p1,Point p2,Line l){
	return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)>eps;
}
int same_side(Point p1,Point p2,Point l1,Point l2){
	return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;
}

//判两点在线段异侧,点在线段上返回0
int opposite_side(Point p1,Point p2,Line l){
	return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)<-eps;
}
int opposite_side(Point p1,Point p2,Point l1,Point l2){
	return xmult(l1,p1,l2)*xmult(l1,p2,l2)<-eps;
}

//判两直线平行
int parallel(Line u,Line v){
	return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y));
}
int parallel(Point u1,Point u2, Point v1,Point v2){
	return zero((u1.x-u2.x)*(v1.y-v2.y)-(v1.x-v2.x)*(u1.y-u2.y));
}

//判两直线垂直
int perpendicular(Line u,Line v){
	return zero((u.a.x-u.b.x)*(v.a.x-v.b.x)+(u.a.y-u.b.y)*(v.a.y-v.b.y));
}
int perpendicular(Point u1,Point u2,Point v1,Point v2){
	return zero((u1.x-u2.x)*(v1.x-v2.x)+(u1.y-u2.y)*(v1.y-v2.y));
}

//判两线段相交,包括端点和部分重合
int intersect_in(Line u,Line v){
	if (!dots_inLine(u.a,u.b,v.a)||!dots_inLine(u.a,u.b,v.b))
		return !same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u);
	return dot_onLine_in(u.a,v)||dot_onLine_in(u.b,v)||dot_onLine_in(v.a,u)||dot_onLine_in(v.b,u);
}
int intersect_in(Point u1,Point u2,Point v1,Point v2){
	if (!dots_inLine(u1,u2,v1)||!dots_inLine(u1,u2,v2))
		return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
	return dot_onLine_in(u1,v1,v2)||dot_onLine_in(u2,v1,v2)||dot_onLine_in(v1,u1,u2)||dot_onLine_in(v2,u1,u2);
}

//判两线段相交,不包括端点和部分重合
int intersect_ex(Line u,Line v){
	return opposite_side(u.a,u.b,v)&&opposite_side(v.a,v.b,u);
}
int intersect_ex(Point u1,Point u2,Point v1,Point v2){
	return opposite_side(u1,u2,v1,v2)&&opposite_side(v1,v2,u1,u2);
}

//计算两直线交点,注意事先判断直线是否平行!
//线段交点请另外判线段相交(同时还是要判断是否平行!)
Point intersection(Line u,Line v){
	Point ret=u.a;
	double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))
			/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
	ret.x+=(u.b.x-u.a.x)*t;
	ret.y+=(u.b.y-u.a.y)*t;
	return ret;
}
Point intersection(Point u1,Point u2,Point v1,Point v2){
	Point ret=u1;
	double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
			/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
	ret.x+=(u2.x-u1.x)*t;
	ret.y+=(u2.y-u1.y)*t;
	return ret;
}

//点到直线上的最近点
Point ptoLine(Point p,Line l){
	Point t=p;
	t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x;
	return intersection(p,t,l.a,l.b);
}
Point ptoLine(Point p,Point l1,Point l2){
	Point t=p;
	t.x+=l1.y-l2.y,t.y+=l2.x-l1.x;
	return intersection(p,t,l1,l2);
}

//点到直线距离
double disptoLine(Point p,Line l){
	return fabs(xmult(p,l.a,l.b))/distance(l.a,l.b);
}
double disptoLine(Point p,Point l1,Point l2){
	return fabs(xmult(p,l1,l2))/distance(l1,l2);
}
double disptoLine(double x,double y,double x1,double y1,double x2,double y2){
	return fabs(xmult(x,y,x1,y1,x2,y2))/distance(x1,y1,x2,y2);
}

//点到线段上的最近点
Point ptoseg(Point p,Line l){
	Point t=p;
	t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x;
	if (xmult(l.a,t,p)*xmult(l.b,t,p)>eps)
		return distance(p,l.a)<distance(p,l.b)?l.a:l.b;
	return intersection(p,t,l.a,l.b);
}
Point ptoseg(Point p,Point l1,Point l2){
	Point t=p;
	t.x+=l1.y-l2.y,t.y+=l2.x-l1.x;
	if (xmult(l1,t,p)*xmult(l2,t,p)>eps)
		return distance(p,l1)<distance(p,l2)?l1:l2;
	return intersection(p,t,l1,l2);
}

//点到线段距离
double disptoseg(Point p,Line l){
	Point t=p;
	t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x;
	if (xmult(l.a,t,p)*xmult(l.b,t,p)>eps)
		return distance(p,l.a)<distance(p,l.b)?distance(p,l.a):distance(p,l.b);
	return fabs(xmult(p,l.a,l.b))/distance(l.a,l.b);
}
double disptoseg(Point p,Point l1,Point l2){
	Point t=p;
	t.x+=l1.y-l2.y,t.y+=l2.x-l1.x;
	if (xmult(l1,t,p)*xmult(l2,t,p)>eps)
		return distance(p,l1)<distance(p,l2)?distance(p,l1):distance(p,l2);
	return fabs(xmult(p,l1,l2))/distance(l1,l2);
}

//矢量V以P为顶点逆时针旋转angle并放大scale倍
Point rotate(Point v,Point p,double angle,double scale){
	Point ret=p;
	v.x-=p.x,v.y-=p.y;
	p.x=scale*cos(angle);
	p.y=scale*sin(angle);
	ret.x+=v.x*p.x-v.y*p.y;
	ret.y+=v.x*p.y+v.y*p.x;
	return ret;
}

Point getMid ( Point a, Point b )
{
	Point tmp;
	tmp.x = (a.x+b.x) / 2;
	tmp.y = (a.y+b.y) / 2;
	return tmp;
}

bool cmp ( const Point & a, const Point & b )
{
	Point O;
	O.x = O.y = 0;
	int tmp = xmult(a,b,O);
	if ( zero(tmp) )
		return distance(a,O) < distance(b,O);
	return tmp > 0;
}

Point p[1000];
Line l[1000];

int main()
{
	int n;
	Point target, mid;
	scanf("%d",&n);
	int i, j, c = 0;
	for ( i = 0; i < n; i++ )
	{
		scanf("%lf%lf%lf%lf", &l[i].a.x, &l[i].a.y, &l[i].b.x, &l[i].b.y);
		p[c].x = l[i].a.x; p[c++].y = l[i].a.y;
		p[c].x = l[i].b.x; p[c++].y = l[i].b.y;
	}
	scanf("%lf%lf",&target.x, &target.y);

	p[c].x = 0; p[c++].y = 0;
	p[c].x = 0; p[c++].y = 100;
	p[c].x = 100; p[c++].y = 0;
	p[c].x = 100; p[c].y = 100;

	sort(p,p+c,cmp);
	int ans = 99999999, step;
	for ( i = 1; i <= c; i++ )
	{
		mid = getMid(p[i],p[i-1]);
		for ( j = step = 0; j < n; j++ )
			if ( intersect_ex(mid, target, l[j].a, l[j].b) ) step++;
		ans = step < ans ? step : ans;
	}
	printf("Number of doors = %d\n", ans+1);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值