计算几何:poj 1228 Grandpa's Estate

2023-12-13 00:10

本文主要是介绍计算几何: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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/486436

相关文章

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D