本文主要是介绍计算几何: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(), points.end());//以points[0] 为原点进行极角排序sort(points.begin() + 1, points.end(), 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 = *(stack.end() - 1);Point p1 = *(stack.end() - 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;
}
这篇关于计算几何:poj 1228 Grandpa's Estate的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!