Space Ant

Space Ant

http://poj.org/problem?id=1696

Description

The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y1999 and called it M11. It has only one eye on the left side of its head and just three feet all on the right side of its body and suffers from three walking limitations:

  1. It can not turn right due to its special body structure.
  2. It leaves a red path while walking.
  3. It hates to pass over a previously red colored path, and never does that.


The pictures transmitted by the Discovery space ship depicts that plants in the Y1999 grow in special points on the planet. Analysis of several thousands of the pictures have resulted in discovering a magic coordinate system governing the grow points of the plants. In this coordinate system with x and y axes, no two plants share the same x or y
An M11 needs to eat exactly one plant in each day to stay alive. When it eats one plant, it remains there for the rest of the day with no move. Next day, it looks for another plant to go there and eat it. If it can not reach any other plant it dies by the end of the day. Notice that it can reach a plant in any distance. 
The problem is to find a path for an M11 to let it live longest. 
Input is a set of (x, y) coordinates of plants. Suppose A with the coordinates (xA, yA) is the plant with the least y-coordinate. M11 starts from point (0,yA) heading towards plant A. Notice that the solution path should not cross itself and all of the turns should be counter-clockwise. Also note that the solution may visit more than two plants located on a same straight line. 图片

Input

The first line of the input is M, the number of test cases to be solved (1 <= M <= 10). For each test case, the first line is N, the number of plants in that test case (1 <= N <= 50), followed by N lines for each plant data. Each plant data consists of three integers: the first number is the unique plant index (1..N), followed by two positive integers x and y representing the coordinates of the plant. Plants are sorted by the increasing order on their indices in the input file. Suppose that the values of coordinates are at most 100.

Output

Output should have one separate line for the solution of each test case. A solution is the number of plants on the solution path, followed by the indices of visiting plants in the path in the order of their visits.

 

1 .凸包 去掉凸包上的点

2. 剩下的点凸包 ,去掉凸包上的点

3. 重复上述两步知道剩下的点的个数为2或1,0

4. 还有一个重要的信息是 Description 中红色标注,用来确定唯一的顺序,要不然有很多条路径,代码以后再附上

 

 

//main.cpp

#pragma once

#include "stdafx.h"

#include "Test_1.h"

int _tmain(int argc, _TCHAR* argv[])

{

     Reti::test();

     return 0;

}

 

// Head_1.h

#pragma once

 

#include<iostream>

#include<time.h>

#include<assert.h>

#include<algorithm>

#include<vector>

#include<map>

#include<deque>

#include<string>

#include<List>

#include<iterator>

 

// Test_1.h

#pragma once

#include<cmath>

#include<stack>
#include"Head_1.h" 

using namespace std;

namespace Reti
{

;

struct Point

{

     int x;

     int y;

 

public:

 

     friend bool operator == ( const Point & val , const Point &rt)

     {

         return (val.x == rt.x && val.y == rt.y);

     }

     bool operator < ( const Point & val )

     {

         if( val.x < x)

              return 1;

         else if( (val.x == x && val.y < y))

         {

              return 1;

         }

         else

         {

              return 0;

         }

     }

     friend ostream & operator << (ostream & out, const Point & val)

     {

         out<<" x ="<<val.x<<" y ="<<val.y<<endl;

         return out;

     }

};

 

int  dist( const Point & lf ,const Point & rt)

{

     return ( lf.x - rt.x ) * ( lf.x - rt.x) + ( lf.y - rt.y) * ( lf.y - rt.y);

}

 

int crossProduct(const Point & init , const Point & lf , const Point & rt)

{

     return ( lf.x - init.x ) * ( rt.y - init.y) - ( rt.x - init.x) * ( lf.y - init.y) ;

}

 

struct pCmp

{

     bool operator ()( const Point & lfv ,const Point & rtv)

     {

         return ( (lfv.y < rtv.y) || (lfv.y == rtv.y && lfv.x < rtv.x ) );

     }

};

 

struct crossCmp

{

     Point m_val;

 

public:

     crossCmp( const Point & val):m_val( val){}

 

     bool operator() ( const Point & lf ,const Point & rt) //极角排序

