Problem 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
解题思路
1、判断对角线是否相交,不想交则为凹四边形,如图1,相交为凸四边形,如图二。
2、判断面积判断,如果三个小三角形面积等于大三角形,则为凹四边形,如图1,S△ABC = S△ABD + S△BCD + S△ACD,那么四边形ABCD为凹四边形。
题解代码
1、根据对角线判断
#include <iostream>
#include <cstdio>
using namespace std;
struct point
{
int x, y;
}p[35];
point a1, a2, a3, a4;
bool inter(point a, point b, point c, point d)
{
if(
min(a.x, b.x) > max(c.x, d.x) || //线段线段是否在直线两侧
min(a.y, b.y) > max(c.y, d.y) ||
min(c.x, d.x) > max(a.x, b.x) ||
min(c.y, d.y) > max(a.y, b.y)
)
return 0;
long long x1, x2, x3, x4;
x1 = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
x2 = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
x3 = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
x4 = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
return x1 * x2 < 0 && x3 * x4 < 0;
}
bool fun()
{
if (inter(a1, a2, a3, a4)) return 1;
if (inter(a1, a3, a2, a4)) return 1;
if (inter(a1, a4, a2, a3)) return 1;
return 0;
}
int main()
{
int T;
scanf ("%d", &T);
for (int times = 1; times <= T; ++times)
{
int n;
scanf ("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf ("%d%d", &p[i].x, &p[i].y);
}
int ans = 0;
for (int i = 0; i < n; ++i)
{
a1 = p[i];
for (int j = i + 1; j < n; ++j)
{
a2 = p[j];
for (int k = j + 1; k < n; ++k)
{
a3 = p[k];
for (int h = k + 1; h < n; ++h)
{
a4 = p[h];
if (fun()) ans++;
}
}
}
}
printf ("Case %d: %d\n", times, ans);
}
return 0;
}
2、根据面积判断
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
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;
}