POJ1696 space ant(计算几何,极角排序)
来源:互联网 发布:维特根斯坦 知乎 编辑:程序博客网 时间:2024/06/14 23:52
Space Ant
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4656 Accepted: 2925
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:
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.
- 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
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
2101 4 52 9 83 5 94 1 75 3 26 6 37 10 108 8 19 2 410 7 6141 6 112 11 93 8 74 12 85 9 206 3 27 1 68 2 139 15 110 14 1711 13 1912 5 1813 7 314 10 16
Sample Output
10 8 7 3 4 9 5 6 2 1 1014 9 10 11 5 12 8 7 6 13 4 14 1 3 2
题意:一只蚂蚁,只能向左转,问最多能经过哪些给定的点,给出路径.
思路:第一个点肯定是最左下角那个,然后根据极角排序结果,每次选择一个点,然后以这个点为基础进行极角排序,直到 找完所有的点.
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <vector>#include <algorithm>#include <iostream>#include <string>#include <set>#include <map>#include <queue>using namespace std;const int MAXN = 5000+10;const int INF=1e9+7;const double eps=1e-8;const double pi=acos(-1.0);//计算几何误差修正//输入为一个double类型的数,返回-1表示负数,1表示正数,0表示x为0int cmp(double x){ if(fabs(x)<eps) return 0; if(x>0) return 1; return -1;}//计算几何点类inline double sqr(double x){ return x*x;}struct point{ double x,y; int id;/// point(){} point(double a,double b):x(a),y(b){} void input(){ scanf("%lf%lf",&x,&y); } //加法 friend point operator + (const point &a,const point &b){ return point(a.x+b.x,a.y+b.y); } //减法 friend point operator - (const point &a,const point &b){ return point(a.x-b.x,a.y-b.y); } //判断相等 friend bool operator == (const point &a,const point &b){ return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0; } //倍增 friend point operator * (const point &a,const double &b){ return point(a.x*b,a.y*b); } //倍增 friend point operator * (const double &b,const point &a){ return point(a.x*b,a.y*b); } //除法 friend point operator / (const point &a,const double b){ return point(a.x/b,a.y/b); } //模长 double norm(){ return sqrt(sqr(x)+sqr(y)); }};//叉积,a×b>0代表a在b的顺时针方向,<0代表a在b的逆时针方向,等于0代表a和b向量共线,但不确定方向是否相同double det(const point &a,const point &b){ return a.x*b.y-a.y*b.x;}//点积double dot(const point &a,const point &b){ return a.x*b.x+a.y*b.y;}//距离double dist(const point &a,const point &b){ return (a-b).norm();}//op向量绕原点逆时针旋转A(弧度)point rotate_point(const point &p,double A){ double tx=p.x,ty=p.y; return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));}//计算几何线段类struct line{ point a,b; line(){} line(point x,point y):a(x),b(y){} void input(){ a.input(); b.input(); }};//用两个点a,b生成的一个线段或者直线line point_make_line(point a,point b){ return line(a,b);}//求点p到线段st的距离double dis_point_segment(point p,point s,point t){ if(cmp(dot(p-s,t-s))<0) return (p-s).norm(); if(cmp(dot(p-s,s-t))<0) return (p-t).norm(); return fabs(det(s-p,t-p)/dist(s,t));}//求点p到线段st的垂足,保存在cp中void PointProjLine(point p,point s,point t,point &cp){ double r=dot((t-s),(p-s))/dot(t-s,t-s); cp=s+(t-s)*r;}//判断p点是否在线段st上bool PointOnSegment(point p,point s,point t){ return cmp(det(p-s,t-s))==0&&cmp(dot(p-s,p-t))<=0;}//判断a和b是否平行bool parallel(line a,line b){ return !cmp(det(a.a-a.b,b.a-b.b));}//判断a和b是否共线bool contribution(line a,line b){ if(!parallel(a, b)) return false; if(!cmp(det(a.b-a.a,b.a-a.b))) return true; return false;}//判断a和b是否相交,若相交则返回true且交点保存在res中bool line_make_point(line a,line b,point &res){ if(parallel(a, b)) return false; double s1=det(a.a-b.a,b.b-b.a); double s2=det(a.b-b.a,b.b-b.a); res=(s1*a.b-s2*a.a)/(s1-s2); return true;}//判断线段是否相交bool segment_make_point(line a,line b,point &res){ if(PointOnSegment(a.a, b.a, b.b)||PointOnSegment(a.b, b.a, b.b)||PointOnSegment(b.a, a.a, a.b)||PointOnSegment(b.b, a.a, a.b)) return true; if(!line_make_point(a,b,res)) return false; if(PointOnSegment(res, a.a, a.b)&&PointOnSegment(res, b.a, b.b)) return true; return false;}//判断线段和直线是否相交,a是直线,b是线段bool line_across_segment(line a,line b,point &res){ if(cmp(det(a.b-a.a,b.a-a.a)*det(a.b-a.a,b.b-a.a))==1){ return false; } line_make_point(a, b, res); return true;}//将直线a沿法向量方向平移距离len得到的直线line move_d(line a,const double &len){ point d=a.b-a.a; d=d/d.norm(); d=rotate_point(d, pi/2); return line(a.a+d*len,a.b+d*len);}point ps[MAXN];int pos;bool cmp_x(const point &a,const point &b){ int x=cmp(det(a-ps[pos],b-ps[pos])); if(x==0){ return dist(a,ps[pos])<dist(b,ps[pos]); }else{ return x>0; }}int main(){ int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&ps[i].id); ps[i].input(); } for(int i=n-1;i>0;i--){ if(ps[i].y<ps[i-1].y) swap(ps[i],ps[i-1]); } pos=0; for(pos=0;pos<n;pos++){ sort(ps+pos,ps+n,cmp_x); } printf("%d",n); for(int i=0;i<n;i++){ printf(" %d",ps[i].id); } printf("\n"); }}
阅读全文
0 0
- POJ1696---Space Ant(基础计算几何:极角排序)
- POJ1696 space ant(计算几何,极角排序)
- poj1696 Space Ant 计算几何
- poj1696 Space Ant (极角排序,叉积)
- POJ 1696 Space Ant (计算几何 + 极角排序)
- Poj1696计算几何的叉积+极角排序+贪心
- POJ1696-Space Ant
- poj1696 Space Ant
- POJ1696-Space Ant
- Poj1696 Space Ant
- poj1696 Space Ant
- poj1696---计算几何
- 极角排序 POJ1696
- poj 1696 Space Ant(计算几何)
- poj 1696 Space Ant 计算几何
- POJ 1696 Space Ant <计算几何>
- poj1696 Space Ant 点积,叉积,夹角
- POJ1696 Space Ant 【凸包-ToLeft-Test】
- 克服拖延的11种方法
- JavaScript进阶:深入理解事件与事件流
- elasticsearch 5.4安装
- 监控SQL Server正在执行的SQL语句和死锁情况
- Linux命令行修改IP、网关、DNS的方法(转载)
- POJ1696 space ant(计算几何,极角排序)
- Hdu 1080 Human Gene Functions(DP)
- Openstack NUMA 分析 (入门)
- SSM(九) 反射的实际应用
- numpy.zeros((5,))中(5,)的含义 顺便复习Python中tuple的用法
- 欢迎使用CSDN-markdown编辑器
- Mysql绿色版 安装全过程及配置时遇到的问题详解(1)(11-15-17)
- Docker 配置
- matlab使用TCP/IP Server Sockets