     {

         int tp = crossProduct( m_val, lf, rt);

         if( tp > 0)

              return 1;

         if( tp == 0 && dist( m_val ,lf) < dist(m_val, rt))

              return 1;

         return 0;

     }

};

 

 

 

 

 

class vectorCmp  // comparator using in connect two hull , the point which turn left first corrspond to the last edge in outer hull is marked

{

     Point pt_start ;

     Point pt_next;

public:

     vectorCmp( const Point & val ,const Point & val2)

     {

         pt_start.x  = val.x ; pt_start.y = val.y;

         pt_next.x = val2.x ; pt_next.y = val2.y;

     }

     bool operator() ( const Point & lf ,const Point & rt)

     {

         //cout<<" lf "<<lf<<"  "<<rt<<pt_start<<pt_next<<endl;

         Point vector1 ,vector2 ,vector3;

         vector1.x = pt_next.x - pt_start.x ; vector1.y = pt_next.y - pt_start.y;

         vector2.x = lf.x - pt_next.x ; vector2.y = lf.y - pt_next.y;

         vector3.x = rt.x - pt_next.x ; vector3.y = rt.y - pt_next.y;

         double theta1 = ::acos( (vector1.x * vector2.x + vector1.y * vector2.y) / ( sqrt( (double)dist( pt_start, pt_next) ) * sqrt( (double)dist( pt_next ,lf) )));

         double theta2 = ::acos( (vector1.x * vector3.x + vector1.y * vector3.y) / ( sqrt( (double)dist( pt_start, pt_next) ) * sqrt( (double)dist( pt_next ,rt )) ));

         //cout<<"theta1 ="<<theta1<<" theta2 ="<<theta2<<endl;

         return theta1 < theta2;

     }

};

 

list<Point> point2list( Point * val, int len)

{

     list<Point> ret;

     ret.clear();

     forint i = 0 ; i < len ; i++)

     {

         Point tp;

         tp.x = val[i].x ;

         tp.y = val[i].y;

         ret.push_back(tp);

     }

     return ret;

}

 

void list2point(const list<Point> & val ,Point * pt)

{

     list<Point>::const_iterator pos;

     int i = 0;

     for( pos = val.begin() ; pos != val.end(); ++pos, i++)

     {

         pt[i] = *pos;

     }

}

 

void computeConvexHull( Point * val,int  len ,list<Point> & conv_hull) // computing convex hull

{

     Point * p = val;

     sort( p , p + len ,pCmp());

     sort( p + 1, p + len ,crossCmp(p[0]));

     vector< Point> mtp(p ,p + len);

     assert( mtp.size() >= 3);

     stack< Point > pStack;

     pStack.push(mtp[0]);

     pStack.push(mtp[1]);

     {

 

         int i = 2;

         while( i < mtp.size() )

         {

              Point pt_1 ,pt;

              {

                   pt_1 = pStack.top();

                   pStack.pop();

                   pt = pStack.top();

                   pStack.push(pt_1);

              }

 

              if( crossProduct( pt_1 ,pt ,mtp[i]) <= 0)

              {

                   pStack.push(mtp[ i++]);

              }

              else

              {

                   pStack.pop();

              }

         }

     }

 

     cout<<"pStack size ="<<pStack.size()<<endl;

     {

         list< Point> m_ret;

         while( !pStack.empty())

         {

              m_ret.push_front( pStack.top());

              pStack.pop();

         }

         conv_hull = m_ret;

     }

    

}

 

 

list<Point> getNonUsed(const list<Point> & allPoints ,const list<Point> & oneConv)

{

     list<Point> ret;

     list<Point>::const_iterator pos;

     for( pos = allPoints.begin() ; pos != allPoints.end() ; ++pos)

     {

         if( find( oneConv.begin() ,oneConv.end() ,*pos) != oneConv.end() ){}

         else

         {

              ret.push_back(*pos);

         }

     }

     return ret;

}

 

void conv_hull(Point *val ,int len)  //computing result

