题目大意:
给出n个点,判断可以组成多少个凸四边形。
解题思路:
因为n很小,所以直接暴力,判断是否为凸四边形的方法是:
如果4个点中存在某个点D,Sabd + Sacd + Sbcd = Sabc,则说明是凹四边形。
给出n个点,判断可以组成多少个凸四边形。
解题思路:
因为n很小,所以直接暴力,判断是否为凸四边形的方法是:
如果4个点中存在某个点D,Sabd + Sacd + Sbcd = Sabc,则说明是凹四边形。
注意这里有个坑点,因为枚举出的每个点,都有可能为内心,所以4种情况都要判断。
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 35;
struct Point {
double x,y;
}p[N];
double area(Point a,Point b,Point c) {
return fabs(a.x*b.y + c.x*a.y + b.x*c.y - c.x*b.y - a.x*c.y - b.x*a.y);
}
bool judge(Point a,Point b,Point c,Point o) {
double s[4];
s[0] = area(a,b,c);
s[1] = area(o,a,b);
s[2] = area(o,b,c);
s[3] = area(o,c,a);
if(fabs(s[0] - s[1] - s[2] - s[3]) < 1e-9
||fabs(s[1] - s[0] - s[2] - s[3]) < 1e-9
||fabs(s[2] - s[0] - s[1] - s[3]) < 1e-9
||fabs(s[3] - s[0] - s[1] - s[2]) < 1e-9) {
return false;
}
return true;
}
int main() {
int t,n,cas = 1;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
for(int i = 0; i < n; i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
}
int cnt = 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 l = k+1; l < n; l++) {
if(judge(p[i],p[j],p[k],p[l]) ) {
cnt++;
}
}
}
}
}
printf("Case %d: %d\n",cas++,cnt);
}
return 0;
}