FZU-2148-Moon Game

I - Moon Game
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

FZU 2148
Description
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

题意:第一行给出T,接下来有T组数据,每组数据第一行给出N,接下来N行,每行输入一个点的x,y坐标。输出这些点一共可以构成多少凸四边形。

思路:凸包不好判断,但是凹包很好判断啊,假设如果四个点a,b,c,d,构成了凹包,且a点凹了进去,那a和另外三个点组成的三角形面积和一定等于b,c,d三个点构成的三角形面积。自己画个图就显而易见。
一个获取三角形面积的函数,加上一个判断四个三角形面积关系的函数,然后四层循环暴力枚举出所有可能。
思路很好想,就是细节处理上,要用fabs获取浮点数的绝对值,要用小于1e-6这种方式近似的判断作差结果是否为0,因为浮点数运算一般不会直接判定是否相等,因为涉及到精度,不准确,所以都是近似的判断是否相等。

代码

#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<stdio.h>
#include<queue>
using namespace std;
//寻找凸包数量,暴力枚举
const double inf=1e-6;
struct node
{
    int x;
    int y;
} num[35];
double area(node a,node b,node c)//返回面积
{
    return fabs(1.0*((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x)))/2.0;
}
bool is_ok(node a,node b,node c,node d)
{
    if(fabs(area(a,b,c)-area(a,b,d)-area(b,c,d)-area(a,c, d))<inf)
        return false;//不是凸包就返回假
    return true;//凸包返回真
}
int main()
{
    int T;
    scanf("%d",&T);
    int count=1;
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            cin>>num[i].x>>num[i].y;
        int sum=0;//初始化凸包数量
        for(int i=0; i<n; i++)
            for(int j=i+1; j<n; j++)
                for(int k=j+1; k<n; k++)
                    for(int p=k+1; p<n; p++)
                        if(is_ok(num[i],num[j],num[k],num[p])&&is_ok(num[i],num[p],num[k],num[j])&&is_ok(num[i],num[j],num[p],num[k])&&is_ok(num[p],num[j],num[k],num[i]))
                            sum++;
        printf("Case %d: ",count++);
        printf("%d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值