fzu Problem 2148 Moon Game(几何 凸四多边形 叉积)

时间:2023-03-09 13:06:07
fzu Problem 2148 Moon Game(几何 凸四多边形 叉积)

题目:http://acm.fzu.edu.cn/problem.php?pid=2148

题意:给出n个点,判断可以组成多少个凸四边形。

思路:

因为n很小,所以直接暴力,判断是否为凸四边形的方法是:

如果4个点中存在某个点D,Sabd + Sacd + Sbcd = Sabc,则说明是凹四边形。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-; //定义成double类型 struct point
{
int x, y;
} p[];
double area(point a, point b, point c)
{
return (fabs((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x))/); //一定要fabs(),有可能为负的,
}
bool ok(point a, point b, point c, point d)
{
if(fabs(area(b,c,d)-area(a,b,c)-area(a,c,d)-area(a,b,d)) < eps)
return false;
return true;
}
int main()
{
int t, i, j, n, k, ans, a, b;
scanf("%d", &t);
for(k = ; k <= t; k++)
{
ans = ;
scanf("%d", &n);
for(i = ; i < n; i++)
scanf("%d%d", &p[i].x, &p[i].y); if(n<)
printf("Case %d: %d", k, ans);
else
{
for(i = ; i < n; i++)
for(j = i+; j < n; j++)
for(a = j+; a < n; a++)
for(b = a+; b < n; b++)
if(ok(p[i],p[j],p[a],p[b])&&ok(p[j],p[i],p[a],p[b])
&&ok(p[a],p[i],p[j],p[b])&&ok(p[b],p[i],p[j],p[a]))
{
ans++;
}
printf("Case %d: %d\n", k, ans);
}
}
return ;
}