POJ2002:Squares

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

枚举两个相邻的点,求出另外两点,再看这两个点是否存在

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i<=y;i++)
#define mem(a,b) memset(a,b,sizeof(a))
#define w(a) while(a)
const int mod=20007;
int n,next[20007],head[20007],m,ans;
struct node
{
    int x,y;
} a[2222];
int cmp(node a,node b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
void insert(int i)
{
    int key=(a[i].x*a[i].x+a[i].y*a[i].y)%mod;
    next[m]=head[key];
    a[m].x=a[i].x;
    a[m].y=a[i].y;
    head[key]=m++;
}

int find(int x,int y)
{
    int key=(x*x+y*y)%mod;
    for(int i=head[key];i!=-1;i=next[i])
    if(a[i].x==x&&a[i].y==y)
    return i;
    return -1;
}

int main()
{
    int i,j;
    w((scanf("%d",&n),n))
    {
        mem(head,-1);
        mem(next,0);
        m=1005;
        ans=0;
        up(i,0,n-1)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            insert(i);
        }
        sort(a,a+n,cmp);
        up(i,0,n-1)
        {
            up(j,i+1,n-1)
            {
                int x1,y1,x2,y2;
                x1=a[i].x-a[j].y+a[i].y;
                y1=a[i].y+a[j].x-a[i].x;
                if(find(x1,y1)==-1)
                continue;
                x2=a[j].x-a[j].y+a[i].y;
                y2=a[j].y+a[j].x-a[i].x;
                if(find(x2,y2)==-1)
                continue;
                ans++;
            }
        }
        printf("%d\n",ans/2);
    }

    return 0;
}

POJ2002:Squares

时间: 2024-10-12 23:48:20

POJ2002:Squares的相关文章

poj2002-Squares(数学+哈希)

---恢复内容开始--- 题意:给n个点,问有多少组四个点能组成正方形. 题解:枚举两个点,通过公式算出另外两个点,然后通过哈希查找另外两个点存不存在. //突然发现好弱,好多基础的算法竟然都不会,哈希这种经典的算法,我貌似基本没怎么做过相关的题0.0 公式是抄网上的,哈希直接用了vector存的,反正时限3500ms 点的哈希就是(x^2+y^2)%MOD AC代码: /************************************** Memory: 924 KB Time: 96

Python学习笔记 (3) :列表、元组的操作

列表,即写在方括号之间.用逗号分隔开的数值列表.列表内的项目不必全是相同的类型. >>> a = ['spam', 'eggs', 100, 1234] >>> a ['spam', 'eggs', 100, 1234] >>> squares = [1, 4, 9, 16, 25] >>> squares [1, 4, 9, 16, 25] 像字符串一样,列表可以被索引和切片: <pre> >>> sq

第四章:操作列表

第四章:操作列表 4.1 遍历整个列表 如果名单很长,将包含大量反复的代码.另外,每当名单的长度发生变化时,都必须修改代码.通过for 循环,可让Python去处理这些问题 1)使用for循环来打印魔术师名单中的所有名字:  magicians.py 例:magicians=['alice','david','carolina'] for magician(变量名) in magicians: print(magician) 变量名 2)这行代码让Python从列表magicians中取出一个名

给有C或C++基础的Python入门 :Python Crash Course 4 操作列表 4.1--4.3

操作列表,也就是遍历列表.本章我们要学的就是如何遍历列表. 4.1--4.2 遍历列表 遍历列表,用for循环. 不同于C++或者C语言的for循环,Python的for循环更容易让人理解. 看一个例子: 1 fruits = ['apple', 'banana', 'orange']; 2 for fruit in fruits: 3 print("I like " + fruit.title() + ". "); 4 print("I really l

go 笔记

匿名函数 拥有函数名的函数只能在包级语法块中被声明,通过函数字面量(function literal),我们可绕过这一限制,在任何表达式中表示一个函数值.函数字面量的语法和函数声明相似,区别在于func关键字后没有函数名.函数值字面量是一种表达式,它的值被成为匿名函数(anonymous function). 函数字面量允许我们在使用时函数时,再定义它.通过这种技巧,我们可以改写之前对strings.Map的调用: strings.Map(func(r rune) rune { return r

委托,不知道你是否有也这样理解(二)

目录 泛型委托 简化语法 委托与反射 相关文章链接: 事件,你是否也这样理解  http://www.cnblogs.com/sunchong/p/sunchong.html 委托,你是否也这样理解(一) http://www.cnblogs.com/sunchong/p/3480612.html 一.泛型委托 我们知道泛型能够提高效率,避免代码重复,灵活. C#提供了无返回值的泛型委托:Action public delegate void Action<in T>(T obj); 从上我们

非正式介绍Python(二)

3.1.3. Lists Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of dif

python的列表(二)

1.遍历整个列表  #for 循环 # >>> name_list['faker', 'dopa', 'gogoing', 'uzi']  >>> for LOL_Player in name_list:... print(LOL_Player)fakerdopagogoinguzi 第一步,定义一个列表*name_list* 第二步,通过for语句遍历列表,结果存放到字符串LOL_Player 第三步,打印字符串 for 循环遍历的结果是把整个list中的所有元素读取

Python的第二天

一.while循环语句 Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务.其基本形式为: while 判断条件: 执行语句-- 执行语句可以是单个语句或语句块.判断条件可以是任何表达式,任何非零.或非空(null)的值均为true. 当判断条件假false时,循环结束. 实例: #!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count