日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 3304 】Segments(计算几何,思想转化,直线和线段相交)

發(fā)布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3304 】Segments(计算几何,思想转化,直线和线段相交) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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?x1y1?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!

題目大意:

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

解題報告:

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

? 這樣我們只需要枚舉頂點(diǎn)就可以了,把直線與線段的直線問題轉(zhuǎn)化成了線段的端點(diǎn)!!

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 p0p2return (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;//如果說是個點(diǎn)的話。 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; }

總結(jié):

? 注意牽扯異或預(yù)算的,都需要加括號,因為異或的優(yōu)先級比較低,類似于if( (p^q) <= 0) 這種,必須要加括號!!(p和q都是Point類型)

總結(jié)

以上是生活随笔為你收集整理的【POJ - 3304 】Segments(计算几何,思想转化,直线和线段相交)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。