{

     vector< Point> mtp( val ,val + len);

     list< Point> pp( val ,val + len);

     int rest = mtp.size();

     list<Point> pS ;

     vector< Point> m_result;

     m_result.clear();

     int conv_count = 0;

     list< Point> hull ,nonUsed ,used;

     hull.clear();

     nonUsed = pp;

     used.clear();

     bool isFirst = true;

     while( rest > 2)

     {

         Point *hulltp = new Point[ nonUsed.size()];

         list2point( nonUsed ,hulltp);

         computeConvexHull(  hulltp, rest ,hull);

         cout<<" hull "<<++conv_count<<endl;

 

         cout<<" Computing link from one extern hull to  inner hull"<<endl;

         {

 

              if( isFirst )

              {

                   isFirst = false;

                   cout<<" hull.size() ="<<hull.size()<<endl;

                   m_result.insert(m_result.end() ,hull.begin() ,hull.end());

              }

              else

              {

                   cout<<" m_result.size() ="<<m_result.size()<<endl;

                   Point pt_s; pt_s.x = m_result[m_result.size() - 2].x ; pt_s.y = m_result[m_result.size() - 2].y;

                   Point pt_n; pt_n.x = m_result[m_result.size() - 1].x ; pt_n.y = m_result[m_result.size() - 1].y;

                   Point * v_tp = new Point[ hull.size()];

                   list2point( hull, v_tp);

                   sort( v_tp , v_tp + hull.size() ,vectorCmp( pt_s ,pt_n));

                   list<Point>::iterator pos ,pos1;

                   pos = find( hull.begin() , hull.end() ,v_tp[0]);

                   delete []v_tp;

                   assert( pos != hull.end() );

                   if( pos == hull.begin() )

                   {

                   }

                   else

                   {

                       list< Point> tp_1(pos ,hull.end());

                       pos1 = hull.begin();

                       while( pos1 != pos)

                       {

                            tp_1.insert(tp_1.end() ,*pos1);

                            ++pos1;

                       }

                       hull = tp_1;

 

                   }

 

                   pos = hull.begin();

                   while( pos != hull.end())

                   {

                       m_result.insert( m_result.end() ,*pos);

                       ++pos;

                   }

              }

         }

         copy( hull.begin() , hull.end() ,ostream_iterator<Point>(cout ,"---"));

         {

              list< Point>::iterator pos;

              for( pos = hull.begin() ; pos != hull.end() ; ++pos)

              {

                   used.push_back(*pos);

              }

         }

         nonUsed = getNonUsed( point2list(val, len) ,used);

         rest = nonUsed.size();

         delete []hulltp;

     }

 

     cout<<" rest =="<<rest<<endl; // take care of the rest points what can not make to a hull

     assert( rest < 3);

     {
        

         vector< Point> tp(nonUsed.begin() ,nonUsed.end());

         if( tp.size() == 1)

         {

              m_result.push_back( tp[0]);

         }
         else if( tp.size() == 0){} 

         else

         {

              //cout<<m_result[ m_result.size() -2]<<endl;

              sort( tp.begin() ,tp.end() ,vectorCmp(m_result[ m_result.size() -2] ,m_result[ m_result.size() -1]));

              m_result.push_back( tp[0]);

              m_result.push_back( tp[1]);

         }

 

     }

 

     cout<<" result = ________________________________"<<endl;

     copy( m_result.begin() , m_result.end() ,ostream_iterator<Point>(cout ,"---"));

    

}

 

 

void test()

{

     //Point p[14] ;

     //int p1[14] = { 6 ,11 ,8 ,12 ,9  ,1 ,2 ,15 ,14, 13, 5 ,7 ,10, 3};

     //int p2[14] = { 11 ,9 ,7 ,8 ,20 ,6 ,13 ,1 ,17 ,19 ,18 ,3 ,16 ,2};

     //for( int i = 0 ; i < 14 ;i++)

     //{

     //   p[i].x = p1[i];

     //   p[i].y = p2[i];

     //}

     //conv_hull( p ,14);

 

     Point p[10] ;

     int p1[10] = { 4, 9 ,5 ,1 ,3, 6 ,10 ,8 ,2 ,7 };

     int p2[10] = { 5, 8, 9, 7, 2, 3, 10, 1, 4, 6};

     forint i = 0 ; i < 10 ;i++)

     {

         p[i].x = p1[i];

         p[i].y = p2[i];

     }

     conv_hull( p ,10);

}

 

}

 

