ACM学习历程—FZU2148 Moon Game(计算几何)

Moon Game

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


这个题目是一道计算凸四边形个数的题目,只需要暴力枚举所有4边形判断即可。
判断的时候就是枚举对角线,只有有一种情况的两条对角线相交即为凸四边形。这里采用线段相交的模板来判断。


代码:
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstdlib>

 4 #include <cstring>

 5 #include <cmath>

 6 #include <algorithm>

 7 #include <set>

 8 #include <map>

 9 #include <queue>

10 #include <string>

11 #include <vector>

12 #define inf 0x3fffffff

13 

14 using namespace std;

15 

16 struct point

17 {

18     int x, y;

19 }p[35];

20 

21 point a1, a2, a3, a4;

22 

23 bool inter(point a, point b, point c, point d)

24 {

25     if(

26        min(a.x, b.x) > max(c.x, d.x) ||

27        min(a.y, b.y) > max(c.y, d.y) ||

28        min(c.x, d.x) > max(a.x, b.x) ||

29        min(c.y, d.y) > max(a.y, b.y)

30        )

31         return 0;

32     long long x1, x2, x3, x4;

33     x1 = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);

34     x2 = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);

35     x3 = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);

36     x4 = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);

37     return x1 * x2 < 0 && x3 * x4 < 0;

38 }

39 

40 bool fun()

41 {

42     if (inter(a1, a2, a3, a4)) return 1;

43     if (inter(a1, a3, a2, a4)) return 1;

44     if (inter(a1, a4, a2, a3)) return 1;

45     return 0;

46 }

47 

48 int main()

49 {

50     //freopen ("test.txt", "r", stdin);

51     int T;

52     scanf ("%d", &T);

53     for (int times = 1; times <= T; ++times)

54     {

55         int n;

56         scanf ("%d", &n);

57         for (int i = 0; i < n; ++i)

58         {

59             scanf ("%d%d", &p[i].x, &p[i].y);

60         }

61         int ans = 0;

62         for (int i = 0; i < n; ++i)

63         {

64             a1 = p[i];

65             for (int j = i + 1; j < n; ++j)

66             {

67                 a2 = p[j];

68                 for (int k = j + 1; k < n; ++k)

69                 {

70                     a3 = p[k];

71                     for (int h = k + 1; h < n; ++h)

72                     {

73                         a4 = p[h];

74                         if (fun()) ans++;

75                     }

76                 }

77             }

78         }

79         printf ("Case %d: %d\n", times, ans);

80     }

81     return 0;

82 }
View Code

 

你可能感兴趣的:(game)