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. 关于PHP的特点

    魔术方法 当一个对象引用变量调用一个没有定义的属性或方法时,可以这一个函数.当发生这种情况时调用这种函数.

  2. MODEL-View-Controller,既模型-视图-控制器

    Swing组件采用MVC(MODEL-View-Controller,既模型-视图-控制器)设计模式,其中模型(Model)用于维护组件的各种状态,视图(View)是组件的可视化表现,控制器(Cont ...

  3. Asakura的魔法世界

    Font Size:Aa Aa Aa Description Asakura存在于一个魔法世界中.有一天,Asakura在一条魔法通道里偷懒,突然接到一个紧急任务,要高速赶往还有一条通道b去. 我们把 ...

  4. 剑指offer_面试题5_从尾到头打印链表(栈和递归实现)

    题目:输入一个链表的头结点,从尾到头反过来打印出每一个节点的值 考察 单链表操作.栈.递归等概念. 理解:要实现单链表的输出,那么就须要遍历.遍历的顺序是从头到尾.而节点输出的顺序是从尾到头.因此,先 ...

  5. apache Storm之一-入门学习

    准备工作 这个教程使用storm-starter项目里面的例子.我推荐你们下载这个项目的代码并且跟着教程一起做.先读一下:配置storm开发环境和新建一个strom项目这两篇文章把你的机器设置好. 一 ...

  6. window用Xmanager4.0的Xstart连接linux远程桌面

    安装包: xorg-x11-xauth xterm.x86_64 0:253-1.el6 Execute command path:/usr/bin/xterm Xstart连接Linux远程桌面有一 ...

  7. select2 选择框插件

    <select id="selBusi_type"><select> //初始化业务类型下拉 var initBusiTypeSel = function( ...

  8. OpenCV学习:体验ImageWatch

    Image Watch是在VS2012及以上版本上使用的一款OpenCV插件工具,能够实时显示图像和矩阵Mat的内容,跟Matlab很像,方便程序调试,相当好用. 1)安装Visual Studio ...

  9. VC++ 打开文件或文件夹对话框的实现方法

    实际工作开发中,由于各种应用,我们需要调用系统的打开文件对话框或者打开文件夹对话框,或两者兼有.特总结了常用的实现方法,仅供开发参考. 1. 打开文件对话框 常用的方法是使用系统的CFileDialo ...

  10. SQLServer------存储过程的使用

    转载: http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html 例子: 1.学生表 CREATE TABLE [dbo].[Stude ...