注:1.对共线上点是否还需去掉还未想过

    2.const_iterator 与iterator之间可以交换次序消去一个错误,具体用在哪里忘记了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Ant Design是一款基于React的企业级UI组件库,而Ant Design4.40是该库的一个版本。 Ant Design4.40作为Ant Design的一个重要版本,主要优化了一些已有组件的功能和性能,并且引入了一些新功能和组件。 首先,Ant Design4.40对一些常用组件进行了优化。例如,Table组件在这个版本中增强了分页功能,并且提供了更灵活的自定义选项,使得用户可以更方便地根据自身需求定制表格的分页功能。 其次,Ant Design4.40引入了一些新功能和组件,丰富了用户的选择。例如,新增了Cascader组件,该组件可以方便地实现级联选择功能,帮助用户在多个级别的选项中进行选择。另外,还增加了Space组件,该组件可以帮助用户更好地布局页面中的元素,提高页面的整体美观性。 除了上述的功能和组件优化外,Ant Design4.40还修复了一些BUG,提升了用户体验。该版本还对表单验证功能进行了完善,用户可以更方便地对表单进行校验和错误提示。 总之,Ant Design4.40是Ant Design的一个重要版本,通过优化已有组件功能和引入新功能和组件,提升了用户的使用体验,更好地满足了用户的需求。 ### 回答2: Ant Design是阿里巴巴在React开源社区中推出的一款UI组件库。Ant Design 4.40是Ant Design的一个版本,在这个版本中,设计和开发团队对Ant Design的功能和用户体验进行了一些更新和改进。 首先,Ant Design 4.40引入了许多新的组件和功能,使开发者可以更好地构建现代化的Web应用程序。例如,它提供了更丰富的表单组件,包括日期选择器、时间选择器、上传组件等,简化了表单的处理。另外,它还增加了新的图标和数据展示组件,如图表组件、树形控件等,帮助开发者更好地展示数据和信息。 其次,Ant Design 4.40在设计上进行了一些调整,使得界面更加美观和易于使用。它采用了现代化的扁平设计风格,简化了组件的样式和交互,提高了用户体验。同时,它还提供了丰富的主题样式,开发者可以根据自己的需求选择不同的主题,以满足不同项目的设计需求。 另外,Ant Design 4.40还对性能进行了一些优化,减少了组件库的体积和加载时间,提高了应用的性能和响应速度。它采用了懒加载和按需加载的技术,只在需要时才加载所需的组件和样式,减少了不必要的网络请求和资源消耗。 总之,Ant Design 4.40是一款功能强大、设计优雅、性能高效的UI组件库,为开发者提供了丰富的组件和样式,帮助开发者快速构建现代化的Web应用程序。无论是个人开发项目还是企业级应用,Ant Design都是一个值得考虑的选择。 ### 回答3: Ant Design 4.40 是一个流行的前端 UI 组件库,它提供了丰富的组件和样式,能够帮助开发者快速构建美观且功能强大的用户界面。 Ant Design 4.40 基于 React 技术栈开发,其设计思想遵循了 Ant Design 的原则:简洁、实用、适应性强、易于扩展和维护。通过使用 Ant Design 4.40,开发者可以节省大量的开发时间和精力,快速搭建出高质量的界面。 Ant Design 4.40 提供了大量的组件,包括基础组件(按钮、输入框、表单等)、布局组件(栅格、布局容器等)、数据展示组件(表格、图表等)、导航组件(菜单、面包屑等)、反馈组件(模态框、通知等)、工具组件(日期选择器、上传等)等等。这些组件都具有良好的一致性和可定制性,开发者可以根据自己的需求对组件进行深度定制。 除了组件,Ant Design 4.40 还提供了丰富的主题设置和样式定制功能,开发者可以自定义颜色、字体、边框等样式,以满足不同项目的设计需求。同时,Ant Design 4.40 还提供了文档和示例代码等资源,方便开发者学习和使用。 总之,Ant Design 4.40 是一个功能丰富、易于使用和高度可定制的前端 UI 组件库,它提供了大量的组件和样式,帮助开发者快速构建出美观且功能强大的用户界面。无论是个人项目还是商业项目,Ant Design 4.40 都是一个非常值得推荐的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值