POJ3304 Segments

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5604 Accepted: 1630

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


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

const double eps = 1e-8;

struct LPoint{
	double x, y;
};

struct LLine{
	LPoint bg, ed;
}line[110];

int dblcmp(double a, double b){
	if (a - b >= eps) return 1;
	if (b - a >= eps) return -1;
	return 0;
}

double VecPro(double x1, double y1, double x2, double y2){
	return (x1 * y2 - x2 * y1);
}

double NumPro(double x1, double y1, double x2, double y2){
	return (x1 * x1 + y1 * y2);
}
//直线ab,与线段pq是否相交
int IsCross(LPoint a, LPoint b, LPoint p, LPoint q){
	if (dblcmp(VecPro(a.x - b.x, a.y - b.y, p.x - b.x, p.y - b.y), 0) *
	    dblcmp(VecPro(a.x - b.x, a.y - b.y, q.x - b.x, q.y - b.y), 0) <= 0)
		return 1;
	else return 0;
}

double Dis(LPoint a, LPoint b){
	return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int main(){
	int i, j, k;
	int n, T;
	scanf("%d", &T);
	while(T--){
		scanf("%d", &n);
		for (i = 0; i < n; i++){
			scanf("%lf %lf %lf %lf", &line[i].bg.x, &line[i].bg.y, &line[i].ed.x, &line[i].ed.y);
		}
		if (n <= 2) k = n;
		else{
			k = 0;
			for (i = 0; i < n; i++){
				for (j = i + 1; j < n; j++){
					if (dblcmp(Dis(line[i].bg, line[j].bg), 0) > 0){
						for (k = 0; k < n; k++){
							if (!IsCross(line[i].bg, line[j].bg, line[k].bg, line[k].ed)) break;
						}
						if (k >= n) break;
					}
					if (dblcmp(Dis(line[i].bg, line[j].ed), 0) > 0){
						for (k = 0; k < n; k++){
							if (!IsCross(line[i].bg, line[j].ed, line[k].bg, line[k].ed)) break;
						}
						if (k >= n) break;
					}
					if (dblcmp(Dis(line[i].ed, line[j].bg), 0) > 0){
						for (k = 0; k < n; k++){
							if (!IsCross(line[i].ed, line[j].bg, line[k].bg, line[k].ed)) break;
						}
						if (k >= n) break;
					}
					if (dblcmp(Dis(line[i].ed, line[j].ed), 0) > 0){
						for (k = 0; k < n; k++){
							if (!IsCross(line[i].ed, line[j].ed, line[k].bg, line[k].ed)) break;
						}
						if (k >= n) break;
					}
				}
				if (k >= n) break;
			}
		}
		if (k >= n) printf("Yes!\n");
		else printf("No!\n");
	}	
	return 0;
}
/*
题意:给n个线段,问是否存在一直线,使所有线段在该直线上投影相交于同一点
转帖discuss里hanjialong的思路:
	首先题中的要求等价于:存在一条直线l和所有的线段都相交。
	证明:若存在一条直线l和所有线段相交,作一条直线m和l垂直,则m就是题中要求的直线,所有线段投影的一个公共点即为垂足。(l和每条线段的交点沿l投影到m上的垂足处)
	反过来,若存在m,所有线段在m上的投影有公共点,则过这点垂直于m作直线l,l一定和所有线段相交。

	然后证存在l和所有线段相交等价于存在l过某两条线段的各一个端点且和所有线段相交。
	充分性显然。必要性:若有l和所有线段相交,则可保持l和所有线段相交,左右平移l到和某一线段交于端点停止(“移不动了”)。然后绕这个交点旋转。也是转到“转不动了”(和另一线段交于其一个端点)为止。这样就找到了一个新的l。

	于是本题可归结为枚举两两线段的各一个端点,连一条直线,再判断剩下的线段是否都和这条直线有交点。
计算几何还是难想啊...
然后就是俩点注意,n <= 2特判为yes, 作为直线的俩点不能重合
dblcmp写错了,贡献一WA...真是不细心啊...
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值