POJ 1696 Space Ant --枚举,模拟,贪心,几何
题意: 有很多点,从最右下角的点开始走起,初始方向水平向右,然后以后每步只能向左边走,问最多能走多少个点。
解法: 贪心的搞的话,肯定每次选左边的与它夹角最小的点,然后走过去。 然后就是相当于模拟地去选点,然后计数,然后走过去。这题就这么搞定了。
我这里用了set和vector。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#define Mod 1000000007
#define pi acos(-1.0)
#define eps 1e-8
using namespace std; int nonsense;
struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%d%lf%lf",&nonsense,&x,&y); }
};
typedef Point Vector;
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.y < b.y || (a.y == b.y && a.x < b.x); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } //data segment
Point p[];
set<int> sk;
vector<int> ans;
//data ends int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sk.clear(), ans.clear();
for(i=;i<=n;i++) p[i].input(), sk.insert(i);
int mintag = ;
double miny = p[].y, minx = p[].x;
for(i=;i<=n;i++)
{
if(p[i].y < miny)
mintag = i, miny = p[i].y, minx = p[i].x;
else if(p[i].y == miny && p[i].x < minx)
mintag = i, minx = p[i].x;
}
Vector now = Vector(,);
Point pnow = p[mintag];
ans.push_back(mintag);
sk.erase(mintag);
set<int>::iterator it;
while()
{
double Mini = Mod;
int tag;
for(it=sk.begin();it!=sk.end();it++)
{
Point A = p[*it]-pnow;
double ang = Angle(now,A);
if(ang < Mini)
Mini = ang, tag = *it;
}
if(Mini >= Mod) break;
now = p[tag]-pnow;
pnow = p[tag];
ans.push_back(tag);
sk.erase(tag);
if(sk.empty()) break;
}
printf("%d",ans.size());
for(i=;i<ans.size();i++)
printf(" %d",ans[i]);
puts("");
}
return ;
}
POJ 1696 Space Ant --枚举,模拟,贪心,几何的更多相关文章
- poj 1696 Space Ant(模拟+叉积)
Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3840 Accepted: 2397 Descrip ...
- poj 1696 Space Ant (极角排序)
链接:http://poj.org/problem?id=1696 Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- POJ 1696 Space Ant(极角排序)
Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2489 Accepted: 1567 Descrip ...
- POJ 1696 Space Ant 卷包裹法
Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3316 Accepted: 2118 Descrip ...
- POJ 1696 Space Ant(点积的应用)
Space Ant 大意:有一仅仅蚂蚁,每次都仅仅向当前方向的左边走,问蚂蚁走遍全部的点的顺序输出.開始的点是纵坐标最小的那个点,開始的方向是開始点的x轴正方向. 思路:从開始点開始,每次找剩下的点中 ...
- 2018.07.04 POJ 1696 Space Ant(凸包卷包裹)
Space Ant Time Limit: 1000MS Memory Limit: 10000K Description The most exciting space discovery occu ...
- poj 1696:Space Ant(计算几何,凸包变种,极角排序)
Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2876 Accepted: 1839 Descrip ...
- 简单几何(凸包) POJ 1696 Space Ant
题目传送门 题意:一个蚂蚁一直往左边走,问最多能走多少步,且输出路径 分析:就是凸包的变形题,凸包性质,所有点都能走.从左下角开始走,不停排序.有点纠结,自己的凸包不能AC.待理解透凸包再来写.. 好 ...
- POJ 1696 - Space Ant 凸包的变形
Technorati Tags: POJ,计算几何,凸包 初学计算几何,引入polygon后的第一个挑战--凸包 此题可用凸包算法做,只要把压入凸包的点从原集合中排除即可,最终形成图形为螺旋线. 关于 ...
随机推荐
- rem在响应式布局中的应用
rem/em/px/pt的基友关系 px 像素相对长度单位,相对于显示器屏幕分辨率而言 em 相对长度单位,根据其父元素来设置字体大小 pt point,是印刷行业常用单位,等于1/72英寸 rem ...
- ViewPager的使用小技巧
1.在ViewPager中默认加载当前屏幕上的界面和左右相邻界面的数据从而实现页面滑动的快速切换.可以通过调用setOffscreenPageLimit(int)方法,定制预加载相邻页面的数目. 2. ...
- CALayer 易混淆的两个属性 - position和anchorPoint
1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; ...
- Asp.net中GridView使用详解(引)
GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠标移到Gr ...
- 如何让光标处于EditText的末尾
经测试发现,如果EditText预先有内容,光标自然会在文字的末尾 . 但是如果预先内容为空,然后设置好内容,这种情况下光标自然会在文字的开头,所以这种情况下可以这样做让光标位于末尾: editTex ...
- 转:JQuery.Ajax之错误调试帮助信息
今天发现一篇讲Ajax比较好的文章,汇总下,作为自己的知识储备. 下面是Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. typ ...
- 坑!坑!坑!防不胜防的unsigned int的运算
我很早之前就知道,unsigned int与int运算的时候,int会被转化为unsigned int来进行运算.一直觉得定这条规则的人是极度反人类的,虽说unsigned int可以表示更大的正值, ...
- php5.3新特性 之 mysql native driver(mysqlnd)
概述 本文主要写给sa看的.码农就不用看了. mysql native driver(mysqlnd) 自从php5.3.0开始成为官方源代码的一部分, 用来取代传统的mysql client lib ...
- 【SQL篇章】【SQL语句梳理 :--基于MySQL5.6】【已梳理:ALTER TABLE解析】
ALTER TABLE 解析实例: SQL: 1.增加列 2.增加列,调整列顺序 3.增加索引 4.增加约束 5.增加全文索引FULL-TEXT 6.改变列的默认值 7.改变列名字(类型,顺序) 8. ...
- SQL Server 使用游标更新数据库中的数据(使用存储过程)
ALTER PROCEDURE [dbo].[POR_CURSOR_FOR_UPDATE] --创建存储过程 AS BEGIN SET nocount ON --忽略行数显示 DECLARE Upda ...