zoukankan      html  css  js  c++  java
  • hust 1607Triangles

    Description

    You are given a figure consisting of n points in a 2D-plane and m segments connecting some of them. We guarantee that any two segments don't share points except their ends and there's no more than one segment between the same pair of points. Please count the total number of triangles in the given figure.

    Input

    There're multiple test cases. In each case:
    The first line contains two positive integers n and m. (n <= 200, m <= 20000)
    Each of the following n lines contains two real numbers xi and yi  indicating the coordinates of the ith point. (-100000 < xiyi < 100000)
    Each of the following m lines contains four real numbers xiyixj , yj . It means (xiyi) and (xj , yj) are connected by a segment. We guarantee that these points are part of the given n points.

    Output

    For each test case, print a single line contains the total number of triangles in the given figure. 
    Please see sample for more details

    Sample Input

    4 5
    0 0
    1 1
    2 0
    1 0
    0 0 1 1
    1 1 2 0
    2 0 1 0
    1 0 0 0
    1 1 1 0
    

    Sample Output

    3

    给定n个点以及他们的连接情况,问能构成多少三角形=。=做的时候感觉应该可以用类似floyd的方法来判断连接情况,但是每个点的坐标范围
    都这么大怎么办呢。此时想到可以用hash的方法来记录,因为每个点的范围到100000,所以只要讲一个点向前移这么多位即可,我这里用了map来
    记录
    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <map>
     4 #include <cmath>
     5 #include <algorithm>
     6 using namespace std;
     7 #define eps 1e-8
     8 struct eg
     9 {
    10     double x,y;
    11 }p[220],t,q;
    12 map<double,int>mp;
    13 int link[220][220];
    14 double hash(eg a)
    15 {
    16     return a.x*100000+a.y;
    17 }
    18 double dis(eg a,eg b)
    19 {
    20     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    21 }
    22 int ok(eg a,eg b,eg c)
    23 {
    24     double x,y,z,t;
    25     x=a.x-c.x,y=a.y-c.y;
    26     z=b.x-c.x,t=b.y-c.y;
    27     if(fabs(x*t-y*z)<eps) return 1;
    28     return 0;
    29 }
    30 int triange(eg a,eg b,eg c)//判断三角形
    31 {
    32     double x,y,z;
    33     x=dis(a,b);
    34     y=dis(a,c);
    35     z=dis(b,c);
    36     if(x+y-z>eps&&x+z-y>eps&&y+z-x>eps)
    37         return 1;
    38     else return 0;
    39 }
    40 int judge(int i,int j,int k)
    41 {
    42     if(link[i][j]&&link[j][k]&&link[i][k])
    43         return triange(p[i],p[j],p[k]);
    44     return 0;
    45 }
    46 int main()
    47 {
    48     int n,m,i,j,k;
    49     int x,y;
    50     while(scanf("%d%d",&n,&m)!=EOF){
    51         mp.clear();
    52         memset(link,0,sizeof(link));
    53         for(i=0;i<n;i++){
    54             scanf("%lf%lf",&p[i].x,&p[i].y);
    55             mp[hash(p[i])]=i;//点和坐标对应
    56         }
    57         for(i=0;i<m;i++){
    58             scanf("%lf%lf%lf%lf",&q.x,&q.y,&t.x,&t.y);
    59             x=mp[hash(q)],y=mp[hash(t)];
    60             link[y][x]=link[x][y]=1;
    61         }
    62         for(i=0;i<n;i++){
    63             for(j=0;j<n;j++){
    64                 if(i!=j){
    65                     for(k=0;k<n;k++){
    66                         if(i!=j&&j!=k&&k!=i){
    67                             if(link[i][k]&&link[i][j]&&!link[j][k]
    68                             &&ok(p[i],p[j],p[k]))
    69                                 link[j][k]=link[k][j]=1;
    70                         }
    71                     }
    72                 }
    73             }
    74         }
    75         int cnt=0;
    76         for(i=0;i<n;i++)
    77             for(j=i+1;j<n;j++)
    78                 for(k=j+1;k<n;k++)
    79                     cnt+=judge(i,j,k);
    80         printf("%d\n",cnt);
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    Linux(Ubuntu)总结(三)
    Linux(Ubuntu):搭建Docker
    JavaScript数字格式化方法:千分位,百分数
    解决mysqld --install:Install/Remove of the Service Denied!
    【转】Oracle基础结构认知——oracle内存结构 礼记八目 2017-12-15 20:31:27
    【转】Oracle基础结构认知—初识oracle 礼记八目 2017-12-12 21:19:30
    Linux 常用命令
    oracle AWR详解
    VMware 虚拟化技术 创建虚拟机
    MySQL数据库各个版本的区别
  • 原文地址:https://www.cnblogs.com/wilsonjuxta/p/2963809.html
Copyright © 2011-2022 走看看