POJ 2318 TOYS(点在多边形内判定 刘汝佳模板)
来源:互联网 发布:三大中文数据库优缺点 编辑:程序博客网 时间:2024/05/29 06:58
POJ 2318 TOYS(点在多边形内判定)
http://poj.org/problem?id=2318
题意:
有一个平行于坐标轴的长矩形,被n块木板分成了n+1个包间.然后给你一些点的坐标,问你每个包间各包含了几个点?
分析:
直接求出每个包间的4个点坐标(按时针顺序),然后对于每个点,用点在多边形内的模板直接判定即可.
判断点在多边形内可以直接判断4个叉积即可,因为每个多边形都是凸4边行.
这题有直接普通的遍历判断差积的话时间1630ms,时间非常久,如果用二分去做的话只有63ms!!!
直接遍历代码:
#include<cstdio>#include<cstring>#include<cmath>using namespace std;const double eps=1e-10;int dcmp(double x){ if(fabs(x)<eps) return 0; return x<0?-1:1;}const int maxn=5000+10;struct Point{ double x,y; Point(){} Point(double x,double y):x(x),y(y){}};typedef Point Vector;Vector operator-(Point A,Point B){ return Vector(A.x-B.x,A.y-B.y);}double Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y;}double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}bool InSegment(Point P,Point A,Point B){ return dcmp(Cross(A-B,P-A))==0 && dcmp(Dot(A-P,B-P))<=0;}/*//本题由于都是4边行,必须加快速度判断.//如果直接用下面这个函数模板,将超时bool IsPointInPolygon(Point p,Point *poly,int n){ int wn=0; for(int i=0;i<n;++i) { if(InSegment(p, poly[(i+1)%n], poly[i]) ) return true; int k=dcmp( Cross(poly[(i+1)%n]-poly[i], p-poly[i] ) ); int d1=dcmp( poly[i].y-p.y ); int d2=dcmp( poly[(i+1)%n].y-p.y ); if(k>0 && d1<=0 && d2>0) ++wn; if(k<0 && d2<=0 && d1>0) --wn; } if(wn!=0) return true; return false;}*/bool IsPointInPolygon(Point p,Point *poly){ for(int i=0;i<4;++i) if(dcmp (Cross(poly[(i+1)%4]-poly[i], p-poly[i]) )>0 ) return false; return true;}Point p[maxn];//需要判断位置的所有点Point up[maxn],down[maxn];//记录上一排的n+1个点,和下一排的n+1个点Point poly[maxn][4];int ans[maxn];//保存第i个隔间有几个点int main(){ int n,m; double x1,y1,x2,y2; bool first=true; while(scanf("%d",&n)==1 && n) { if(!first) printf("\n"); first=false; scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2); up[0]=Point(x1,y1); up[n+1]=Point(x2,y1); down[0]=Point(x1,y2); down[n+1]=Point(x2,y2); for(int i=1;i<=n;++i) { scanf("%lf%lf",&x1,&x2); up[i]=Point(x1,y1); down[i]=Point(x2,y2); } for(int i=0;i<=n;++i) { poly[i][0]=up[i]; poly[i][1]=up[i+1]; poly[i][2]=down[i+1]; poly[i][3]=down[i]; } memset(ans,0,sizeof(ans)); for(int i=1;i<=m;++i) { double x,y; scanf("%lf%lf",&x,&y); for(int j=0;j<=n;++j) if(IsPointInPolygon(Point(x,y), poly[j] )) { ans[j]++; break; } } for(int i=0;i<=n;++i) printf("%d: %d\n",i,ans[i]); } return 0;}二分查找 63ms:
#include<cstdio>#include<cstring>#include<cmath>using namespace std;const double eps=1e-10;int dcmp(double x){ if(fabs(x)<eps) return 0; return x<0?-1:1;}const int maxn=5000+10;struct Point{ double x,y; Point(){} Point(double x,double y):x(x),y(y){}};typedef Point Vector;Vector operator-(Point A,Point B){ return Vector(A.x-B.x,A.y-B.y);}double Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y;}double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}Point p[maxn];//需要判断位置的所有点Point up[maxn],down[maxn];//记录上一排的n+1个点,和下一排的n+1个点Point poly[maxn][4];int ans[maxn];//保存第i个隔间有几个点int main(){ int n,m; double x1,y1,x2,y2; bool first=true; while(scanf("%d",&n)==1 && n) { if(!first) printf("\n"); first=false; scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2); up[0]=Point(x1,y1); up[n+1]=Point(x2,y1); down[0]=Point(x1,y2); down[n+1]=Point(x2,y2); for(int i=1;i<=n;++i) { scanf("%lf%lf",&x1,&x2); up[i]=Point(x1,y1); down[i]=Point(x2,y2); } memset(ans,0,sizeof(ans)); for(int i=1;i<=m;++i) { double x,y; scanf("%lf%lf",&p[i].x,&p[i].y); int l = 1,r = 1+n; int tmp; while( l <= r) { int mid = (l + r)/2; if(dcmp (Cross(up[mid]-down[mid], p[i]-down[mid]) )>0) { tmp = mid; r = mid - 1; } else l = mid + 1; } ans[tmp-1]++; } for(int i=0;i<=n;++i) printf("%d: %d\n",i,ans[i]); } return 0;}
阅读全文
0 0
- POJ 2318 TOYS(点在多边形内判定 刘汝佳模板)
- POJ 2318 TOYS(点在多边形内判定)
- poj 2318 TOYS(判断点在多边形内+二分查找)
- 点在多边形内判定
- UVA 634 Polygon(模板题:判定点在多边形内)
- POJ 2318 判定点在多边形内外(二分)
- 几何模板总结(二):点在多边形内判定(转角法)
- 模板[点在多边形内的判断]
- ZOJ 1081 Points Within(点在多边形内判定)
- HDU 1756 Cupid's Arrow(判定点在多边形内)
- HDU 1756 Cupid's Arrow(点在多边形内判定)
- ZOJ 1081 Points Within(点在多边形内判定)
- 【解题报告】 POJ 2318 TOYS -- 判断点在某个梯形内 叉积 + 二分
- poj 2318 TOYS(叉积判断点是否在四边形内)
- POJ 1569 Myacm Triangles(判定点在三角形内)
- POJ 2398 Toy Storage(判定点在四边形内)
- poj 2318 TOYS--点在直线左侧还是右侧
- POJ 2318 Toys(判断点在直线的方向)
- 转载:unity坐标系详解
- Thrift c++ server & java client配置
- Redis源码剖析--简单动态字符串sds
- 「音视频直播技术」OpenGL渲染之距阵变换
- 字符串的基本操作
- POJ 2318 TOYS(点在多边形内判定 刘汝佳模板)
- SpringMVC与MyBatis的整合
- Redis源码剖析-链表list
- 第31讲:永久存储:腌制一缸美味的泡菜
- 文章标题
- E: Unable to lock directory /var/lib/apt/lists/_在Ubuntu下解决E: 加锁的问题
- 1298:The Hardest Problem Ever
- spring的xml中注册bean的时候报错1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 <xsd:schema>
- linux 命令备忘随笔