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后的第一个挑战--凸包 此题可用凸包算法做,只要把压入凸包的点从原集合中排除即可,最终形成图形为螺旋线. 关于 ...
随机推荐
- CSS代码记录
1. 内容横向滚动的代码 .ul { display: box; display: -webkit-box; width: 250px; background: yellow; overflow-y: ...
- Junit单元测试笔记
什么是单元测试? 单元测试是开发者编写的一小段代码,用于检验被测代码的一个很小的.很明确的功能是否正确. 单元测试目的? 执行单元测试,是为了证明某段代码的行为确实和开发者所期望的一致. 白盒测试和单 ...
- 原生JS:Object对象详细参考
Object对象详细参考 本文参考MDN做的详细整理,方便大家参考MDN JavaScript原生提供一个Object对象(注意起首的O是大写),所有其他对象都继承自这个对象. 构造函数: Objec ...
- jQuery原型方法each使用和源码分析
jQuery.each方法是jQuery的核心工具方法之一,通用例遍方法,可用于例遍对象和数组.不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任何对象.通常需要两个参数 ...
- Vue数据绑定隐藏的神坑....
今天被Vue的一个坑给折磨了一天,终于发现是什么问题,我们先来模拟一个场景: 代码如下: <!DOCTYPE html> <html lang="en"> ...
- C# PPT 查找替换
public void ReplaceAll(string OldText,string NewText) { int num = PageNum(); ...
- Atitti.java android反编译解决方案-----虚拟机方案
Atitti.java android反编译解决方案-----虚拟机方案 哈哈,终极解决方案是虚拟机...c++也可以反编译为汇编代码,但无需担心,因为读懂汇编太麻烦..只要不能拿到c++源码就可.. ...
- disable-the-loopback-check-for-specific-host-names-on-all-sharepoint-web-and-application-servers/
Microsoft has introduced new feature – Loopback Security Check in Windows Server 2003 SP1 to prevent ...
- 一起来学习Android自定义控件1
概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...
- Xcode7.3更新后插件失效的解决方法
昨天发布的Xcode7.3,用了一天的时间终于装上了(网络不给力),突然发现原来所使用的插件不能用了,当时表情如下: 记得在更新7.2的时候也是这样的,当时重新下载的插件安装成功,但是未免有些麻烦,经 ...