1. 程式人生 > >【POJ - 3304 】Segments(計算幾何,思想轉化,直線和線段相交)

【POJ - 3304 】Segments(計算幾何,思想轉化,直線和線段相交)

題幹:

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 x1yxy2 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!

題目大意:

     給出n條線段,判斷是否存在有一條直線,滿足所有的線段在直線上投影后至少有一個公共點。

解題報告:

    原命題等價為存在一條直線穿過所有的線段(易知過公共點且垂直於所求直線的直線符合條件,設為直線a),該命題又等價於從所有線段中任選兩端點形成的直線存在可以穿過所有的線段的直線(可將a平移至一條線段端點,然後繞這點旋轉,使a過另一條線段端點)

  這樣我們只需要列舉頂點就可以了,把直線與線段的直線問題轉化成了線段的端點!!

AC程式碼:

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const double eps = 1e-8;
int sgn(double x) {
	if(fabs(x) < eps)return 0;
	if(x < 0) return -1;
	return 1;
}
struct Point {
	double x,y;
	Point(){}
	Point(double x,double y):x(x),y(y){}
	Point operator -(const Point &b)const {
		return Point(x - b.x,y - b.y);
	}
	double operator ^(const Point &b)const {
		return x*b.y - y*b.x;
	}
	double operator *(const Point &b)const {
		return x*b.x + y*b.y;
	}
};
struct Line {
	Point s,e;
	Line() {}
	Line(Point s,Point e):s(s),e(e){}
};
double xmult(Point p0,Point p1,Point p2) { //p0p1 X p0p2
	return (p1-p0)^(p2-p0);
}
bool Seg_inter_line(Line l1,Line l2) { //判斷直線l1和線段l2是否相交
	return sgn(xmult(l2.s,l1.s,l1.e))*sgn(xmult(l2.e,l1.s,l1.e)) <= 0;
}
double dist(Point a,Point b) {
	return sqrt((b - a)*(b - a));
}
const int MAX = 110;
Line line[MAX];
int n;
bool ok(Line l1) {
	if(sgn(dist(l1.s,l1.e)) == 0 )return 0;//如果說是個點的話。 
	for(int i = 1; i <= n; i++)
		if(Seg_inter_line(l1,line[i]) == 0) return 0;
	return 1;
}
int main() 
{
	int t;
	cin>>t;
	while(t--) {
		scanf("%d",&n);
		double x1,y1,x2,y2;
		for(int i = 1; i<=n; i++) {
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			line[i] = Line(Point(x1,y1),Point(x2,y2));
		}
		bool flag = 0;
		for(int i = 1; i<=n; i++)
			for(int j = 1; j<=n; j++)
				if(ok(Line(line[i].s,line[j].s)) || ok(Line(line[i].s,line[j].e))|| ok(Line(line[i].e,line[j].s)) || ok(Line(line[i].e,line[j].e)) ) {
					flag = 1;
					break;
				}
		if(flag) puts("Yes!");
		else puts("No!");
	}
	return 0;
}

總結:

  注意牽扯異或預算的,都需要加括號,因為異或的優先順序比較低,類似於if( (p^q) <= 0) 這種,必須要加括號!!(p和q都是Point型別)