二维坐标系极角排序的应用(POJ1696)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3170 | Accepted: 2029 |
Description
of its body and suffers from three walking limitations:
- It can not turn right due to its special body structure.
- It leaves a red path while walking.
- It hates to pass over a previously red colored path, and never does that.
The pictures transmitted by the Discovery space ship depicts that plants in the Y1999 grow in special points on the planet. Analysis of several thousands of the pictures have resulted in discovering a magic coordinate system governing the grow points of the
plants. In this coordinate system with x and y axes, no two plants share the same x or y.
An M11 needs to eat exactly one plant in each day to stay alive. When it eats one plant, it remains there for the rest of the day with no move. Next day, it looks for another plant to go there and eat it. If it can not reach any other plant it dies by the end
of the day. Notice that it can reach a plant in any distance.
The problem is to find a path for an M11 to let it live longest.
Input is a set of (x, y) coordinates of plants. Suppose A with the coordinates (xA, yA) is the plant with the least y-coordinate. M11 starts from point (0,yA) heading towards plant A. Notice that the solution path should not cross itself and all of the turns
should be counter-clockwise. Also note that the solution may visit more than two plants located on a same straight line.
Input
integers: the first number is the unique plant index (1..N), followed by two positive integers x and y representing the coordinates of the plant. Plants are sorted by the increasing order on their indices in the input file. Suppose that the values of coordinates
are at most 100.
Output
Sample Input
2
10
1 4 5
2 9 8
3 5 9
4 1 7
5 3 2
6 6 3
7 10 10
8 8 1
9 2 4
10 7 6
14
1 6 11
2 11 9
3 8 7
4 12 8
5 9 20
6 3 2
7 1 6
8 2 13
9 15 1
10 14 17
11 13 19
12 5 18
13 7 3
14 10 16
Sample Output
10 8 7 3 4 9 5 6 2 1 10
14 9 10 11 5 12 8 7 6 13 4 14 1 3 2
题意:在二维平面坐标系中给出n个不同的点,然后一个人可以从任何点为起点,然后走下个点,保证不会向右转弯,如果连个点在一条直线上,可以一直走,问最多可以经过多少点,并输出路径?
分析:其实给出的n个点都是可以走的,首先找到y坐标最小的点,即一定是凸包上的点,然后极角排序,走排序后的第2个点,然后删除第一个点,在以刚才走的点为标志,进行极角排序,取第2个点,以此类推;
#include"string.h"
#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#define M 1000
#define N 100009
#include"stdlib.h"
#include"math.h"
#define inf 10000000000000000LL
#define INF 0x3f3f3f3f
const double PI=acos(-1.0);
#define eps 1e-10
using namespace std;
struct node
{
double x,y;
int id;
node (){}
node (double xx,double yy):x(xx),y(yy){}
node operator -(node p)
{
return node (x-p.x,y-p.y);
}
double operator *(node p)
{
return x*p.y-y*p.x;
}
double operator ^(node p)
{
return x*p.x+y*p.y;
}
}p[M];
struct line
{
node s,e;
int id;
line(){}
line(double x1,double y1,double x2,double y2)
{
s.x=x1;
s.y=y1;
e.x=x2;
e.y=y2;
}
line(node a,node b)
{
s=a;
e=b;
}
}l[M],L[M];
double max(double a,double b)
{
return a>b?a:b;
}
double min(double a,double b)
{
return a<b?a:b;
}
double cross(node a,node b,node c)
{
return (b-a)*(c-a);
}
double dot(node a,node b,node c)
{
return (b-a)^(c-a);
}
double len(node a)
{
return sqrt(a^a);
}
double dis(node a,node b)
{
return len(b-a);
}
int judge(line a,line b)
{
if(fabs(cross(a.s,a.e,b.s))<eps&&fabs(cross(a.s,a.e,b.e))<eps)//重合
return 1;
else if(fabs((a.e-a.s)*(b.e-b.s))<eps)//平行
return -1;
else
return 0;//相交
}
node intersection(line a,line b)
{
double a1=a.s.y-a.e.y;
double b1=a.e.x-a.s.x;
double c1=(a.e.y-a.s.y)*a.s.x+a.s.y*(a.s.x-a.e.x);
double a2=b.s.y-b.e.y;
double b2=b.e.x-b.s.x;
double c2=(b.e.y-b.s.y)*b.s.x+b.s.y*(b.s.x-b.e.x);
node ret;
ret.x=(c2*b1-c1*b2)/(a1*b2-a2*b1);
ret.y=(c2*a1-c1*a2)/(a2*b1-a1*b2);
return ret;
}
int paichi(line a,line b)//快速排斥,若通过快速排斥进行跨立实验,否则无交点;
{
if(max(a.e.x,a.s.x)>=min(b.s.x,b.e.x)
&&max(b.s.x,b.e.x)>=min(a.s.x,a.e.x)
&&max(a.s.y,a.e.y)>=min(b.s.y,b.e.y)
&&max(b.s.y,b.e.y)>=min(a.s.y,a.e.y))
return 1;
else
return 0;
}
int kuali(line a,line b)//跨立实验(通过相互跨立则可确定两线段相交返回1)
{
if(cross(a.s,a.e,b.s)*cross(a.s,a.e,b.e)<eps
&&cross(b.s,b.e,a.s)*cross(b.s,b.e,a.e)<eps)
return 1;
return 0;
}
int cmp(node a,node b)//极角排序
{
double temp=cross(p[0],a,b);//逆时针排序
if(temp>0)
return 1;
else if(fabs(temp)<eps&&dis(p[0],a)<dis(p[0],b))//角度相同则按照距离排序
return 1;
else
return 0;
}
int main()
{
int n,i,a;
int T;
cin>>T;
while(T--)
{
scanf("%d",&n);
int nn=n;
node start(INF,INF);
int id;
for(i=0;i<n;i++)
{
scanf("%d%lf%lf",&a,&p[i].x,&p[i].y);
p[i].id=a;
if(start.y>p[i].y||(fabs(start.x-p[i].x)<eps&&start.x>p[i].x))
{
start=p[i];
id=i;
}
}
p[id]=p[0];
p[0]=start;
queue<int>q;
q.push(start.id);
while(n>1)
{
sort(p+1,p+n,cmp);
q.push(p[1].id);
//printf("%d\n",p[1].id);
swap(p[0],p[n-1]);
n--;
swap(p[0],p[1]);
}
printf("%d",nn);
while(!q.empty())
{
printf(" %d",q.front());
q.pop();
}
printf("\n");
}
}
二维坐标系极角排序的应用(POJ1696)的更多相关文章
- 代码分享:php对二维数组进行排序
发布:net/PHP编程 编辑:thebaby 2013-06-28 13:12:54 [大 中 小] 转自:http://www.jbxue.com/article/9991.html本文介 ...
- php对二维数据进行排序
PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义. 方法一:(经验证,成功) 作用:对二维数组进行指定key排序 参数:$arr ...
- php 二维数组自定义排序
eg1:只根据一个规则进行排序,比如我下面的数组是一组满减折扣的信息,我要按照满减的金额从小到大排序 代码: <?php $arr =[ ["amount"=> 60, ...
- Java 二维数组,排序、切换顺序,查表法二进制十进制,这班查找、排序(冒泡、选择)、遍历,获取最大小值(4)
Java 二维数组,排序.切换顺序,查表法二进制十进制,折半查找.排序(冒泡.选择).遍历,获取最大小值(4)
- Codeforces Round #524 (Div. 2)C 二维坐标系求俩矩形面积交
题:https://codeforces.com/contest/1080/problem/C 题意:给n*m的二维坐标系,每个位置(xi,yi)都表示一个方格,(1,1)的位置是白色,整个坐标系黑白 ...
- php 二维数组的排序
写这是之前一直二维数组排名困扰.自己写的好复杂. 正题: array_mutisort 官方帮助文档 <?php// 取得列的列表foreach ($data as $key => $ro ...
- Java数组排序基础算法,二维数组,排序时间计算,随机数产生
import java.util.Arrays; //包含Arrays import java.util.Random; public class HelloWorld { public static ...
- 如何使用python来对二维数组进行排序
1.复合排序 直接用numpy的lexsort就可以 import numpy as np data = np.array([[1,2,3,4,5], [1,2,3,6,7], [2,3,4,5,7] ...
- 二维数组sort排序
和副本任务完全无关的奇怪感慨: 完全搞不懂我为什么会在搞图论的时候学这种奇怪东西,需要的时候不会,不需要的时候又莫名增加了奇怪的技能点. 之前的假期规划在十多天的放飞自我中彻底泡汤,简单的图论都一点不 ...
随机推荐
- js元素绑定事件
想给一个元素绑定一个方法之后,在绑定一个方法而且不被覆盖 window.onload = function () { alert('a'); } window.onlaod=function(){ a ...
- Spark SQL怎么创建编程创建DataFrame
创建DataFrame在Spark SQL中,开发者可以非常便捷地将各种内.外部的单机.分布式数据转换为DataFrame.以下Python示例代码充分体现了Spark SQL 1.3.0中DataF ...
- linux profile\bashrc\bash_profile之间的区别和联系
/etc/profile 每个用户,首次登录时被执行: /etc/bashrc 每个运行bash shell的用户都执行此文件,当bsh被打开时,该文件被读取: ~/.bash_profile 专用于 ...
- 第二百九十八节,python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表
python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表 sadd(name,values)name对应的集合中添加元素 #!/usr/bin/env python # -*- ...
- 剑指offer_面试题5_从尾到头打印链表(栈和递归实现)
题目:输入一个链表的头结点,从尾到头反过来打印出每一个节点的值 考察 单链表操作.栈.递归等概念. 理解:要实现单链表的输出,那么就须要遍历.遍历的顺序是从头到尾.而节点输出的顺序是从尾到头.因此,先 ...
- erlang 自定义函数的初步应用
一.模块内调用 1> AA=fun(BB)-> io:format("this is test args ~s~n",[BB]) end.#Fun<erl_eva ...
- vncserver的安装和使用
环境:RedHat Linux 6企业版.Xwindows:gnome (红帽默认安装的图形界面) 尽管我们可以使用SSH连接远程通过字符界面来操作Linux,但是对于更多熟悉图形人来说是很不方便的, ...
- Json.net 时间格式处理
json.net转json时生成的时间格式是这种 2015-11-14T06:59:59+08:00 格式化为这种2015-11-14 后台代码: IsoDateTimeConverter timeF ...
- Spring-profile设置
开发环境和生产环境通常采用不同的数据库连接方式,开发环境可以采用侵入式,而生产环境中采用jndi连接池,所以要根据不同环境配置不同的bean,Spring中提供了profile来实现动态生成相应的be ...
- 演示-JQuery关系选择器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...