计算几何: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

相关文章

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

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一