zoukankan      html  css  js  c++  java
  • POJ 1696 Space Ant (极角排序)

    题目链接

    题意 : 一只蚂蚁,只会向左转,现在给出平面上很多个点,求解一种走法,能使得蚂蚁能经过的点最多,每个顶点该蚂蚁只能经过一次,且所行走的路线不能发生交叉.

    思路 : 每次找的时候排一下序然后输出最外边的点就行了。

    极角排序是根据坐标系内每一个点与x轴所成的角,逆时针比较,。按照角度从小到大的方式排序。

    其实我觉得就是用atan2排序 。。。。。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <math.h>
     5 #include <algorithm>
     6 
     7 using namespace std ;
     8 
     9 struct point
    10 {
    11     int x,y ;
    12     int ID ;
    13 }p[100];
    14 point pp ;
    15 
    16 bool cmp(point a,point b)
    17 {
    18     return a.y < b.y ;
    19 }
    20 double cross(point a,point b,point c)
    21 {
    22     return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y) ;
    23 }
    24 
    25 double dis(point a,point b)
    26 {
    27     return sqrt(double((b.x-a.x)*(b.x-a.x)+ (b.y-a.y)*(b.y-a.y))) ;
    28 }
    29 bool cmp1(point a,point b)
    30 {
    31     int cr = cross(a,b,pp) ;//判断顺时针还是逆时针
    32     if(cr > 0) return true ;
    33     else if(cr < 0) return false ;
    34     else//共线
    35     {
    36         if(dis(a,pp) < dis(b,pp)) return true ;
    37         else return false ;
    38     }
    39 }
    40 int main()
    41 {
    42     int T ,n;
    43     scanf("%d",&T) ;
    44     while(T--)
    45     {
    46         scanf("%d",&n) ;
    47         for(int i = 0 ; i < n ; i++)
    48         {
    49             scanf("%d %d %d",&p[i].ID,&p[i].x,&p[i].y) ;
    50         }
    51         sort(p,p+n,cmp) ;
    52         pp = p[0] ;
    53         printf("%d",n) ;
    54         printf(" %d",pp.ID) ;
    55         for(int i = 1 ; i < n ; i++)
    56         {
    57             sort(p+i,p+n,cmp1) ;
    58             pp = p[i] ;
    59             printf(" %d",pp.ID) ;
    60         }
    61         printf("
    ") ;
    62     }
    63     return 0 ;
    64 }
    View Code
  • 相关阅读:
    转载--重写、覆盖、重载、多态几个概念的区别分析
    笔试题--奇虎360-2013
    转载---数据挖掘十大经典算法
    Nginx的启动、停止与重启
    程序员的十种级别,看看你属于哪一种?
    C标签的用法
    eclipse修改代码后都需要clean的解决办法
    创建一个jFinal项目
    java redirect用法
    java获取访问者真实的IP地址
  • 原文地址:https://www.cnblogs.com/luyingfeng/p/3941477.html
Copyright © 2011-2022 走看看