Uva 10112 【简单的计算几何】.cpp

题意:

  给出n个点的x坐标和y坐标,让你求出由其中三个点组成的三角形,要求这个三角形里面不包含别的点且面积最小。

思路:

  三角形里面不包含别的点的判断方法是最暴力的一层for循环用面积法看看除去组成这个三角形以外的别的点是否在这个三角形内。

Tips:

  这题的关键其实只是掌握面积法。即利用差积的方法求面积。

Code:

View Code
 1 #include <stdio.h>

 2 #include <cstring>

 3 #include <cmath>

 4 #include <algorithm>

 5 #include <ctime>

 6 using namespace std;

 7 #define eps 1e-8

 8 

 9 struct Point

10 {

11     char num;

12     double x;

13     double y;

14 }po[20];

15 

16 double xmul(Point a, Point b, Point c) {

17     return (c.y-a.y)*(b.x-a.x)-(b.y-a.y)*(c.x-a.x);

18 }

19 

20 double area(Point a, Point b, Point c)

21 {

22     return fabs(0.5*xmul(a, b, c));

23 }

24 

25 bool isin(Point a, Point b, Point c, Point p) {

26     return fabs(area(a, b, p)+area(a, c, p)+area(b, c, p)-area(a, b, c)) < eps;

27 }

28 

29 int main()

30 {

31     Point a, b, c;

32     bool flag;

33     int n;

34     double tar;

35     while(EOF != scanf("%d", &n)) {

36         if(n == 0) break;

37         tar = -1;

38         for(int i = 0; i < n; ++i) {

39             scanf("%*c%c %lf %lf", &po[i].num, &po[i].x, &po[i].y);

40          //   printf("%c %lf %lf\n", po[i].num, po[i].x, po[i].y);

41         }

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

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

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

45                     flag = true;

46                     for(int t = 0; t < n; ++t)

47                         if(t != i && t != j && t != k && isin(po[i], po[j], po[k], po[t])) {

48                             flag = false;

49                             break;

50                         }

51                     if(flag && fabs(area(po[i], po[j], po[k]) > tar+eps)) {

52                         a = po[i], b = po[j], c = po[k];

53                         tar = area(po[i], po[j], po[k]);

54                     }

55                 }

56         printf("%c%c%c\n", a.num, b.num, c.num);

57     }

58     return 0;

59 }

 

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=101&page=show_problem&problem=1053

你可能感兴趣的:(cpp)