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;
    }
  • 相关阅读:
    Leetcode 1002. 查找常用字符
    Leetcode 1020. 将数组分成和相等的三个部分
    Leetcode 1021. 最佳观光组合
    Leetcode 1022. 可被 K 整除的最小整数
    算法入门经典第六章 例题6-9 天平
    例题6-7 树的层次遍历
    算法入门经典第六章 例题6-4 破损的键盘
    算法入门经典-第五章 例题5-7 丑数
    算法入门经典第六章 例题6-5 移动盒子
    算法入门经典第六章 例题6-2 铁轨
  • 原文地址:https://www.cnblogs.com/aiguona/p/7262039.html
Copyright © 2011-2022 走看看