hdu 5365 计算几何 给几个点判断是否为正方形

本文介绍了一道算法题的解决方法,题目要求找出所有可能的正多边形路径组合,通过分析得出只需计算正方形的数量。文章提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




Run

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
AFA is a girl who like runing.Today,he download an app about runing .The app can record the trace of her runing.AFA will start runing in the park.There are many chairs in the park,and AFA will start his runing in a chair and end in this chair.Between two chairs,she running in a line.she want the the trace can be a regular triangle or a square or a regular pentagon or a regular hexagon.
Please tell her how many ways can her find.
Two ways are same if the set of chair that they contains are same.
 

Input
There are multiply case.
In each case,there is a integer n(1 < = n < = 20)in a line.
In next n lines,there are two integers xi,yi(0 < = xi,yi < 9) in each line.
 

Output
Output the number of ways.
 

Sample Input
4 0 0 0 1 1 0 1 1
 

Sample Output
1



最后题目意思就是给你一系列点,问能组成正三角形,正四边形,正五边形,正六边形的个数(如果使用的点相同,则视为一种),实际上由于正三角形正五边形,正六边形不可能都由整数点(即横坐标和纵坐标都是整数)构成,故我们只用判断能组成多少个正方形即可。


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
struct point
{
    int  x,y;
} pp[110];
int s[6];
bool judge(int a,int b,int c,int d)
{
    int cnt=0;
    s[cnt++]=(pp[b].x-pp[a].x)*(pp[b].x-pp[a].x)+(pp[b].y-pp[a].y)*(pp[b].y-pp[a].y);
    s[cnt++]=(pp[c].x-pp[a].x)*(pp[c].x-pp[a].x)+(pp[c].y-pp[a].y)*(pp[c].y-pp[a].y);
    s[cnt++]=(pp[d].x-pp[a].x)*(pp[d].x-pp[a].x)+(pp[d].y-pp[a].y)*(pp[d].y-pp[a].y);
    s[cnt++]=(pp[c].x-pp[b].x)*(pp[c].x-pp[b].x)+(pp[c].y-pp[b].y)*(pp[c].y-pp[b].y);
    s[cnt++]=(pp[d].x-pp[b].x)*(pp[d].x-pp[b].x)+(pp[d].y-pp[b].y)*(pp[d].y-pp[b].y);
    s[cnt++]=(pp[d].x-pp[c].x)*(pp[d].x-pp[c].x)+(pp[d].y-pp[c].y)*(pp[d].y-pp[c].y);
    sort(s,s+6);//以上代码就是在求这四个点构成的边的长度,如果是正方形,那么排序后前四个大小一样,最后后两个大小一样
    if(s[0]==s[1]&&s[2]==s[1]&&s[2]==s[3]&&s[4]==s[5]&&s[4]!=s[3])return true;
    return false;

}
int main()
{
    int  t,i,j,n,k,l,cnt;
    while(scanf("%d",&n)!=EOF)
    {


        cnt=0;
        for(i=0; i<n; i++)
            scanf("%d%d",&pp[i].x,&pp[i].y);
        for(i=0; i<n; i++)
            for(j=i+1; j<n; j++)
                for(k=j+1; k<n; k++)
                    for(l=k+1; l<n; l++)
                        if(judge(i,j,k,l))cnt++;

       printf("%d\n",cnt);

    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值