POJ 1696 Space Ant 【几何叉积】
传送门:POJ 1696
Problem 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 ared 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.
Sample Input
2
10
1 4 5
2 9 8
3 5 9
4 1 7
5 3 2
6 6 3
7 10 10
8 8 1
9 2 4
10 7 6
14
1 6 11
2 11 9
3 8 7
4 12 8
5 9 20
6 3 2
7 1 6
8 2 13
9 15 1
10 14 17
11 13 19
12 5 18
13 7 3
14 10 16
Sample Output
10 8 7 3 4 9 5 6 2 1 10
14 9 10 11 5 12 8 7 6 13 4 14 1 3 2
解题思路:
叉积的应用+dfs。易证:按照一定的顺序,每次选择当前左转角度最小的点(相等则选距离最近的点),必能按条件遍历所有的点。
其实无论输入什么样的点集,一定可以走完全部n个点的,这是凸包的性质决定
AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const double eps=1e-8;
int sgn(double x)//判断一个数>,<,=0
{
if(fabs(x)<eps) return 0;
if(x<0) return -1;
else return 1;
}
int dcmp(double x, double y)
{
if(fabs(x - y) < eps)
return 0;
if(x > y)
return 1;
return -1;
}
struct point //点的操作
{
double x,y;
int dep,id;
point() {
}
point(double x,double y) : x(x),y(y) {
}
void input()
{
scanf("%lf%lf",&x,&y);
}
bool operator ==(point b)const {
return sgn(x-b.x)==0&&sgn(y-b.y)==0;
}
bool operator <(point b)const {
return sgn(x-b.x)==0 ? sgn(y-b.y)<0 : x<b.x;
}
point operator -(const point &b)const {
return point(x-b.x,y-b.y);
}
point operator +(const point &b)const {
return point(x+b.x,y+b.y);
}
point operator *(const double &k)const {
return point(x*k,y*k);
}
point operator /(const double &k)const {
return point(x/k,y/k);
}
double operator ^(const point &b)const { //叉乘
return x*b.y-y*b.x;
}
double operator *(const point &b)const { //点乘
return x*b.x+y*b.y;
}
double len(){ //返回长度
return hypot(x,y);
}
double len2(){ //返回长度平方
return x*x+y*y;
}
//求单位向量
point trunc(double r){
double l=len();
if(!sgn(l)) return *this;
r/=l;
return point(x*r,y*r);
}
};
int m,n;
point p[55];
//对于同顶点的两个向量p1,p2 ,如果p1^p2>0,说明p1在p2的顺时针方向,如果p1^p2<0,说明p1在p2的逆时针方向
double xmult(point p0,point p1,point p2)
{
point lin=point(p1.x-p0.x,p1.y-p0.y);
point tlin=point(p2.x-p0.x,p2.y-p0.y);
return lin^tlin;
}
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(point a,point b)
{
return a.dep<b.dep;
}
void dfs(int x,int dep)
{
if(dep==n) return;
int key=1;
while(p[key].dep!=-1)
key++;
for(int i=key+1;i<=n;i++)
{
if(p[i].dep!=-1) continue;
double w=xmult(p[x],p[key],p[i]);
if(sgn(w)==-1)
key=i;
else if(sgn(w)==0&&dist(p[x],p[key])>dist(p[x],p[i]))
key=i;
}
p[key].dep=dep;
dfs(key,dep+1);
}
int main()
{
int id;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
int key=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&id);
p[id].input();
p[id].dep=-1;
p[id].id=id;
if(p[id].y<p[key].y) key=id;
}
p[key].dep=0;
dfs(key,1);
sort(p+1,p+1+n,cmp);
printf("%d",n);
for(int i=1;i<=n;i++)
{
printf(" %d",p[i].id);
}
printf("\n");
}
return 0;
}
上一篇: 回顾-二分
下一篇: 修改eclipse字体大小