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
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.
2 4 0 0 100 0 0 100 100 100 4 0 0 100 0 0 100 10 10Sample Output
Case 1: 1 Case 2: 0
题意:第一行给出T,接下来有T组数据,每组数据第一行给出N,接下来N行,每行输入一个点的x,y坐标。输出这些点一共可以构成多少凸四边形。
思路首先枚举四个点,然后任意选出两对点相连(四个点,共有3中可能),看两条直线是否是对角线(一条直线两点在另一条直线的两侧),只要三种情况中有一种可以出现对角线即可,利用叉积判断
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
struct node{
double x,y;
}p[40];
bool cmp1(node a,node b){
if(a.y == a.x) return a.x < b.x;
return a.y < b.y;
}
bool iscross(node a,node b,node c,node d){
double ans1 = (c.x - a.x) * (b.y - a.y) - (c.y - a.y) * (b.x - a.x);
double ans2 = (d.x - a.x) * (b.y - a.y) - (d.y - a.y) * (b.x - a.x);
double ans3 = (a.x - d.x) * (c.y - d.y) - (a.y - d.y) * (c.x - d.x);
double ans4 = (b.x - d.x) * (c.y - d.y) - (b.y - d.y) * (c.x - d.x);
if(ans1 * ans2 < 0 && ans3 * ans4 < 0) return true;//判断cd两点在直线ab两侧,同时ab两点在直线cd两侧
else return false;
}
bool judge(node a,node b,node c,node d){
if(iscross(a,b,c,d) || iscross(a,c,b,d) || iscross(a,d,b,c))//ab和cd,ac和bd,ad和bc判断是否相交
return true;
else return false;
}
int main(){
int t,cas = 0;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmp1);
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 q = k+1; q < n; q++){
if(judge(p[i],p[j],p[k],p[q]))
ans++;
}
}
}
}
printf("Case %d: %d\n",++cas,ans);
}
return 0;
}
本文介绍了一种算法,用于解决在给定点集中寻找不同凸四边形的问题。通过枚举点集中的四个点并检查它们是否能形成凸四边形来实现。使用了交叉判断和叉积的方法来确定是否存在对角线。
2958

被折叠的 条评论
为什么被折叠?



