Squares(哈希)
| Time Limit: 3500MS | Memory Limit: 65536K | |
| Total Submissions: 14328 | Accepted: 5393 |
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
题意:给出n个点的坐标,计算这些点可以构成多少个正方形,不同顺序的相同四个点被视为同一个正方形。 思路:这里n最大是1000,显然一个点一个点的枚举不行。参考了别人的结题报告,据说有这样的定理:
已知(x1,y1) 和 (x2,y2)
则
x3 = x1 + (y1-y2); y3 = y1 - (x1-x2);
x4 = x2 +(y1-y2; y4 = y2 - (x1-x2);
或
x3 = x1 - (y1-y2); y3 = y1 + (x1-x2);
x4 = x2 - (y1-y2); y4 = y2 + (x1-x2);
因此可以先枚举两个点,根据这两个点(点1 ,点2)的坐标可以得到另外两个点(点3, 点4),若在哈希表中能找得到这两个(点3, 点4),说明能构成一个正方形,
注意每个点被枚举了四次,最后要除以4;
再者就是找哈希函数,这里用的平方取余法,每输入一个点,就将这个点插入哈希表中;
这个题也受了 poj 3274的启发;
#include<stdio.h>
#include<string.h> const int prime = ;
struct node
{
int x,y;
}pos[]; struct HashTable
{
int x;
int y;
struct HashTable* next;
}*Hash[prime];//Hash[]是指针数组,存放地址;
int n; //插入哈希表
void hash_insert(int x, int y)
{
int key = (x*x + y*y)%prime;//平方求余法;
if(!Hash[key])
{
Hash[key] = new struct HashTable;
Hash[key]->x = x;
Hash[key]->y = y;
Hash[key]->next = NULL;
}
else
{
struct HashTable *tmp = Hash[key];
while(tmp->next)
tmp = tmp->next;//开放寻址,直到next为空
//插入新结点
tmp->next = new struct HashTable;
tmp->next->x = x;
tmp->next->y = y;
tmp->next->next = NULL;
}
}
bool find(int x, int y)
{
int key = (x*x+y*y)%prime;
if(!Hash[key])
return false;//key 对应的地址不存在,
else
{
struct HashTable *tmp = Hash[key];
while(tmp)
{
if(tmp->x == x && tmp->y == y)
return true;
tmp = tmp->next;
}
return false;
}
}
int main()
{
while(scanf("%d",&n)!= EOF)
{
int i,j;
if(n == ) break;
memset(Hash,,sizeof(Hash));
for(i = ; i < n; i++)
{
scanf("%d %d",&pos[i].x,&pos[i].y);
hash_insert(pos[i].x, pos[i].y);
}
int ans = ;
for(i = ; i < n-; i++)
{
for(j = i+; j < n; j++)
{
int x1 = pos[i].x, y1 = pos[i].y;
int x2 = pos[j].x, y2 = pos[j].y;
int add_x = x1-x2,add_y = y1-y2;
int x3 = x1+add_y;
int y3 = y1-add_x;
int x4 = x2+add_y;
int y4 = y2-add_x;
if(find(x3,y3) && find(x4,y4))
ans++; x3 = x1-add_y;
y3 = y1+add_x;
x4 = x2-add_y;
y4 = y2+add_x;
if(find(x3,y3) && find(x4,y4))
ans++;
}
}
printf("%d\n",ans/);
}
return ;
}
Squares(哈希)的更多相关文章
- Squares<哈希>
Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-d ...
- POJ-2002 Squares,哈希模板+数学公式!
Squares 题意:二维坐标轴给出n个点求有多少个正方形. 要是平时做比赛的话毫无疑问会 ...
- POJ 2002 Squares 哈希
题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...
- poj 2002 Squares 几何二分 || 哈希
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 5749 Descript ...
- URAL - 1486 Equal Squares 二维哈希+二分
During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...
- 【URAL 1486】Equal Squares(二维哈希+二分)
Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- PKU 2002 Squares(二维点哈希+平方求余法+链地址法)
题目大意:原题链接 给定平面上的N个点,求出这些点一共可以构成多少个正方形. 解题思路: 若正方形为ABCD,A坐标为(x1, y1),B坐标为(x2, y2),则很容易可以推出C和D的坐标.对于特定 ...
- poj Squares n个点,共能组成多少个正方形 二分 + 哈希
题目链接:http://poj.org/problem?id=2002 测试数据: 41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20 有 ...
随机推荐
- 零基础学习云计算及大数据DBA集群架构师【预科2015年12月14日周一】
1.第一天比较轻松,上午填表格,录指纹,拍照片,做自我介绍. 2.下午老师简单介绍了一下PC\交换机\路由器\塔式服务器\机架式服务器(1U\2U)\刀片服务器\磁带机 3.班主任陈老师朱老师,预科秦 ...
- 读取xml字符串
string strXml = @"<MessageData><pm_id>10</pm_id><pm_title>这是公司或者产品的名称&l ...
- Activity---Fragment---listView的实现
我们要做的是在Activity中加入一个ViewPager,利用ViewPager的适配器(继承于FragmentPagerAdapter)将Fragment加到其中,而我们在又在Fragment中又 ...
- PHP上传原理及应用
概要 1.FORM表现enctype属性 2.$_FILES系统函数 3.move_uploaded_file函数 4.is_uploaded_file函数 1.FORM标签的enctype属性 只有 ...
- 【转】iOS25彩票 幸运转盘
原文 : http://www.it165.net/pro/html/201409/21216.html 最终效果图: 各个view的关系图: 背景圆盘(需穴ky"http://www.it ...
- 移动端touchstar、touchmove、touchend 事件如果页面有滚动时不让触发 touchend 事件。
/*仅适用于内容中点击元素.对于拖动等元素,需要自行在页面处理. * 主要是绑定touchstart和touchmove事件,并判断用户按下之后手指移动了多少像素. * 如果手指移动距离小于10像素, ...
- Java hashCode 和 equals()
1 Object中定义的hashCode() public int hashCode() Returns a hash code value for the object. This method i ...
- J2se中的声音---AudioPlayer
1 package cn.gp.tools; import java.io.FileInputStream; import java.io.FileNotFoundException; import ...
- 英语学习[ZZ]
本文作者三年间从四级勉强及格到高级口译笔试210,口试232.找工作面试时给其口试的老外考官听了一分钟就说你的英语不用考了.虽不敢说方法一定是最好的,但从现在开始随便谁不要再去找学习资料,每天花两个钟 ...
- VC++读取资源中文件
//查找目标资源 HRSRC hResource = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MAINPROG), TEXT(& ...