zoukankan      html  css  js  c++  java
  • HDU6055 Regular polygon(计算几何)

    Description

    On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make.
     

    Input

    The input file consists of several test cases. Each case the first line is a numbers N (N <= 500). The next N lines ,each line contain two number Xi and Yi(-100 <= xi,yi <= 100), means the points’ position.(the data assures no two points share the same position.)
     

    Output

    For each case, output a number means how many different regular polygon these points can make.
     
    Sample
    Sample Input
    4
    0 0
    0 1
    1 0
    1 1
    6
    0 0
    0 1
    1 0
    1 1
    2 0
    2 1 
     
    Sample Output
    1
    2 

    题意:

      给定n个整数点,问能组成多少正n边形。

    思路:

      因为给出的是整数点,所以只可能是正方形

      可以枚举正方形的对角线,因为有两条对角线,最后答案要/2

      也可以枚举正方形的边,因为有四条边,答案要/4

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    
    #define MAX 2000
    
    using namespace std;
    
    struct node
    {
        int x,y;
    } p[MAX];
    
    int vis[MAX][MAX];
    
    int solve(int node1,int node2)
    {
        int x = p[node1].x - p[node2].x;
        int y = p[node1].y - p[node2].y;
        int ans=0;
        if(p[node1].x-y>=0 && p[node1].y+x>=0 && p[node2].x-y>=0 && p[node2].y+x>=0 )//判断成正方形的条件
            if(vis[p[node1].x-y][p[node1].y+x] && vis[p[node2].x-y][p[node2].y+x])//判断两个点是否存在
                ans++;
        if(p[node1].x+y>=0 && p[node1].y-x>=0 && p[node2].x+y>=0 && p[node2].y-x>=0 )
            if( vis[p[node1].x+y][p[node1].y-x] && vis[p[node2].x+y][p[node2].y-x])
                ans++;
        return ans;
    }
    
    int main()
    {
        int n,x,y;
        while(scanf("%d",&n)!=EOF)
        {
            int ans = 0;
            memset(vis,0,sizeof(vis));
            //vis数组用来查看是否存在,结构体p用来存所有的点
            for(int i=0; i<n; i++)
            {
                scanf("%d%d",&x,&y);
                vis[x+200][y+200] = 1;//注意+200是为了将负数化为正数
                p[i].x = x+200;
                p[i].y = y+200;
            }
            //枚举所有的点
            for(int i=0; i<n; i++)
            {
                for(int j=i+1; j<n; j++)
                {
                    ans += solve(i,j);
                }
            }
    
            ans /= 4;//因为枚举的是边长,所以最后除以4
    
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    无线渗透开启WPS功能的路由器
    写代码怎能不会这些Linux命令?
    分布式服务框架 Zookeeper -- 管理分布式环境中的数据
    每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)
    Innodb 中的事务隔离级别和锁的关系
    线上操作与线上问题排查实战
    MySQL 四种事务隔离级的说明
    一次由于 MTU 设置不当导致的网络访问超时
    SYN 和 RTO
    The story of one latency spike
  • 原文地址:https://www.cnblogs.com/aiguona/p/7262039.html
Copyright © 2011-2022 走看看