Accept: 806 Submit: 2452
Time Limit: 1000 mSec Memory Limit : 32768 KB
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
Sample Output
这个题目是一道计算凸四边形个数的题目,只需要暴力枚举所有4边形判断即可。
判断的时候就是枚举对角线,只有有一种情况的两条对角线相交即为凸四边形。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 50;
struct Point {
int x, y;
Point(int _x = 0, int _y = 0) {
x = _x; y = _y;
}
Point operator-(const Point& p) const {
return Point(x - p.x, y - p.y);
}
//重载叉乘,返回叉乘的模
int operator*(const Point& p) const {
return x * p.y - y * p.x;
}
} point[MAXN];
int t, n;
//快速排斥实验
bool QuiEx(const Point& a, const Point& b,
const Point& c, const Point& d) {
return min(a.x, b.x) <= max(c.x, d.x) &&
min(c.x, d.x) <= max(a.x, b.x) &&
min(a.y, b.y) <= max(c.y, d.y) &&
min(c.y, d.y) <= max(a.y, b.y);
}
//线段相交
bool Cross(const Point& a, const Point& b,
const Point& c, const Point& d) {
return QuiEx(a, b, c, d) &&
((c - d)*(c - a))*((c - d)*(c - b)) < 0 && //此为跨立实验
((a - b)*(a - c))*((a - b)*(a - d)) < 0;
}
bool Fun(const Point& a, const Point& b,
const Point& c, const Point& d) {
return (Cross(a, b, c, d) || Cross(a, c, b, d) || Cross(a, d, b, c));
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int ca = 0;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &point[i].x, &point[i].y);
}
int ans = 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 h = k + 1; h < n; ++h) {
if (Fun(point[i], point[j], point[k], point[h])) {
++ans;
}
}
}
}
}
printf("Case %d: %d\n", ++ca, ans);
}
return 0;
}