POJ 1228 凸包,判断点在线段上
2014-04-21 13:03
405 查看
Grandpa's Estate
Description
Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one was a piece of convex polygon shaped farm in the grandpa's birth village. The farm was originally
separated from the neighboring farms by a thick rope hooked to some spikes (big nails) placed on the boundary of the polygon. But, when Kamran went to visit his farm, he noticed that the rope and some spikes are missing. Your task is to write a program to
help Kamran decide whether the boundary of his farm can be exactly determined only by the remaining spikes.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1 <= n <= 1000) which
is the number of remaining spikes. Next, there are n lines, one line per spike, each containing a pair of integers which are x and y coordinates of the spike.
Output
There should be one output line per test case containing YES or NO depending on whether the boundary of the farm can be uniquely determined from the input.
Sample Input
Sample Output
问给定点能否确定一个多边形,先求一次凸包,只要保证凸包上的每一条边上至少有给定的点中的三个点,就可以保证多边形确定。
代码:
/* ***********************************************
Author :rabbit
Created Time :2014/4/20 9:39:31
File Name :8.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define pi acos(-1.0)
typedef long long ll;
int dcmp(double x){
if(fabs(x)<eps)return 0;
return x>0?1:-1;
}
struct Point{
double x,y;
Point(double _x=0,double _y=0){
x=_x;y=_y;
}
};
Point operator + (Point a,Point b){
return Point(a.x+b.x,a.y+b.y);
}
Point operator - (Point a,Point b){
return Point(a.x-b.x,a.y-b.y);
}
Point operator * (Point a,double p){
return Point(a.x*p,a.y*p);
}
Point operator / (Point a,double p){
return Point(a.x/p,a.y/p);
}
bool operator < (const Point &a,const Point &b){
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
bool operator == (const Point &a,const Point &b){
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(Point a,Point b){
return a.x*b.x+a.y*b.y;
}
double Length(Point a){
return sqrt(Dot(a,a));
}
double Angle(Point a,Point b){
return acos(Dot(a,b)/Length(a)/Length(b));
}
double angle(Point a){
return atan2(a.y,a.x);
}
double Cross(Point a,Point b){
return a.x*b.y-a.y*b.x;
}
Point vecunit(Point x){
return x/Length(x);
}
Point Normal(Point x){
return Point(-x.y,x.x);
}
Point Rotate(Point a,double rad){
return Point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
struct Line{
Point p,v;
double ang;
Line(){}
Line(Point P,Point v):p(P),v(v){
ang=atan2(v.y,v.x);
}
bool operator < (const Line &L) const {
return ang<L.ang;
}
Point point(double a){
return p+(v*a);
}
};
Point p[1000],ch[1001];
int ConvexHull(Point *p,int n,Point *ch){
sort(p,p+n);
int m=0;
for(int i=0;i<n;i++){
while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--){
while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
ch[m++]=p[i];
}
if(n>1)m--;
return m;
}
bool OnSegment(Point p,Point a1,Point a2){
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<=0;
}
int main(){
int T,n;
cin>>T;
while(T--){
cin>>n;
for(int i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
int m=ConvexHull(p,n,ch);
if(m<3)puts("NO");
else{
int flag=1;
for(int i=0;i<m;i++){
int cnt=0;
for(int j=0;j<n;j++)
if(OnSegment(p[j],ch[i],ch[(i+1)%m]))cnt++;
if(cnt<3){
flag=0;break;
}
}
if(flag)puts("YES");
else puts("NO");
}
}
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10511 | Accepted: 2838 |
Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one was a piece of convex polygon shaped farm in the grandpa's birth village. The farm was originally
separated from the neighboring farms by a thick rope hooked to some spikes (big nails) placed on the boundary of the polygon. But, when Kamran went to visit his farm, he noticed that the rope and some spikes are missing. Your task is to write a program to
help Kamran decide whether the boundary of his farm can be exactly determined only by the remaining spikes.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1 <= n <= 1000) which
is the number of remaining spikes. Next, there are n lines, one line per spike, each containing a pair of integers which are x and y coordinates of the spike.
Output
There should be one output line per test case containing YES or NO depending on whether the boundary of the farm can be uniquely determined from the input.
Sample Input
1 6 0 0 1 2 3 4 2 0 2 4 5 0
Sample Output
NO
问给定点能否确定一个多边形,先求一次凸包,只要保证凸包上的每一条边上至少有给定的点中的三个点,就可以保证多边形确定。
代码:
/* ***********************************************
Author :rabbit
Created Time :2014/4/20 9:39:31
File Name :8.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define pi acos(-1.0)
typedef long long ll;
int dcmp(double x){
if(fabs(x)<eps)return 0;
return x>0?1:-1;
}
struct Point{
double x,y;
Point(double _x=0,double _y=0){
x=_x;y=_y;
}
};
Point operator + (Point a,Point b){
return Point(a.x+b.x,a.y+b.y);
}
Point operator - (Point a,Point b){
return Point(a.x-b.x,a.y-b.y);
}
Point operator * (Point a,double p){
return Point(a.x*p,a.y*p);
}
Point operator / (Point a,double p){
return Point(a.x/p,a.y/p);
}
bool operator < (const Point &a,const Point &b){
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
bool operator == (const Point &a,const Point &b){
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(Point a,Point b){
return a.x*b.x+a.y*b.y;
}
double Length(Point a){
return sqrt(Dot(a,a));
}
double Angle(Point a,Point b){
return acos(Dot(a,b)/Length(a)/Length(b));
}
double angle(Point a){
return atan2(a.y,a.x);
}
double Cross(Point a,Point b){
return a.x*b.y-a.y*b.x;
}
Point vecunit(Point x){
return x/Length(x);
}
Point Normal(Point x){
return Point(-x.y,x.x);
}
Point Rotate(Point a,double rad){
return Point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
struct Line{
Point p,v;
double ang;
Line(){}
Line(Point P,Point v):p(P),v(v){
ang=atan2(v.y,v.x);
}
bool operator < (const Line &L) const {
return ang<L.ang;
}
Point point(double a){
return p+(v*a);
}
};
Point p[1000],ch[1001];
int ConvexHull(Point *p,int n,Point *ch){
sort(p,p+n);
int m=0;
for(int i=0;i<n;i++){
while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--){
while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
ch[m++]=p[i];
}
if(n>1)m--;
return m;
}
bool OnSegment(Point p,Point a1,Point a2){
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<=0;
}
int main(){
int T,n;
cin>>T;
while(T--){
cin>>n;
for(int i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
int m=ConvexHull(p,n,ch);
if(m<3)puts("NO");
else{
int flag=1;
for(int i=0;i<m;i++){
int cnt=0;
for(int j=0;j<n;j++)
if(OnSegment(p[j],ch[i],ch[(i+1)%m]))cnt++;
if(cnt<3){
flag=0;break;
}
}
if(flag)puts("YES");
else puts("NO");
}
}
}
相关文章推荐
- POJ 1228 Grandpa's Estate 计算凸包+判断点在线段上
- POJ 1228 Grandpa's Estate 判断原始凸包是否唯一
- POJ 1228 Grandpa's Estate【稳定凸包判断】
- poj-1228 Grandpa's Estate(判断凸包是否唯一)
- POJ 1228 Grandpa's Estate(判断是否稳定凸包)
- POJ 1228 Grandpa's Estate(凸包唯一性判断)
- poj 1584 计算几何 (点到线段距离+判断点是否在多边形内+判断多边形是否为凸包)
- POJ 1228 凸包惟一性判断(多边形模板)
- POJ 1228 判断原始凸包是否唯一
- POJ 1228 Grandpa's Estate(给定凸包上的一部分顶点 判断表示的凸包是否唯一)
- POJ 1066 Treasure Hunt(线段相交判断)
- POJ 2653 暴力判断线段相交
- POJ 1228 Grandpa's Estate | 凸包
- POJ-1556 线段相交的判断与最短路(几何+图论)
- 叉积、线段相交判断、凸包
- Poj 1228 Grandpa's Estate (凸包及唯一性判定)
- 【POJ 2653】Pick-up sticks 判断线段相交
- poj 3304 判断是否有与所有线段相交的直线
- poj 1228 凸包
- POJ 1228:Grandpa's Estate (稳定凸包)