设为首页 加入收藏

TOP

POJ 1696 Space Ant 计算几何 叉积的应用
2015-11-21 01:23:30 来源: 作者: 【 】 浏览:5
Tags:POJ 1696 Space Ant 计算 几何 应用

题目大意:平面内有一些点,我们要通过一些方式来走遍这所有的点,要求一个点只能走一次,只能向左转而不能向右转。求遍历这些点的顺序。


思路:数据范围是可以怎么搞都0ms的(n<=50,case<=100),所以只要有思路就可以了。

只能左转,想想好像有点像凸包啊。但是这个题要遍历所有的点,所以就把已经走过的点删掉,然后像凸包一样的往前走,每次找一个没走过的极角最小的点走,然后把它标记上。最后都走完就全部遍历完了。


CODE:


#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #define MAX 110 #define INF 0x7f7f7f7f using namespace std; struct Point{ int _id; double x,y; double alpha; Point(double _x,double _y):x(_x),y(_y) {} Point() {} Point operator -(const Point &a) { return Point(x - a.x,y - a.y); } bool operator <(const Point &a)const { return alpha < a.alpha; } void Read() { scanf("%d%lf%lf",&_id,&x,&y); } bool Cross(Point &a,Point &b) { Point p1 = *this - a,p2 = *this - b; return p1.x * p2.y - p2.x * p1.y < 0; } }point[MAX],now; int cases,points; int rubbish; inline void Initialize(); int main() { for(cin >> cases;cases; --cases) { scanf("%d",&points); Initialize(); for(int x,y,i = 1;i <= points; ++i) { point[i].Read(); if(point[i].y < now.y) now = point[i]; } printf("%d",points); now.x = 0; point[0] = now; for(int i = 1;i <= points; ++i) { for(int j = i + 1;j <= points; ++j) if(point[j].Judge(point[i],point[i - 1])) swap(point[i],point[j]); printf(" %d",point[i]._id); } puts(""); } return 0; } inline void Initialize() { now = Point(0,INF); }
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 1143 number game 下一篇分治、递归,冒泡、奇偶、快速、..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: