计算几何:poj 1228 Grandpa's Estate

阅读: 评论:0

计算几何:poj 1228 Grandpa's Estate

计算几何:poj 1228 Grandpa's Estate

002:Grandpa’s Estate
查看提交统计提问
总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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.
输出
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.
样例输入
1
6
0 0
1 2
3 4
2 0
2 4
5 0
样例输出
NO
来源
Tehran 2002 Preliminery


判断闭包每条边都为三点共线

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define EPS 1e-6
int Sign(int x)
{ // 判断 x 是大于0,等于0还是小于0if(x > 0)return 1;else if(x < 0)return -1;elsereturn 0;
}
struct Point
{int x, y;Point(int xx = 0, int yy = 0) : x(xx), y(yy) {}Point(Point xx, Point yy) : x(xx.x - yy.x), y(xx.y - yy.y) {}Point operator-(const Point &p) const{return Point(x - p.x, y - p.y);}bool operator<(const Point &p) const{if (y < p.y)return true;else if (y > p.y)return false;elsereturn x < p.x;}bool operator==(const Point &p) const{return x == p.x && y == p.y;}
};
typedef Point Vector;
int Cross(const Vector &v1, const Vector &v2)
{ //叉积return v1.x * v2.y - v2.x * v1.y;
}
int Distance(const Point &p1, const Point &p2)
{return sqrt((double)((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)));
}
struct Comp
{             //用来定义极角排序规则的函数对象Point p0; //以p0为原点进行极角排序,极角相同的,离p0近算小Comp(const Point &p) : p0(p.x, p.y) {}bool operator()(const Point &p1, const Point &p2) const{int s = Sign(Cross(p1 - p0, p2 - p0));if (s > 0)return true;else if (s < 0)return false;else{if (Distance(p0, p1) < Distance(p0, p2))return true;elsereturn false;}}
};
vector<Point> points;
vector<Point> stack;
int operator^(Vector p, Vector q)
{return p.x * q.y - q.x * p.y;
}
int Graham()
{//points是点集合//cout << "whi" << endl;if (points.size() < 3)return 0; //返回凸包顶点数stack.clear();//先按坐标排序,最左下的放到points[0]sort(points.begin(), d());//以points[0] 为原点进行极角排序sort(points.begin() + 1, d(), Comp(points[0]));stack.push_back(points[0]);stack.push_back(points[1]);stack.push_back(points[2]);for (int i = 3; i < points.size(); ++i){while (stack.size() >= 3){Point p2 = *(d() - 1);Point p1 = *(d() - 2);if (Sign(Cross(p2 - p1, points[i] - p2) <= 0)) //p2->points[i]没有向左转,就让p2出栈stack.pop_back();elsebreak;}stack.push_back(points[i]);// cout << "?" << endl;}int ss = stack.size();/* for (int i = 0; i < ss; i++){cout << stack[i].x << ' ' << stack[i].y << endl;}*/if(ss < 3)return 0;for (int i = 0; i < ss; i++){Vector m(stack[i], stack[(i + 1) % ss]);int flag = 0;for (int j = 0; j < points.size(); j++){if (points[j] == stack[i] || points[j] == stack[(i + 1) % ss]){continue;}else{Vector mm(points[j], stack[i]);if (!(mm ^ m))flag = 1;}}if (flag == 0)return 0;}return stack.size();
}int main()
{int sbss;int wfy, cyz;int wd;cin >> sbss;while (sbss--){cin >> wd;int m = wd;while (m--){cin >> wfy >> cyz;Point lha(wfy, cyz);points.push_back(lha);}if(wd <= 5){cout << "NO" << endl;points.clear();continue;}int cbyl = Graham();if (cbyl == 0)cout << "NO" << endl;elsecout << "YES" << endl;points.clear();}return 0;
}

本文发布于:2024-02-03 00:27:39,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170689125847456.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:几何   poj   Grandpa   Estate
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23