hdu 4709 叉乘计算

10 篇文章 0 订阅

Herding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1052    Accepted Submission(s): 303


Problem Description
Little John is herding his father's cattles. As a lazy boy, he cannot tolerate chasing the cattles all the time to avoid unnecessary omission. Luckily, he notice that there were N trees in the meadow numbered from 1 to N, and calculated their cartesian coordinates (Xi, Yi). To herding his cattles safely, the easiest way is to connect some of the trees (with different numbers, of course) with fences, and the close region they formed would be herding area. Little John wants the area of this region to be as small as possible, and it could not be zero, of course.
 

Input
The first line contains the number of test cases T( T<=25 ). Following lines are the scenarios of each test case.
The first line of each test case contains one integer N( 1<=N<=100 ). The following N lines describe the coordinates of the trees. Each of these lines will contain two float numbers Xi and Yi( -1000<=Xi, Yi<=1000 ) representing the coordinates of the corresponding tree. The coordinates of the trees will not coincide with each other.
 

Output
For each test case, please output one number rounded to 2 digits after the decimal point representing the area of the smallest region. Or output "Impossible"(without quotations), if it do not exists such a region.
 

Sample Input
  
  
1 4 -1.00 0.00 0.00 -3.00 2.00 0.00 2.00 2.00
 

Sample Output
  
  
2.00
 

Source
2013 ACM/ICPC Asia Regional Online —— Warmup

第一次做这种叉乘的题,记下了,知道点坐标以后遇到判断点在多边形内部,点在直线的上下,或者点是否在某个区域
都可以用到,觉得很方便,三角形的面积要用到向量的叉乘以及矩阵的模

设矢量P = x1,y1 Q = (x2,y2)
则矢量叉积定义为:  P × Q = x1*y2 – x2*y1   得到的是一个标量
显然有性质 P × Q = – ( Q × P )   P × ( – Q ) = – ( P × Q )
如不加说明,下面所有的点都看作矢量,点的乘法看作矢量叉积;
叉乘的重要性质:
  P × Q  > 0 ,  Q的顺时针方向
  P × Q  < 0 ,  Q的逆时针方向
  P × Q  = 0 ,  Q共线,但可能同向也可能反向

我提出的算法中有一步要判断顺时针逆时针的方向,其实很简单。假设有两端连续的险段,其公共端点是P0,其他的两个端点是P1P1,计算向量<P0,P1><P0,P2>的叉乘,如果若结果为正,则<P0,P1><P0,P2>的顺时针方向;若为0<P0,P1><P0,P2>共线(可能同向和反向);若为负则<P0,P1><P0,P2>的在逆时针方向。因此可以根据这个判断p0p1p1p2p1处是左转还是右转,只要求(p2-p0)*(p1-p0),若<0则左转,>0则右转,=0则共线。

、、三点p0,p2,p1:p0p2在p0p1的顺时针方向叉积为正否则为负,为负就大拇指向外为正就向内

叉积x1*Y2--Y1*x2

#include<iostream>
#include<math.h>
//#define eps 0.0000000005//这里用过宏定义以后就会出现runtime error
using namespace std;
typedef struct 
{
	double x;
	double y;
}zuobiao;
int main()
{
	int T,N;
	double s,area;
	cin>>T;
	zuobiao tree[100+5];
	while(T--)
	{
		s=100000000000;
		int count1=1;
		cin>>N;
		
		if(N<3)
		{
				for(int i=0;i<N;i++)
			{
				cin>>tree[i].x>>tree[i].y;
			}
			cout<<"Impossible"<<endl;
		
		}
		else
		{
			for(int i=0;i<N;i++)
			{
				cin>>tree[i].x>>tree[i].y;
			}
			for(i=1;i<N-1;i++)
			{
				//这里判断了斜率在这里主要是判断是不是共线,最主要的方法就是斜率,最好不要
				//用除法会增加分母是0的判断,这里也可以进行叉乘,

			
					if((tree[i].y-tree[i-1].y)*(tree[i+1].x-tree[i-1].x) != 
						(tree[i+1].y-tree[i-1].y)*(tree[i].x-tree[i-1].x))
					{
						
						break;
					}	count1++;		
			
			}
			
			if(N==(count1+1))
			{
				cout<<"Impossible"<<endl;
				continue;
			}

			for( i=0;i<N-2;i++)
				for(int j=i+1;j<N-1;j++)
					for(int k=j+1;k<N;k++)
					{
						//用叉乘法计算了三角形的面积
						area=0.5*fabs(tree[i].x*tree[j].y+tree[i].y*tree[k].x+tree[j].x*tree[k].
							y-tree[k].x*tree[j].y-tree[i].y*tree[j].x-tree[i].x*tree[k].y+0.0);
						if(s>area && area>0)
							s=area;
					}
						printf("%.2f\n",s);
		}
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值