POJ 1696 Space Ant

简单题,题意就是从y坐标最低的点出发,每次只能向左转,并且不能与之前走过的路相交,问最多能经过多少个点。
	这其实就是一个对每个点,找求最小极角的问题首先判断当前的角与堆栈顶部的两个角的夹角是否成左旋,然后找与当前栈顶的点构成的直线角度最小的点入栈,然后重复上面的步骤,直到没有满足条件的点为止。代码如下:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
double eps=1e-8;
bool map[60];
struct Point{
	int x,y,index;
}plant[60];
Point que[60];
int n;
int top;
int mult( Point p1, Point p2, Point p3)		//p2p1,p2p3的叉积
{
	return (p1.x - p2.x)*(p3.y - p2.y)-( p1.y - p2.y )*( p3.x - p2.x );
}
int cmp(Point a,Point b)
{
	return a.y < b.y ;
}
void addPlant()
{
	int i,tmp;
	que[1] = plant[0];
	top=1;
	while(1)
	{
		tmp=0;
		double angle=4.2;
		for( i = 1; i < n; i++)
			if( !map[i] && mult( que[top-1] , que[top] , plant[i] ))				//判断是否与栈顶成左旋
			{
				int x1 = plant[i].x - que[top].x,x2 = que[top].x - que[top-1].x,y1 = plant[i].y - que[top].y,y2 = que[top].y - que[top-1].y;
				double ang = acos((x1 * x2 + y1 * y2)/sqrt(double(x1 * x1 + y1 * y1)*(x2 * x2 + y2 * y2)));
				if(ang < angle - eps)		//找角度最小的角
				{
					angle = ang;
					tmp = i;
				}
			}
		if( tmp )
		{
			que[++top] = plant[tmp];
			map[tmp] = 1;
		}
		else 
			break;
	}
			
}
int main()
{
	int t,i;
	scanf("%d",&t);
	while(t--)
	{
		memset(map,0,sizeof(map));
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%d %d %d", &plant[i].index , &plant[i].x , &plant[i].y );
		sort(plant,plant + n,cmp);//按y坐标排序
		que[0].y = plant[0].y;
		que[0].x = 0;
		addPlant();
		printf("%d",top);
		for(i=1 ; i<=top ;i++)
			printf(" %d",que[i].index);
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值