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;
    }
  • 相关阅读:
    JS中prototype属性解释及常用方法
    HTML5 组件Canvas实现图像灰度化
    洛谷.5284.[十二省联考2019]字符串问题(后缀自动机 拓扑 DP)
    洛谷.5290.[十二省联考2019]春节十二响(贪心)
    洛谷.5283.[十二省联考2019]异或粽子(可持久化Trie 堆)
    SDOI2019 省选前模板整理
    完美理论(最大权闭合子图)
    BZOJ.3566.[SHOI2014]概率充电器(概率DP 树形DP)
    BZOJ.2616.SPOJ PERIODNI(笛卡尔树 树形DP)
    4.2模拟赛 wormhole(期望DP Dijkstra)
  • 原文地址:https://www.cnblogs.com/aiguona/p/7262039.html
Copyright © 2011-2022 走看看