您的位置:首页 > 其它

POJ 3304 segments (直线与线段相交)

2014-10-26 19:48 363 查看
[align=center]Segments[/align]

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 9837Accepted: 3030
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 (x1,
y1) and (x2, y2) 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!

题意:题中给出一些线段,问这些线段在某一个方向上的投影是否能存在公共点;

题解:
很容易想到,要是在某个方向上投影能够存在公共点的话,那么必存在一条直线可以与所有的直线相交,另外弱国存在该直线的话,该直线经过一定旋转后必存在两点是原线段中的某两点,那么该题就转化为枚举直线看是否存在一条直线能和所有的线段相交。

题目链接:http://poj.org/problem?id=3304

判断直线与线段相交的方法:利用叉积

代码:
/**********
Problem: 3304		User: pluto227
Memory: 160K		Time: 32MS
Language: C++		Result: Accepted
**********/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#define eps 1e-8
#define LL long long
using namespace std;

const int N = 200;

struct Point {
double x;
double y;
} point
;

inline double cross(Point a,Point b,Point c) {
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

inline double dist(double x1,double y1,double x2,double y2) {
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

inline bool judge(Point a,Point b,int n) {
if((dist(a.x,a.y,b.x,b.y))<eps) return false;
for(int t=1; t<=n; t++) {
int mk=2*t-1;
if(cross(a,b,point[mk])*cross(a,b,point[mk+1])>eps) return false;
}
return true;
}

int main() {
int T;
scanf("%d",&T);
while(T--) {
int n,k=1;
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%lf %lf",&point[k].x,&point[k].y);
k++;
scanf("%lf %lf",&point[k].x,&point[k].y);
k++;
}
bool ans=false;
if(n==1) {
printf("Yes!\n");
continue;
}
for(int i=1; i<=k-1; i++) {
for(int j=i+1; j<=k-1; j++) {
if(judge(point[i],point[j],n)){
ans=true;
}
}
}
if(ans) printf("Yes!\n");
else printf("No!\n");
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: