数论 - 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
-----------------------------------------------------我是分割线^_^--------------------------------------------------------
题目意思就是说问你在给出的点中最多可以组成多少个凸四边形,此题没做出来,数学底子还是太差了,
直接暴力使用四重循环枚举判断即可,其中对于四边形的判断可以使用面积进行判断,具体图如下:
取四个点,如果有四边形为凹四边形,则必有一个点在其他三个点所形成的三角形内部,所以凸四边形
的问题也是类似的,然后就是求面积了,求面积可以使用向量的叉乘,因为向量a*向量b=|a|*|b|*sin<a,b>,
面积就出来了,于是便进行判断,注意转为浮点数,进行误差处理。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
#define lson rt<<1
#define rson rt<<1|1
#define INF 0x3f3f3f3f
#define Int __int64
typedef pair<int,int> pii;
const int MAXN = 55;
pii pos[MAXN];
double Area(pii p1, pii p2, pii p3) {
return fabs(1.0 * (p1.first - p2.first) * (p1.second - p3.second) - (p1.second - p2.second) * (p1.first - p3.first)) / 2.0;
}
bool Ok(pii p1, pii p2, pii p3, pii p4) {
if (fabs(Area(p1, p2, p3) - Area(p1, p2, p4) - Area(p1, p3, p4) - Area(p2, p3, p4)) < 1e-8) return false;
else return true;
}
int main() {
//freopen("input.txt", "r", stdin);
int cas;
while (cin>>cas) {
int sign = 1;
while (cas--) {
printf("Case %d: ", sign++);
int n;
cin>>n;
for (int i = 0; i < n; i++) {
cin>>pos[i].first>>pos[i].second;
}
int ans = 0;
for (int i1 = 0; i1 < n; i1++) {
for (int i2 = i1 + 1; i2 < n; i2++) {
for (int i3 = i2 + 1; i3 < n; i3++) {
for (int i4 = i3 + 1; i4 < n; i4++) {
if (Ok(pos[i1], pos[i2], pos[i3], pos[i4]) &&
Ok(pos[i1], pos[i2], pos[i4], pos[i3]) &&
Ok(pos[i1], pos[i4], pos[i3], pos[i2]) &&
Ok(pos[i4], pos[i2], pos[i3], pos[i1])) {
ans++;
}
}
}
}
}
printf("%d\n", ans);
}
}
return 0;
}