poj2002--Squares(n个点求正方形个数)

Squares

Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 16615   Accepted: 6320

Description

A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon
with the latter property, however, as a regular octagon also has this property.

So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x
and y coordinates.

Input

The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers)
of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.

Output

For each test case, print on a line the number of squares one can form from the given stars.

Sample Input

4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0

Sample Output

1
6
1

给出n个点,求出有可以组成多少个正方形?

枚举对角的两个点,然后求解出其他的两个点,将这两个点带入到n个点中查找,可以hash 或 二分。

已知对角线的点 (x1,y1) (x3,y3)  求出中点( (x1+x3)/2 , (y1+y3)/2 ) ->(X,Y) 用对角线的一个点减去中点得到(x,y),那么其他的两个点就是( x1-Y,y1+X ) (x1+Y,y1-X)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std ;
#define eqs 1e-9
struct node{
    double x , y ;
}p[1100] ;
bool cmp(node a,node b)
{
    return ( a.x < b.x || ( a.x == b.x && a.y < b.y ) ) ;
}
bool judge(double x,double y,int n)
{
    int low = 0 , mid , high = n-1 ;
    while( low <= high )
    {
        mid = (low + high) / 2 ;
        if( fabs(p[mid].x-x) < eqs && fabs(p[mid].y-y) < eqs )
            return true ;
        else if( p[mid].x-x > eqs || ( fabs(p[mid].x-x) < eqs && p[mid].y-y > eqs ) )
            high = mid - 1 ;
        else
            low = mid + 1 ;
    }
    return false ;
}
int main()
{
    int n , i , j , num ;
    double x , y , xx , yy ;
    while( scanf("%d", &n) && n )
    {
        num = 0 ;
        for(i = 0 ; i < n ; i++)
        {
            scanf("%lf %lf", &p[i].x, &p[i].y) ;
        }
        sort(p,p+n,cmp) ;
        for(i = 0 ; i < n ; i++)
        {
            for(j = i+1 ; j < n ; j++)
            {
                if( i == j ) continue ;
                x = (p[i].x+p[j].x)/2 ;
                y = (p[i].y+p[j].y)/2 ;
                xx = p[i].x - x ;
                yy = p[i].y - y ;
                if( judge(x+yy,y-xx,n) && judge(x-yy,y+xx,n) )
                {
                    //printf("(%.1lf,%.1lf) (%.1lf,%.1lf) (%.1lf,%.1lf) (%.1lf,%.1lf)\n", p[i].x, p[i].y , p[j].x, p[j].y,x+yy,y-xx,x-yy,y+xx ) ;
                    num++ ;
                }
            }
        }
        printf("%d\n", num/2) ;
    }
    return 0;
}
时间: 2024-08-05 23:41:09

poj2002--Squares(n个点求正方形个数)的相关文章

hdu6055(求正方形个数)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6055 题意: 给出 n 组坐标 x, y, 输出其中的正多边形个数 . 其中 x, y 均为整数. 思路: x, y 为整数, 所以只存在正方形, 不会有其他正多边形 . 那么只需要枚举正方形的对角线即可 . 代码: 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include &l

Poj2002 Squares

题意描述:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少.相同的四个点,不同顺序构成的正方形视为同一正方形. 思路变迁: 1.最简单的方法,直接暴力搜索,即依次取四个顶点,根据其坐标判断是否能组成正方形.组成正方形的条件是四个顶点可组成的六条边里面,有四条相等,剩下两条相等.当然由于其时间复杂度为O(n^4),所以提交结果为TLE 2.考虑降低时间复杂度.如任取三个顶点,根据组成正方形的条件计算得到第四个顶点,判断其是否在点的集合内,其复杂度为O(n^3).或者任取两个顶点,根

Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是完全平方数 求有多少种方案 思路:每个数分解因子 每隔数可以选也可以不选 0 1表示 然后设有m种素数因子 选出的数组成的各个因子的数量必须是偶数 组成一个m行和n列的矩阵 每一行代表每一种因子的系数 解出自由元的数量 #include <cstdio> #include <cstring> #include <algorithm&g

数学:求一个数的真约数(因数)的个数及所有约数之和

一. 我们知道,每个自然数(不包括0和1)都有2个以上的因数,因数最少的是质数(也叫素数),质数的因数是1和它本身.非质数的自然数也叫合数,它们都含有3个以上(含3个)的因数. 1.怎样求一个数有多少个因数? 对于一个已知的自然数,要求出它有多少个因数,可用下列方法: 首先将这个已知数分解质因数,将此数化成几个质数幂的连乘形式,然后把这些质数的指数分别加一,再相乘,求出来的积就是我们要的结果. 例如:求360有多少个因数. 因为360分解质因数可表示为:360=2^3×3^2×5,2.3.5的指

写一个方法,求两个数的最大公约数和最小公倍数。

package homework0702; /* * 最大公约数 利用辗转相除法求解两个正整数的最大公约数 在循环中,只要除数不等于0,用较大的数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环较小的数,如此循环直到较小的数值为0,返回较大的数.即为最大公约数. 辗转相除法(欧几里得算法) 定理:两个整数的最大公约数等于其中较小的那个数和两数的相除余数的最大公约数.最大公约数(greatest common divisor)缩写为gcd. 最小公倍数 最小公倍数 = (a

求两个数的最大公约数

求两个数的最大公约数 问题:给定两个正整数a和b,求他们的最大公约数. 最简单的方法就是穷举法,如果a>b,那么依次计算1~b的所有整数是否是a和b的公约数. public static void main(String[] args) { long timer = System.currentTimeMillis(); System.out.println(getGCB(1000234234,1242342390)); System.out.println(System.currentTime

求两个数的最大公约数和最小公倍数

import java.util.Scanner; //求两个数的最大公约数,最小共倍数. public class CommonMaxDivisor { public static void main(String[] args){ Scanner scanner=new Scanner(System.in); int m=scanner.nextInt(); int n=scanner.nextInt(); scanner.close(); CommonMaxDivisor cmd=new

【C语言】求多个数中的最大值(可变参数列表)

求多个数中的最大值要求用可变参数列表: 代码如下: <span style="font-size:18px;">#include<stdio.h> #include<stdarg.h> int Max(int n,...) { int i=0; int max=0; va_list arg; va_start(arg,n); for(i=0;i<n;i++) { int val=va_arg(arg,int); if (val>max)

算法 - 求n个数的中位数(C++)

placeholder算法 - 求n个数的中位数(C++),布布扣,bubuko.com