Moon Game
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
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组数据,每组n个点,n小于等于30,问从这些点中任取四个点构成的四边形有多少个是凸四边形。
题解:暴力跑,用三角形有向面积的绝对值判断是否为凹多边形,注意优化即可。
开始用ijkl都是从0开始跑的叉积判断,TLE了,这道题用面积判断更好。
#include <iostream> #include <math.h> #include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <vector> const double PI=acos(-1.0); using namespace std; struct Point{ double x,y; int id; Point(double x=0,double y=0,int id=-1):x(x),y(y){} //构造函数,方便代码编写 }; typedef Point Vector; //从程序上实现,Vector只是Point的别名 Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y); } //点-点=向量 Vector operator - (Point A,Point B) { return Vector(A.x-B.x,A.y-B.y); } //向量*数=向量 Vector operator * (Vector A,double p) { return Vector(A.x*p,A.y*p); } //向量/数=向量 Vector operator / (Vector A,double p) { return Vector(A.x/p,A.y/p); } //运算符重载 bool operator <(const Point &a,const Point &b) { return a.x<b.x||(a.x==b.x&&a.y<b.y); } const double eps=1e-10; //三态函数精度问题 int dcmp(double x) { if(fabs(x)<eps) return 0; else return x<0?-1:1; } bool operator ==(const Point &a,const Point &b) { return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; } //叉积 double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; } double Area(Point A,Point B,Point C) { return Cross(B-A,C-A); } int main() { int t,cas=1; scanf("%d",&t); while(t--) { int n; Point p[30]; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); int ans=0; for(int i=0;i<n-3;i++) for(int j=i+1;j<n;j++) for(int k=j+1;k<n;k++) for(int l=k+1;l<n;l++) { double a=fabs(Area(p[i],p[j],p[k])); //有向面积 double b=fabs(Area(p[j],p[k],p[l])); double c=fabs(Area(p[k],p[l],p[i])); double d=fabs(Area(p[l],p[i],p[j])); double m=max(max(a,b),max(c,d)); double sum=a+b+c+d-m; if(sum==m) continue; ans++; } printf("Case %d: %d\n",cas++,ans); } return 0; }