题目传送门

题意:一个蚂蚁一直往左边走,问最多能走多少步,且输出路径

分析:就是凸包的变形题,凸包性质,所有点都能走。从左下角开始走,不停排序。有点纠结,自己的凸包不能AC。待理解透凸包再来写。。 好像只能用卷包裹法来写,就是从一个起点出发,每次相对于起点用叉积排序,选择最外侧的点,更新起点。

/************************************************
* Author :Running_Time
* Created Time :2015/10/27 星期二 14:20:36
* File Name :POJ_1696.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
int dcmp(double x) { //三态函数,减少精度问题
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
struct Point { //点的定义
double x, y;
int id;
Point (double x=0, double y=0, int id = 0) : x (x), y (y), id (id) {}
Point operator + (const Point &r) const { //向量加法
return Point (x + r.x, y + r.y);
}
Point operator - (const Point &r) const { //向量减法
return Point (x - r.x, y - r.y);
}
Point operator * (double p) { //向量乘以标量
return Point (x * p, y * p);
}
Point operator / (double p) { //向量除以标量
return Point (x / p, y / p);
}
bool operator < (const Point &r) const { //点的坐标排序
return x < r.x || (x == r.x && y < r.y);
}
bool operator == (const Point &r) const { //判断同一个点
return dcmp (x - r.x) == 0 && dcmp (y - r.y) == 0;
}
};
typedef Point Vector; //向量的定义
Point read_point(void) { //点的读入
int id;
double x, y;
scanf ("%d%lf%lf", &id, &x, &y);
return Point (x, y, id);
}
double dot(Vector A, Vector B) { //向量点积
return A.x * B.x + A.y * B.y;
}
double cross(Vector A, Vector B) { //向量叉积
return A.x * B.y - A.y * B.x;
}
double polar_angle(Vector A) { //向量极角
return atan2 (A.y, A.x);
}
double length(Vector A) { //向量长度,点积
return sqrt (dot (A, A));
}
double angle(Vector A, Vector B) { //向量转角,逆时针,点积
return acos (dot (A, B) / length (A) / length (B));
}
Vector rotate(Vector A, double rad) { //向量旋转,逆时针
return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad));
}
Vector nomal(Vector A) { //向量的单位法向量
double len = length (A);
return Vector (-A.y / len, A.x / len);
}
Point line_line_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程
Vector U = p - q;
double t = cross (W, U) / cross (V, W);
return p + V * t;
}
double point_to_line(Point p, Point a, Point b) { //点到直线的距离,两点式
Vector V1 = b - a, V2 = p - a;
return fabs (cross (V1, V2)) / length (V1);
}
double point_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式
if (a == b) return length (p - a);
Vector V1 = b - a, V2 = p - a, V3 = p - b;
if (dcmp (dot (V1, V2)) < 0) return length (V2);
else if (dcmp (dot (V1, V3)) > 0) return length (V3);
else return fabs (cross (V1, V2)) / length (V1);
}
Point point_line_proj(Point p, Point a, Point b) { //点在直线上的投影,两点式
Vector V = b - a;
return a + V * (dot (V, p - a) / dot (V, V));
}
bool can_inter(Point a1, Point a2, Point b1, Point b2) { //判断线段相交,两点式
double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1),
c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1);
return dcmp (c1) * dcmp (c2) < 0 && dcmp (c3) * dcmp (c4) < 0;
}
bool on_seg(Point p, Point a1, Point a2) { //判断点在线段上,两点式
return dcmp (cross (a1 - p, a2 - p)) == 0 && dcmp (dot (a1 - p, a2 - p)) < 0;
}
double area_triangle(Point a, Point b, Point c) { //三角形面积,叉积
return fabs (cross (b - a, c - a)) / 2.0;
}
double area_poly(Point *p, int n) { //多边形面积,叉积
double ret = 0;
for (int i=1; i<n-1; ++i) {
ret += fabs (cross (p[i] - p[0], p[i+1] - p[0]));
}
return ret / 2;
}
int pos;
bool vis[55];
Point p[55];
bool cmp(Point a, Point b) {
double t = cross (a - p[pos], b - p[pos]);
if (dcmp (t) == 0) {
return length (a - p[pos]) < length (b - p[pos]);
}
else if (dcmp (t) < 0) return false;
else return true;
} /*
点集凸包
*/
vector<int> convex_hull(vector<Point> P) {
sort (P.begin (), P.end ());
int n = P.size (), k = 0;
vector<Point> ret (n * 2);
vector<int> id (n * 2);
for (int i=0; i<n; ++i) {
while (k > 1 && cross (ret[k-1] - ret[k-2], P[i] - ret[k-1]) <= 0) k--;
id[k] = P[i].id;
ret[k++] = P[i];
}
for (int i=n-2, t=k; i>=0; --i) {
while (k > t && cross (ret[k-1] - ret[k-2], P[i] - ret[k-1]) <= 0) k--;
id[k] = P[i].id;
ret[k++] = P[i];
}
ret.resize (k-1); id.resize (k - 1);
return id;
} int main(void) {
int T; scanf ("%d", &T);
while (T--) {
int n; scanf ("%d", &n);
for (int i=0; i<n; ++i) {
p[i] = read_point ();
if (p[0].y > p[i].y || (p[0].y == p[i].y && p[0].x > p[i].x)) swap (p[0], p[i]);
}
pos = 0;
for (int i=1; i<n; ++i) {
sort (p+i, p+n, cmp);
pos++;
}
printf ("%d", n);
for (int i=0; i<n; ++i) {
printf (" %d", p[i].id);
}
puts ("");
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}

  

简单几何(凸包) POJ 1696 Space Ant的更多相关文章

  1. poj 1696 Space Ant (极角排序)

    链接:http://poj.org/problem?id=1696 Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  2. 2018.07.04 POJ 1696 Space Ant(凸包卷包裹)

    Space Ant Time Limit: 1000MS Memory Limit: 10000K Description The most exciting space discovery occu ...

  3. poj 1696:Space Ant(计算几何,凸包变种,极角排序)

    Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2876   Accepted: 1839 Descrip ...

  4. POJ 1696 Space Ant 卷包裹法

    Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3316   Accepted: 2118 Descrip ...

  5. POJ 1696 Space Ant(极角排序)

    Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2489   Accepted: 1567 Descrip ...

  6. poj 1696 Space Ant(模拟+叉积)

    Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3840   Accepted: 2397 Descrip ...

  7. POJ 1696 Space Ant(点积的应用)

    Space Ant 大意:有一仅仅蚂蚁,每次都仅仅向当前方向的左边走,问蚂蚁走遍全部的点的顺序输出.開始的点是纵坐标最小的那个点,開始的方向是開始点的x轴正方向. 思路:从開始点開始,每次找剩下的点中 ...

  8. POJ 1696 - Space Ant 凸包的变形

    Technorati Tags: POJ,计算几何,凸包 初学计算几何,引入polygon后的第一个挑战--凸包 此题可用凸包算法做,只要把压入凸包的点从原集合中排除即可,最终形成图形为螺旋线. 关于 ...

  9. POJ 1696 Space Ant(凸包变形)

    Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scie ...

随机推荐

  1. 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(六)

    Service和Thread的关系 不少初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread? 答案是Service和T ...

  2. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. Notice The minimu ...

  3. iOS 和Android中的基本日期处理

    提到日期处理,主要有2个参数,一个是所在的时区,一个是所用的日历方法. 主要涉及2大类问题,一类是日期类型和字符串之间的转化,另一类是日期的计算问题.ios和android都提供了相应的类来处理问题. ...

  4. 转数据库Sharding的基本思想和切分策略

    本文着重介绍sharding的基本思想和理论上的切分策略,关于更加细致的实施策略和参考事例请参考我的另一篇博文:数据库分库分表(sharding)系列(一) 拆分实施策略和示例演示 一.基本思想 Sh ...

  5. 【JAVA、C++】LeetCode 019 Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  6. Greedy:Saruman's Army(POJ 3069)

    2015-09-06 萨鲁曼军队 问题大意:萨鲁曼白想要让他的军队从sengard到Helm’s Deep,为了跟踪他的军队,他在军队中放置了魔法石(军队是一条线),魔法石可以看到前后距离为R的距离, ...

  7. ubuntu下java环境变量配置

    转自:http://blog.csdn.net/tenpage/article/details/7215810 如果是配置java环境,请参照这篇,更简单:在Ubuntu 12.04 LTS上安装JD ...

  8. 【pymongo】连接认证 auth failed解决方法

    故事背景: 我在虚拟机(ip:192.168.xx.xx)上建立了一个mongo的数据库,里面已经存好了内容.里面的一个database叫做 "adb", 里面有个collecti ...

  9. Java性能优化权威指南-读书笔记(五)-JVM性能调优-吞吐量

    吞吐量是指,应用程序的TPS: 每秒多少次事务,QPS: 每秒多少次查询等性能指标. 吞吐量调优就是减少垃圾收集器消耗的CPU周期数,从而将更多的CPU周期用于执行应用程序. CMS吞吐调优 CMS包 ...

  10. 修改PHP网站默认首页

    一般默认的首页文件是index.php index.php3 index.html index.htm之类的,要想修改为myNewIndex.php, 进入服务器Apache目录,找到httpd.co ...