zoukankan      html  css  js  c++  java
  • 凸包的变形 基点每次都在变化 poj 1696 Space Ant

    题目来源:http://poj.org/problem?id=1696

    分析:

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

    思路:

    先将所有的点按纵坐标从小到大进行排序,选择第一个点然后对剩下的点进行按极角从小到大排序,选择排序后的第一个点,在对剩下的点按极角从小到大进行排序以此到最后一个点。

    代码如下:

    #include <iostream>
    #include <algorithm>
    #include <stdlib.h>
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <string.h>
    #include <vector>
    #include <set>
    #include <math.h>
    #include <cmath>
    #include <map>
    #include <queue>
    
    using namespace std;
    typedef long long ll;
    
    struct Point{
        int x,y,id;
    };
    int xmult(Point p0,Point p1,Point p2) // p0p1 向p2转 叉积形式是 p0p1X p0p2
    {
        return (p1.x-p0.x)*(p2.y-p0.y) - (p1.y-p0.y)*(p2.x-p0.x);
    }
    int dist(Point p1,Point p2)
    {
        return ( p2.x-p1.x )*( p2.x-p1.x ) + ( p2.y-p1.y )*( p2.y-p1.y );
    }
    const int N=60;
    Point List[N];   // 输入点集Q
    int stack[N];
    int now; // 为基准点的序号
    // 按极角排序,如果极角相等则按距离从小到大,sort是按从小到大排列的,故需对<符号重载
    int operator<(Point p1,Point p2)
        {
        double  tmp=xmult(List[now],p1,p2); //List[now]为基点 , 向p2转
        if(tmp>0) return 1;
        else if(tmp==0 && dist( List[now], p1 ) < dist( List[now], p2 ) ) return 1;
        else return 0;
        }
    int cmp(Point p1,Point p2)   // 先按y从小到大, y相等 则按 x 从小到大,最小的 即是最左下方
    {
        if(p1.y != p2.y)
            return p1.y < p2.y;
        else return p1.x < p2.x;
    }
    int main() {
        int T,n;
        cin>>T;
        while(T--)
        {
            cin>>n;
        for(int i=0;i<n;i++)
            cin>>List[i].id>>List[i].x>>List[i].y;
        sort(List, List + n,cmp);   // 选择y值最小的点 作为第一个点
        now=0;
        int si=0;
        stack[si]=List[now].id;
        for(int i=1; i<n;i++)
        {
            sort(List+i,List+n);    // 对List[now] 基点做 极角排序 , 选择第一个点
            si++,now++;
            stack[si]=List[now].id;
        }
    
            printf("%d",n);
            for(int i=0;i<n;i++)
            {
                printf(" %d",stack[i]);
            }
            printf("
    ");
    
        }
        return 0;
    }
  • 相关阅读:
    Dllimport函数時无法在Dll中找到的入口点
    cb35a_c++_STL_算法_for_each
    cb34a_c++_STL_算法_查找算法_(7)_lower_bound
    cb33a_c++_STL_算法_查找算法_(6)binary_search_includes
    cb32a_c++_STL_算法_查找算法_(5)adjacent_find
    cb31a_c++_STL_算法_查找算法_(4)find_first_of
    cb30a_c++_STL_算法_查找算法_(3)search_find_end
    cb29a_c++_STL_算法_查找算法_(2)search_n
    cb28a_c++_STL_算法_查找算法_(1)find_find_if
    cb27a_c++_STL_算法_最小值和最大值
  • 原文地址:https://www.cnblogs.com/zn505119020/p/3627560.html
Copyright © 2011-2022 走看看