Space Ant
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3170   Accepted: 2029

Description

The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y1999 and called it M11. It has only one eye on the left side of its head and just three feet all on the right side
of its body and suffers from three walking limitations:

  1. It can not turn right due to its special body structure.
  2. It leaves a red path while walking.
  3. 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

The first line of the input is M, the number of test cases to be solved (1 <= M <= 10). For each test case, the first line is N, the number of plants in that test case (1 <= N <= 50), followed by N lines for each plant data. Each plant data consists of three
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

Output should have one separate line for the solution of each test case. A solution is the number of plants on the solution path, followed by the indices of visiting plants in the path in the order of their visits.

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)的更多相关文章

  1. 代码分享:php对二维数组进行排序

    发布:net/PHP编程  编辑:thebaby   2013-06-28 13:12:54  [大 中 小] 转自:http://www.jbxue.com/article/9991.html本文介 ...

  2. php对二维数据进行排序

    PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义. 方法一:(经验证,成功)   作用:对二维数组进行指定key排序 参数:$arr ...

  3. php 二维数组自定义排序

    eg1:只根据一个规则进行排序,比如我下面的数组是一组满减折扣的信息,我要按照满减的金额从小到大排序 代码: <?php $arr =[ ["amount"=> 60, ...

  4. Java 二维数组,排序、切换顺序,查表法二进制十进制,这班查找、排序(冒泡、选择)、遍历,获取最大小值(4)

    Java 二维数组,排序.切换顺序,查表法二进制十进制,折半查找.排序(冒泡.选择).遍历,获取最大小值(4)

  5. Codeforces Round #524 (Div. 2)C 二维坐标系求俩矩形面积交

    题:https://codeforces.com/contest/1080/problem/C 题意:给n*m的二维坐标系,每个位置(xi,yi)都表示一个方格,(1,1)的位置是白色,整个坐标系黑白 ...

  6. php 二维数组的排序

    写这是之前一直二维数组排名困扰.自己写的好复杂. 正题: array_mutisort 官方帮助文档 <?php// 取得列的列表foreach ($data as $key => $ro ...

  7. Java数组排序基础算法,二维数组,排序时间计算,随机数产生

    import java.util.Arrays; //包含Arrays import java.util.Random; public class HelloWorld { public static ...

  8. 如何使用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] ...

  9. 二维数组sort排序

    和副本任务完全无关的奇怪感慨: 完全搞不懂我为什么会在搞图论的时候学这种奇怪东西,需要的时候不会,不需要的时候又莫名增加了奇怪的技能点. 之前的假期规划在十多天的放飞自我中彻底泡汤,简单的图论都一点不 ...

随机推荐

  1. Ubuntu-Python2.7安装 scipy,numpy,matplotlib 和pip

    一. scipy,numpy,matplotlib sudo apt-get install python-scipy sudo apt-get install python-numpy sudo a ...

  2. Linux如何通过命令查看日志文件的某几行(中间几行或最后几行)

    linux 如何显示一个文件的某几行(中间几行) [一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1 ...

  3. selenium测试(Java)-- 验证信息(八)

    package com.test.validationinfor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.f ...

  4. spring FactoryBean配置Bean

    概要: 实例代码具体解释: 文件夹结构 Car.java package com.coslay.beans.factorybean; public class Car { private String ...

  5. 多个进程对同一个监听套接字调用函数gen_tcp:accept/1

    源于<<erlang程序设计>>的第14章的14.1.4大约第197页. 未发现多个进程对同一个监听套接字调用函数gen_tcp:accept/1比单进程的效率更高或者更快.

  6. 10个开发中常用的PHP代码样例

    一.黑名单过滤 function is_spam($text, $file, $split = ':', $regex = false){ $handle = fopen($file, 'rb'); ...

  7. iOS 图文混排 (Swift版)

    // 0> 图片附件 let attachment = NSTextAttachment() attachment.image = #imageLiteral(resourceName: &qu ...

  8. poj 3414 Pots(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...

  9. HttpClient传递Cookie

    使用代码访问http资源,我们通常用WebRequest,当然,HttpClient提供了更方便的封装,我用得更多.只是碰到一些需要(cookie)鉴权的情况,需要把cookie伴随请求一起发到服务器 ...

  10. 【python】\\u的字符编码问题

    Str = "\\u559c\\u6b22\\u4e00\\u4e2a\\u4eba";Str = Str.decode("unicode-escape")