POJ 2007 Scrambled Polygon 凸包
Scrambled Polygon
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7214 Accepted: 3445 Description
A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments are called the vertices of the polygon. When one starts at any vertex of a closed polygon and traverses each bounding line segment exactly once, one comes back to the starting vertex.A closed polygon is called convex if the line segment joining any two points of the polygon lies in the polygon. Figure 1 shows a closed polygon which is convex and one which is not convex. (Informally, a closed polygon is convex if its border doesn't have any "dents".)
The subject of this problem is a closed convex polygon in the coordinate plane, one of whose vertices is the origin (x = 0, y = 0). Figure 2 shows an example. Such a polygon will have two properties significant for this problem.The first property is that the vertices of the polygon will be confined to three or fewer of the four quadrants of the coordinate plane. In the example shown in Figure 2, none of the vertices are in the second quadrant (where x < 0, y > 0).
To describe the second property, suppose you "take a trip" around the polygon: start at (0, 0), visit all other vertices exactly once, and arrive at (0, 0). As you visit each vertex (other than (0, 0)), draw the diagonal that connects the current vertex with (0, 0), and calculate the slope of this diagonal. Then, within each quadrant, the slopes of these diagonals will form a decreasing or increasing sequence of numbers, i.e., they will be sorted. Figure 3 illustrates this point.
![]()
Input
The input lists the vertices of a closed convex polygon in the plane. The number of lines in the input will be at least three but no more than 50. Each line contains the x and y coordinates of one vertex. Each x and y coordinate is an integer in the range -999..999. The vertex on the first line of the input file will be the origin, i.e., x = 0 and y = 0. Otherwise, the vertices may be in a scrambled order. Except for the origin, no vertex will be on the x-axis or the y-axis. No three vertices are colinear.Output
The output lists the vertices of the given polygon, one vertex per line. Each vertex from the input appears exactly once in the output. The origin (0,0) is the vertex on the first line of the output. The order of vertices in the output will determine a trip taken along the polygon's border, in the counterclockwise direction. The output format for each vertex is (x,y) as shown below.Sample Input
0 0
70 -50
60 30
-30 -50
80 20
50 -60
90 -20
-30 -40
-10 -60
90 10Sample Output
(0,0)
(-30,-40)
(-30,-50)
(-10,-60)
(50,-60)
(70,-50)
(90,-20)
(90,10)
(80,20)
(60,30) 题目叙述很长,其实就是给出一组包括原点在内的点,求出这组点的凸包的各个定点,按照逆时针方向从原点开始输出整个凸包的顶点
两种方法可以做:一个是Graham-Scan,还有就是直接极坐标排序,选取原点为基准点来排
代码如下
/*极坐标排序方法*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#define EPS 1e-8 using namespace std;
struct point{
double x, y;
};
const int maxn = ;
point p[maxn], pp;//pp是基准点
int n;
int sgn(double x)
{
if (fabs(x) < EPS)
return ;
return x < ? - : ;
}
double get_direction(point p1, point p2, point p3)
{
return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
}
double get_distance(point p1, point p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
bool cmp(const point p1, const point p2)//极坐标排序的比较函数
{
if (sgn(get_direction(pp, p1, p2)) < )
return true;
if (sgn(get_direction(pp, p1, p2)) == && get_distance(pp, p1) < get_distance(pp, p2))
return true;
return false;
}
int main()
{
n = ;
while (~scanf("%lf %lf", &p[n].x, &p[n].y))
n++;
int i;
for (i = ; i < n; i++)
{
if (p[i].x == && p[i].y == )
break;
}
pp = p[i];
p[i] = p[];
p[] = pp; sort(p, p + n, cmp);
for (int i = ; i < n; i++)
printf("(%.0f,%.0f)\n", p[i].x, p[i].y); return ;
}
普通的Graham
/*************************************************************************
> File Name: poj_2007.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年04月16日 星期四 14时47分43秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#define EPS 1e-8
using namespace std;
struct point{
double x, y;
};
const int maxn = ;
point p[maxn];
int n, top, convex[maxn];
int sgn(double x)
{
if (fabs(x) < EPS)
return ;
return x < ? - : ;
}
bool cmp(const point p1, const point p2)
{
return ((p1.y == p2.y && p1.x < p2.x) || p1.y < p2.y);
}
double get_direction(point p1, point p2, point p3)
{
return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
}
void Graham()
{
top = ;
for (int i = ; i < n; i++)
{
while (top > && sgn(get_direction(p[convex[top - ]], p[convex[top - ]], p[i])) >= )
top--;
convex[top++] = i;
}
int tmp = top;
for (int i = n - ; i >= ; i--)
{
while (top > tmp && sgn(get_direction(p[convex[top - ]], p[convex[top - ]], p[i])) >= )
top--;
convex[top++] = i;
}
}
int main()
{
n = ;
while (~scanf("%lf %lf", &p[n].x, &p[n].y)) n++;
sort(p, p + n, cmp);
Graham();
int k;
for (k = ; k < top; k++)
if (p[convex[k]].x == && p[convex[k]].y == )
break;
for (int i = k; i < top - ; i++)
printf("(%.0f,%.0f)\n", p[convex[i]].x, p[convex[i]].y);
for (int i = ; i < k; i++)
printf("(%.0f,%.0f)\n", p[convex[i]].x, p[convex[i]].y);
return ;
}
POJ 2007 Scrambled Polygon 凸包的更多相关文章
- POJ 2007 Scrambled Polygon [凸包 极角排序]
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8636 Accepted: 4105 ...
- POJ 2007 Scrambled Polygon 凸包点排序逆时针输出
题意:如题 用Graham,直接就能得到逆时针的凸包,找到原点输出就行了,赤果果的水题- 代码: /* * Author: illuz <iilluzen[at]gmail.com> * ...
- POJ 2007 Scrambled Polygon 极角序 水
LINK 题意:给出一个简单多边形,按极角序输出其坐标. 思路:水题.对任意两点求叉积正负判断相对位置,为0则按长度排序 /** @Date : 2017-07-13 16:46:17 * @File ...
- poj 2007 Scrambled Polygon(极角排序)
http://poj.org/problem?id=2007 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6701 A ...
- ●POJ 2007 Scrambled Polygon
题链: http://poj.org/problem?id=2007 题解: 计算几何,极角排序 按样例来说,应该就是要把凸包上的i点按 第三像限-第四像限-第一像限-第二像限 的顺序输出. 按 叉积 ...
- 简单几何(极角排序) POJ 2007 Scrambled Polygon
题目传送门 题意:裸的对原点的极角排序,凸包貌似不行. /************************************************ * Author :Running_Time ...
- POJ 2007 Scrambled Polygon(简单极角排序)
水题,根本不用凸包,就是一简单的极角排序. 叉乘<0,逆时针. #include <iostream> #include <cstdio> #include <cs ...
- POJ 2007 Scrambled Polygon (简单极角排序)
题目链接 题意 : 对输入的点极角排序 思路 : 极角排序方法 #include <iostream> #include <cmath> #include <stdio. ...
- poj 2007 Scrambled Polygon 极角排序
/** 极角排序输出,,, 主要atan2(y,x) 容易失精度,,用 bool cmp(point a,point b){ 5 if(cross(a-tmp,b-tmp)>0) 6 retur ...
随机推荐
- Python--模块微谈
一个.py文件是一个模块,模块里面可以定义很多函数,载入模块就可以使用函数.模块又有python内置模块,又有第三方模块,不同模块里可以有相同的函数名和变量名,模块又可以组织到package里,不同的 ...
- 简述MVC思想 与PHP如何实现MVC
我相信已经有很多这样的文章了,但是我今天还是愿意把自己的经验与大家分享一下.纯属原创,我也没什么保留,希望对新手有帮助,有说的不对的地方,也欢迎指出. 什么是MVC? 简单的说就是将网站源码分类.分层 ...
- 创建mysql数据库
mysql> create database wzhpush3 default charset utf8 collate utf8_general_ci;
- 关于 Boolean 的转换
前端经常喜欢这样写 if else if(value) { //do something } javascript 能智能的把任何类型的 value 转换成 boolean 来进行 if 判断 转换是 ...
- The Child and Toy
Codeforces Round #250 (Div. 2) C:http://codeforces.com/problemset/problem/437/C 题意:给以一个无向图,每个点都有一点的权 ...
- varnish、squid、apache、nginx缓存的对比<转>
1.Squid,很古老的反向代理软件,拥有传统代理.身份验证.流量管理等高级功能,但是配置太复杂.它算是目前互联网应用得最多的反向缓存代理服务器,工作于各大古老的cdn上. 2.Varnish是新兴的 ...
- 深入浅出 Java Concurrency (2): 原子操作 part 1
转:http://www.blogjava.net/xylz/archive/2010/07/01/324988.html 从相对简单的Atomic入手(java.util.concurrent是基于 ...
- 修改css
.content{ height: 100%; } .con{ border: 1px solid #eeeeee; display: inline-block; width:86.8%; ##修改这 ...
- bzoj1135
POI阴影又发作了但这道题挺好的,比较涨知识裸的想法是裸的每次二分图匹配,但显然会TLE这里就要引入Hall定理:二分图G中的两部分顶点组成的集合分别为X, Y, X={X1, X2, X3,X4,. ...
- 搜索(四分树):BZOJ 4513 [SDOI2016 Round1] 储能表
4513: [Sdoi2016]储能表 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 395 Solved: 213[Submit][Status] ...

