Segments(问题抽象与转化+暴力枚举+线段与直线判交)

73 篇文章 0 订阅
35 篇文章 0 订阅


Link:http://poj.org/problem?id=3304


Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10959 Accepted: 3429

Description

Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

Input

Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1y1) and (x2y2) are the coordinates of the two endpoints for one of the segments.

Output

For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a -b| < 10-8.

Sample Input

3
2
1.0 2.0 3.0 4.0
4.0 5.0 6.0 7.0
3
0.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
1.0 1.0 2.0 1.0
3
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0

Sample Output

Yes!
Yes!
No!

Source


题意:给n条线段,问是否存在一条直线,所有线段在直线上的投影有公共点

编程思想:这个问题等价于:是否存在一条直线,与所有线段都有交点

证明我不会,下面证明思路是参考某大牛的。


证明思路:首先,如果线段AB在直线L上的投影为A'B',那么A'B'之间的任意点C',过它做L的垂线,与AB必然有交点,同样的,AB之间的任意点C,过它做AB的垂线,与A'B'必然有交点。这个结论可以推广到N条线段,那么如果N条线段在某条直线上的投影有公共点,过这个公共点做这条直线的垂线,该垂线必然与N条线段都相交。反过来也一样。所以,我们只要找到一条直线与N条线段都相交,那么,做这条直线的垂线,就能找到题目要求的那条直线。至于怎么找与N条线段都相交的直线,我们可以枚举每条线段的两个端点,理由如下:假设直线L现在与N条直线都相交,那么将L平移,直到它不能与N条线段相交,那么现在它至少与一条线段的端点(临界点)相交,此时再把这个端点作为固定点,将L旋转,直到它不能与N条线段相交,那么此时L又必然会与至少一条线段的端点相交,这个两个端点就能找出L了。但要注意的是如果两个端点相同,则视为一个点,不能当作直线。


AC code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#define EPS 1e-8
using namespace std;
struct point{
    double x;
    double y;
}p[44444];
struct v{
    point s;
    point e;
}seg[22222];
v se;
/*语法:result=lineintersect(Point p1,Point p2,Point p3,Point p4);
	参数:
	p1、p2:线段的两个端点
	p3、p4:直线上的两个点
	返回值:0:线段直线不相交;1:线段和直线相交
	注意: 如线段在直线上,返回 1
	源程序:
	typedef struct {
	    double x,y;
	} Point;  */ 
int lineintersect(point p1,point p2,point p3,point p4)
{
    point tp1,tp2,tp3;
    tp1.x=p1.x-p3.x;
    tp1.y=p1.y-p3.y;
    tp2.x=p4.x-p3.x;
    tp2.y=p4.y-p3.y;
    tp3.x=p2.x-p3.x;
    tp3.y=p2.y-p3.y;
    if ((tp1.x*tp2.y-tp1.y*tp2.x)*(tp2.x*tp3.y-tp2.y*tp3.x)>=0) return 1; else return 0;
}
double multi(point p1,point p2,point p0)//叉积 
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int Acoross(struct v v1,struct v v2)//线段相交 
{
    if(max(v1.s.x,v1.e.x)>=min(v2.s.x,v2.e.x)&&
       max(v2.s.x,v2.e.x)>=min(v1.s.x,v1.e.x)&&
       max(v1.s.y,v1.e.y)>=min(v2.s.y,v2.e.y)&&
       multi(v2.s,v1.e,v1.s)*multi(v1.e,v2.e,v1.s)>=0&&
       multi(v1.s,v2.e,v2.s)*multi(v2.e,v1.e,v2.s)>=0)
        return 1;
    return 0;
}
int main()
{
  //freopen("D:\in.txt","r",stdin);
   int n,i,j,k,fg,t,cnt,ans;
   scanf("%d",&t);
   while(t--)
   {
   		scanf("%d",&n);
   		cnt=0;
       for(i=1;i<=n;i++)
       {
           scanf("%lf%lf%lf%lf",&seg[i].s.x,&seg[i].s.y,&seg[i].e.x,&seg[i].e.y);
           p[++cnt]=seg[i].s;
           p[++cnt]=seg[i].e;
       }
       if(n==1) {printf("Yes!\n");continue;}  
	   for(i=1;i<=cnt;i++)//找第一个端点
       {
           for(j=i+1;j<=cnt;j++)//找第二个端点
           {
           		if(fabs(p[i].x-p[j].x)<EPS&&fabs(p[i].y-p[j].y)<EPS)//如果第一和第二个端点相同,跳过
           			continue;
           		ans=0;
           		for(k=1;k<=n;k++)
           		{
	            	if(lineintersect(seg[k].s,seg[k].e,p[i],p[j]))//找出所有线段,并与上述两端点的直线做比较
	            	{
	            		ans++;
					}
					else
						break;
				}
                if(ans==n)
                	break;
           }
           if(ans==n)
                break;
       }
       if(ans==n)
       	 	printf("Yes!\n");
       else
        printf("No!\n");
   }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值