大致题意:

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

卷包裹法:

形象思维假想一根无限长的绳子,一开始,左端粘在点集的最低点上。若有多个最低点,则去最做的最低点。然后拉动绳子的另一端逆时针绕行,每次紧贴一条边(这就是凸包上的一条边),这样绕行一周,绳子的形状就是凸包,这就是所谓的卷包裹法;该算法的效率比较低是O(n^2)的时间效率;

View Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
using namespace std;
class Point
{
public:int x,y;
}point[54];
int on_segment( Point p, Point q1,Point q2 )//叉积
{return ( q1.x - p.x )*( q2.y - p.y ) - ( q2.x - p.x )*( q1.y - p.y );
}
double Distance( Point  a, Point b )//距离计算
{return sqrt( double(( a.x - b.x )*( a.x - b.x ) + ( a.y - b.y )*( a.y - b.y )) );
}
bool segment( Point p, Point q1,Point q2 )
{int t = on_segment( p , q1 , q2 );if( t < 0 )  return true;else if( t==0 && Distance( p , q1 ) > Distance( p , q2 ))return true;return false;
}
int main(  )
{int N,n,num;while( scanf( "%d",&N )==1 ){while( N -- ){bool visit[54]={0};//标记已经进入凸包的点 int record[54],count=0;//记录点顺序 scanf( "%d",&n );int min_y = 0x7fffffff,t=0; for( int i = 1; i <= n ; i++ ){scanf( "%d %d %d",&num,&point[i].x , &point[i].y );if( point[i].y < min_y )//寻找最低点
              {min_y = point[i].y;t = i;}        }record[++count] = t; visit[t] = true;while( count < n ){int temp,s=record[count];//最新的凸包起点 for( int i = 1 ; i <= n ; i ++ )//寻找新的凸包起点
              {if( !visit[i] ){temp = i;break;    }        }        for( int i = 1 ; i <= n ; i++ )//寻找最优的凸包起点
              {if( !visit[i] && i != temp ){if( segment( point[s],point[temp],point[i] )  ){temp = i;        }         }    }visit[temp] = true; record[++count] = temp;}printf( "%d",count );for( int i = 1; i <= count ; i++ ){printf( " %d",record[i] );        }puts( "" );}} //system( "pause" );return 0;
}

下面用Graham—Scan算法:用极坐标排序。

View Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
using namespace std;
class Point
{
public:int x,y,num;
}point[54];
int pos;
double Distance( Point a, Point b )
{return sqrt( double(( a.x - b.x )*( a.x - b.x ) + ( a.y - b.y )*( a.y - b.y )) );
}
int segment( Point p1 ,Point p2 ,Point q )
{return     ( p1.x - q.x )*( p2.y - q.y ) - ( p2.x - q.x )*( p1.y - q.y );
}
bool cmp( Point  a, Point  b )
{int t = segment( a , b, point[pos] ); if( t > 0 ) return true;else if( t==0 && Distance( a , point[pos] ) < Distance( b , point[pos] )) return true;return false;
}
int main(  )
{int N,n,num;while( scanf( "%d",&N )==1 ){while( N -- ){scanf( "%d",&n );for( int i = 0 ; i < n ; i ++ ){scanf( "%d %d %d",&point[i].num , &point[i].x ,&point[i].y );if( point[i].y < point[0].y )swap( point[0],point[i] );        }printf( "%d %d",n ,point[0].num );for( int i = 1 ; i < n ; i++ ){pos = i - 1;sort( point + i , point + n ,cmp );//极坐标排序 printf( " %d",point[i].num );}    puts( "" );}    }//system( "pause" );return 0;
}

转载于:https://www.cnblogs.com/bo-tao/archive/2012/07/06/2579695.html

poj 1696 Space Ant相关推荐

  1. POJ 1696 Space Ant(极角排序)【计算几何】

    ACM博客_kuangbin POJ 1696 Space Ant(极角排序) Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  2. POJ - 1696 Space Ant(极角排序)

    题目链接:点击查看 题目大意:现在有一只特殊的蚂蚁,它会按照以下规则尽可能长的寻找路径: 不能回头 不能右转 只能逆时针行走 现在给出n个点,输出最长的路径 题目分析:既然是逆时针旋转,那么每次只能走 ...

  3. POJ 1696 Space Ant 极角排序(叉积的应用)

    题目大意:给出n个点的编号和坐标,按逆时针方向连接着n个点,按连接的先后顺序输出每个点的编号. 题目思路:Cross(a,b)表示a,b的叉积,若小于0:a在b的逆时针方向,若大于0a在b的顺时针方向 ...

  4. 【POJ - 1696】Space Ant (凸包,最小极角,排序)

    题干: The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists t ...

  5. POJ1696 Space Ant

    原题传送:http://poj.org/problem?id=1696 利用叉积判断所有点与线段位置关系.下一棵植物A'的位置与蚂蚁位置A的连线得到线段AA',如果所有没有吃掉的植物均在AA'的左端, ...

  6. POJ 2392 Space Elevator(多重背包变形)

    Q: 额外添加了最大高度限制, 需要根据 alt 对数据进行预处理么? A: 是的, 需要根据 alt 对数组排序 Description The cows are going to space! T ...

  7. poj 2392 Space Elevator

    题意 :给定n种积木,每种积木都有一个高度hi,一个数量ci,还有一个限制条件,这个积木所在的位置不能高于ai,问能叠起的最大高度// 这里有个问题需要注意 就是ai小的要先进行背包 不然 在 ai高 ...

  8. poj 1696(极角排序)

    题目让找出一条最长的非右拐的路径. 这道题类似于凸包的Graham扫描法,依次对某点进行极角的排序. 参考博客:http://blog.csdn.net/zhengnanlee/article/det ...

  9. POJ 计算几何入门题目推荐

      其实也谈不上推荐,只是自己做过的题目而已,甚至有的题目尚未AC,让在挣扎中.之所以推荐计算几何题,是因为,本人感觉ACM各种算法中计算几何算是比较实际的算法,在很多领域有着重要的用途(例如本人的专 ...

最新文章

  1. 2010年的退休畅想
  2. form表单中,file选择图片后预览
  3. mongodb 启动_精心总结--mongodb分片集群启动与关闭
  4. MTK优美代码赏析6:电话本里的快速排序和插入排序算法
  5. 微信应该砍掉这些功能
  6. android制作弹出框样式,Android Dialog 弹出框 自定义 样式
  7. mysql8.0和phpmyadmin_MySQL 8.0上的phpMyAdmin?mysql-问答-阿里云开发者社区-阿里云
  8. 汽车主要电子控制系统模块
  9. Neo4j Desktop 添加算法插件Graph Algorithms
  10. 解决IAR编译警告Warning[Pa089]: enumerated type mixed with another enumerated type
  11. opencv-牛刀小试
  12. Gradle's dependency cache may be corrupt (this sometimes occurs after a net错误解决
  13. 山西票号的内部控制分析
  14. MYSQL求百分比的几种方法
  15. 使用Camtasia来给视频或者图片调色
  16. MySQL中幻读现象
  17. 2020李宏毅机器学习笔记-More about Auto-encoder
  18. 人机交互及界面设计序言
  19. chrome浏览器打不开网页 显示远程计算机或设备将不接受连接?
  20. 【Arcgis】绘制甘肃省行政区划地图

热门文章

  1. Word Count Example of Hadoop V1.0 – Mapper的实现
  2. ubuntu16 下 源码配置Lnmp环境
  3. AutoPostBack与AutoComplete介绍
  4. LINUX 第七章 Squid配置
  5. 1. Windows Powershell初接触
  6. 想成为架构师,你必须掌握的CAP细节
  7. 连续和问题C语言,内存连续的有关问题
  8. 匿名函数php作用,深入理解PHP中的匿名函数
  9. 投递简历总是石沉大海?HR表现的机会都不给你?【Python爬虫实战:简历模板采集】
  10. c语言dfs算法,DFS算法源程序