博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2002 Squares 几何二分 || 哈希
阅读量:6477 次
发布时间:2019-06-23

本文共 3470 字,大约阅读时间需要 11 分钟。

Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 15137   Accepted: 5749

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

41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20

Sample Output

161

Source

 
1 /* 2 题意:给你1000个点的坐标(x,y),让你找出能 3             构成正方形的个数。 4 思路:由于是1000,则枚举两个点,求出相应的另外 5 两个点的坐标。然后用二分判断是否两个点都存在。 6  7  8 就个人而言,关键在  "求出相应的另外两个点的坐标" 9 设两个点a1,a2;10 由a1为中心,逆时针旋转求出11 a3.x=a1.y-a2.y+a1.x;12 a3.y=a2.x-a1.x+a1.y;13 由a2为中心,顺时针旋转求出14 a4.x=a1.y-a2.y+a2.x;15 a4.y=a2.x-a1.x+a2.y;16 由于被计算两次,所以除217 */18 #include
19 #include
20 #include
21 #include
22 #include
23 using namespace std;24 25 typedef struct26 {27 int x,y;28 }node;29 node a[1003];30 bool cmp(node n1,node n2)31 {32 if( n1.x!=n2.x )33 return n1.x
cur.x || ( a[mid].x==cur.x&&a[mid].y>cur.y))45 r=mid-1;46 if( a[mid].x==cur.x && a[mid].y==cur.y) return true;47 }48 return false;49 }50 int main()51 {52 int n,i,j,num;53 node a1,a2,a3,a4;54 while(scanf("%d",&n)>0)55 {56 if(n==0)break;57 for(i=1;i<=n;i++)58 scanf("%d%d",&a[i].x,&a[i].y);59 sort(a+1,a+1+n,cmp);60 61 for(i=1,num=0;i

 

哈希做法:

1 #include
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 8 const int INF = 1000; 9 typedef struct10 {11 int x,y;12 }node;13 struct hash14 {15 int x;16 int y;17 struct hash *next;18 };19 struct hash hash_table[1003];20 node a[INF+3];21 22 bool cmp(node n1,node n2)23 {24 if( n1.x!=n2.x )25 return n1.x
x=x;34 new_hash->y=y;//build 35 36 new_hash->next=hash_table[k].next;37 hash_table[k].next=new_hash;38 }39 bool found(int x,int y)40 {41 unsigned k=(x*x+y*y)%INF;42 struct hash *new_hash;43 new_hash=hash_table[k].next;44 while(new_hash!=NULL)45 {46 if(new_hash->x==x && new_hash->y==y)break;47 else new_hash=new_hash->next;48 }49 if(new_hash==NULL)return false;50 else return true;51 }52 53 int main()54 {55 int n,i,j,num;56 node a1,a2,a3,a4;57 while(scanf("%d",&n)>0)58 {59 if(n==0)break;60 memset(hash_table,0,sizeof(hash_table));61 for(i=1;i<=n;i++)62 {63 scanf("%d%d",&a[i].x,&a[i].y);64 Insert(a[i].x,a[i].y);65 }66 sort(a+1,a+1+n,cmp);67 68 for(i=1,num=0;i

 

转载于:https://www.cnblogs.com/tom987690183/p/3564375.html

你可能感兴趣的文章
三分 POJ 2420 A Star not a Tree?
查看>>
修改OBS为仅直播音频
查看>>
OCA读书笔记(3) - 使用DBCA创建Oracle数据库
查看>>
Python基础进阶之路(一)之运算符和输入输出
查看>>
阻塞非阻塞异步同步 io的关系
查看>>
ClickStat业务
查看>>
spring3.0.7中各个jar包的作用总结
查看>>
Windows 10 /win10 上使用GIT慢的问题,或者命令行反应慢的问题
查看>>
Windows平台分布式架构实践 - 负载均衡
查看>>
iOS自定制tabbar与系统的tabbar冲突,造成第一次点击各个item图片更换选中,第二次选中部分item图片不改变...
查看>>
SVN服务器使用(二)
查看>>
反射获取内部类以及调用内部类方法
查看>>
App里面如何正确显示用户头像
查看>>
U-BOOT之一:BootLoader 的概念与功能
查看>>
我的路上
查看>>
Velocity处理多余空白和多余空白行问题
查看>>
DB2与oracle有什么区别
查看>>
创建一个多级文件目录
查看>>
Picasa生成图片幻灯片页面图文教程
查看>>
svn status 显示 ~xx
查看>>