您的位置:首页 > 其它

hdu 4709 叉乘计算

2013-10-05 20:37 288 查看

Herding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1052    Accepted Submission(s): 303


[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

1
4
-1.00 0.00
0.00 -3.00
2.00 0.00
2.00 2.00

 

[align=left]Sample Output[/align]

2.00

 

[align=left]Source[/align]
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 ,  则P 在Q的顺时针方向
 若 P × Q  < 0 ,  则P 在Q的逆时针方向
 若 P × Q  = 0 ,  则P 与Q共线,但可能同向也可能反向

我提出的算法中有一步要判断顺时针逆时针的方向,其实很简单。假设有两端连续的险段,其公共端点是P0,其他的两个端点是P1和P1,计算向量<P0,P1>和<P0,P2>的叉乘,如果若结果为正,则<P0,P1>在<P0,P2>的顺时针方向;若为0则<P0,P1><P0,P2>共线(可能同向和反向);若为负则<P0,P1>在<P0,P2>的在逆时针方向。因此可以根据这个判断p0p1和p1p2在p1处是左转还是右转,只要求(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  叉乘