FZU 2148 Moon Game(线性代数,几何)

本文介绍了一种算法,用于计算给定点集可以构成的不同凹四边形的数量。通过判断四点是否能形成凹四边形,并利用三角形面积计算公式进行验证。

Fat brother and Maze are playing a kind of special (hentai) game in the clearly blue sky which we can just consider as a kind of two-dimensional plane. Then Fat brother starts to draw N starts in the sky which we can just consider each as a point. After he draws these stars, he starts to sing the famous song “The Moon Represents My Heart” to Maze.

You ask me how deeply I love you,

How much I love you?

My heart is true,

My love is true,

The moon represents my heart.

But as Fat brother is a little bit stay-adorable(呆萌), he just consider that the moon is a special kind of convex quadrilateral and starts to count the number of different convex quadrilateral in the sky. As this number is quiet large, he asks for your help.

Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains an integer N describe the number of the points.

Then N lines follow, Each line contains two integers describe the coordinate of the point, you can assume that no two points lie in a same coordinate and no three points lie in a same line. The coordinate of the point is in the range[-10086,10086].

1 <= T <=100, 1 <= N <= 30

Output

For each case, output the case number first, and then output the number of different convex quadrilateral in the sky. Two convex quadrilaterals are considered different if they lie in the different position in the sky.

Sample Input
2
4
0 0
100 0
0 100
100 100
4
0 0
100 0
0 100
10 10
Sample Output
Case 1: 1
Case 2: 0
 又是一道数学题→ →。。可怕
题目大意:给出n个点,求这n个点能构成的凹四边形个数,任意三点不共线。
假设点a是凹进去的点,如果四个点a,b,c,d构成凹四边形,那么点a与任意两点的面积和与其余三点构成的三角形面积相同,否则不是凹四边形。
还有就是通过三个点的坐标来求三角形的面积,用线性代数的行列式来求解。
公式是我百度的。。→ →
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
设A(x1,y1),B(x2,y2),C( x3,y3)
由A-->B-->C-->A 按逆时针方向转。(行列式书写要求)
设三角形的面积为S
则S=(1/2)*(下面行列式)
|x1 y1 1|
|x2 y2 1|
| x3 y3 1|
S=(1/2)*(x1y2*1+x2y3*1+ x3y1*1-x1y3*1-x2y1*1-x3y2*1)
即用三角形的三个顶点坐标求其面积的公式为:
S=(1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
要好好学高数了哈哈哈哈哈~
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct node
{
    int x,y;
}a[1234];
int flag;
double SS(node a,node b,node c)
{
    double s=a.x*b.y+b.x*c.y+c.x*a.y-a.x*c.y-c.x*b.y-b.x*a.y;//通过三点的坐标计算三角形面积
    return s;
}
int OK(node a,node b,node c,node d)
{
    double s1=fabs(SS(a,b,c));
    double s2=fabs(SS(a,b,d))+fabs(SS(a,c,d))+fabs(SS(b,c,d));
    if(s1==s2) return 1;
    else return 0;
}
int Judge(node a,node b,node c,node d)
{
    if(OK(a,b,c,d)) return 0;
    if(OK(a,b,d,c)) return 0;
    if(OK(a,d,c,b)) return 0;
    if(OK(d,c,b,a)) return 0;
    return 1;
}
int main()
{
    int t;
    scanf("%d",&t);
    int count=0;
    while(t--)
    {
        count++;
        int n;
        scanf("%d",&n);
        int i,j,k,l;
        for(i=0;i<=n-1;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        int ans=0;
        flag=0;
        for(i=0;i<=n-1;i++)
        {
            for(j=i+1;j<=n-1;j++)
            {
                for(k=j+1;k<=n-1;k++)
                {
                    for(l=k+1;l<=n-1;l++)
                    {
                        if(Judge(a[i],a[j],a[k],a[l]))
                           ans++;
                    }
                }
            }
        }
        printf("Case %d: %d\n",count,ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值