您的位置:首页 > 其它

UVA 11072 - Points(凸包+点在多边形内判定)

2014-06-27 18:33 375 查看
题目:Points

题意:给出两个点集A和B,对于B中每个点,问能否在A中找到不同的三个点组成三角形,使得该点落在这个三角形中。

先对A求凸包,很明显,如果一个点落在A的凸包里,那么肯定能在A中找到一个三角形使得该点在里面,反过来如果不在凸包里,就不可能找到。

点在多边形内的判定有多种方法,这里因为已经先求凸包了,所以可以利用点和线段的位置关系,如果点在所有线段的左边或是线段上(这里点在边上也算在多边形内),则说明点在多边形内。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
inline void in(int &x){
x=0;
char c=getchar();
bool mk=0;
while(c<48 || c>57){
if(c=='-')	mk=1;
c=getchar();
}
while(c>=48 && c<=57){
x = x*10+c-48;
c = getchar();
}
if(mk)	x = -x;
}
struct Point{
int x, y;
Point(){}
Point(int x, int y):x(x),y(y){}
bool operator < (const Point &tmp)const{
return x<tmp.x || (x==tmp.x&&y<tmp.y);
}
Point operator - (Point A){
return Point(x-A.x, y-A.y);
}
};
#define pb push_back
int det(Point A, Point B){
return A.x*B.y - A.y*B.x;
}
vector<Point> V;
Point ch[100010];
int m;
char ans[2][10]={"inside","outside"};
void ConvexHull(){
m = 0;
for(int i=0; i<V.size(); i++){
while(m>1 && det(ch[m-1]-ch[m-2], V[i]-ch[m-2]) <= 0)	m--;
ch[m++] = V[i];
}
int k = m;
for(int i=V.size()-2; i>=0; i--){
while(m>k && det(ch[m-1]-ch[m-2], V[i]-ch[m-2]) <= 0)	m--;
ch[m++] = V[i];
}
}
bool find(){
int x, y;
in(x); in(y);
Point P = Point(x,y);
for(int i=0; i<m-1; i++){
if(det(ch[i+1]-ch[i], P-ch[i]) < 0)	return 1;
}
return 0;
}
int main(){
int n, x, y;
while(~scanf("%d", &n)){
V.clear();
for(int i=0; i<n; i++){
in(x); in(y);
V.pb(Point(x,y));
}
sort(V.begin(), V.end());
ConvexHull();
in(n);
while(n--){
puts(ans[find()]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: