Squares - poj 2002(hash)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
int x,y;
} point[]; struct hash{
int pos;
hash* next;
} hashtable[];
int find(int x,int y){ hash* t;
int tmp=(x*x+y*y)%;
t=&hashtable[tmp];
if(t->pos==){
return ;
}else{
int pos=t->pos;
if(point[pos].x==x&&point[pos].y==y)
return ;
}
while(t->next){
t=t->next;
int pos=t->pos;
if(point[pos].x==x&&point[pos].y==y)
return ;
}
return ;
}
int N;
int main() {
scanf("%d",&N);
int i,j;
while(N){
memset(hashtable,,sizeof(hashtable));
memset(point,,sizeof(point));
for(i=;i<=N;i++){
int x,y;
scanf("%d %d",&x,&y);
point[i].x=x;
point[i].y=y;
int t=(x*x+y*y)%;
if(hashtable[t].pos==){
hashtable[t].pos=i;
}else{
hash* tmp=(hash*)malloc(sizeof(hash));
tmp->next=;
tmp->pos=i;
tmp->next=hashtable[t].next;
hashtable[t].next=tmp;
}
}
int result=;
for(i=;i<=N;i++){
for(j=i+;j<=N;j++){
int x1=point[i].x+(point[j].y-point[i].y);
int y1=point[i].y-(point[j].x-point[i].x);
int x2=point[j].x-(point[i].y-point[j].y);
int y2=point[j].y+(point[i].x-point[j].x);
if(find(x1,y1)&&find(x2,y2))
result++;
}
}
for(i=;i<=N;i++){
for(j=i+;j<=N;j++){
int x1=point[i].x-(point[j].y-point[i].y);
int y1=point[i].y+(point[j].x-point[i].x);
int x2=point[j].x+(point[i].y-point[j].y);
int y2=point[j].y-(point[i].x-point[j].x);
if(find(x1,y1)&&find(x2,y2))
result++;
}
}
printf("%d\n",result/);
scanf("%d",&N);
} return ;
}
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 17553 | Accepted: 6677 |
Description
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
Output
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
Squares - poj 2002(hash)的更多相关文章
- POJ 2002 Squares 数学 + 必须hash
http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...
- POJ 2002 Squares [hash]
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 16631 Accepted: 6328 Descript ...
- POJ 2002 统计正方形 HASH
题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...
- POJ 2002 Squares 哈希
题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- POJ 2002 点hash
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15489 Accepted: 5864 Descript ...
- POJ 2002 Squares 几何, 水题 难度: 0
题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...
- poj 2002 Squares 几何二分 || 哈希
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 5749 Descript ...
- poj 2002(好题 链式hash+已知正方形两点求另外两点)
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 18493 Accepted: 7124 Descript ...
随机推荐
- os.waitpid()无法获取sys.exit()退出时的status code
[目的] 父进程使用os.waitpid()等待子进程退出,并检测子进程的exit code,以决定是否重启子进程. (常见的应用场景是:子进程接收外部命令,收到"stop"时退出 ...
- dbus启动失败:Couldn't connect to system bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
在没有开启x窗口的shell下启动dbus相关程序时会如上错误,详细原因如下: This is not considered to be a bug. Auto-launching D-Bus ses ...
- 【笔记】关于require.js 的用法
最近忙于学校的一个新网站建设,对于以前的前端程序编写方式的不正规特意上网学习了require.js 的用法,使此次的工程更加有条理同时符合当前前端的开发模式——前端模块化. 网上有不少很好的学习文章这 ...
- ResourceBundle的使用
ResourceBundle用来读取properties配置文件,配置文件的位置只能放到src根目录下,当然这个功能的目的是为了实现国际化. 代码如下: package com.comp.common ...
- iOS Programming - Views(视图 - 基本绘制,变换,平移,旋转,反转,倾斜)
1. Views A view (an object whose class is UIView or a subclass of UIView) knows how to draw itself i ...
- Android常用酷炫控件(开源项目)github地址汇总
转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...
- 详细解说 STL 排序(Sort)(转)
作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1.1 所有sort算法介绍 1.2 sort 中的比较函数 1.3 sor ...
- Android Touch事件传递机制引发的血案
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38942135 关于Android Touch事件传递机制我之前也写过两篇文章,自觉得对Tou ...
- SSH面试题目
一简答 spring工作原理 为什么要用spring 3.请你谈谈SSH整合 4.介绍一下Spring的事务管理 5.Struct2基本流程 6.在Hibernate应用中Java对象的状态有哪些? ...
- “ResGen.exe”已退出,代码为2 问题处理
这属于VS2010不能编译.Net3.5的问题 用VS2010创建了一个.Net 3.5的Winform项目,结果编译失败,这个问题也算是第二次碰到了,真纠结···这次不再偷懒了,把解决方法记录下来吧 ...