hdu 2428(哈希)
来源:互联网 发布:流体仿真分析软件 编辑:程序博客网 时间:2024/06/06 00:45
Stars
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Problem Description
Lucy loves stars very much. There are N (1 <= N <= 1000) stars in the sky. Assume the sky is a flat plane. All of the stars lie on it with a location (x, y), -10000 <= x, y <= 10000. Now, Lucy wants you to tell her how many squares with each of four vertices formed by these stars, and the edges of the squares should parallel to the coordinate axes.
Input
The first line of input is the number of test case.
The first line of each test case contains an integer N. The following N lines each contains two integers x, y, meaning the coordinates of the stars. All the coordinates are different.
The first line of each test case contains an integer N. The following N lines each contains two integers x, y, meaning the coordinates of the stars. All the coordinates are different.
Output
For each test case, output the number of squares in a single line.
Sample Input
211 240 01 01 10 1
Sample Output
01解题思路:这道题目就是找到两点,然后查看另外两点是否存在。。。。典型的哈希AC:#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 1010;struct node{int x,y;}star[maxn];struct link{int id;int next;}Hash[maxn];int n,h[maxn*3],cnt;int calc(int x,int y){int key = x*x + y*y;return key % 1003;}void add(int id){int key = calc(star[id].x,star[id].y);Hash[cnt].id = id;Hash[cnt].next = h[key];h[key] = cnt++;}bool find(int x, int y){int key = calc(x,y);int p = h[key];while(p != -1){int t = Hash[p].id;if(star[t].x == x && star[t].y == y)return true;p = Hash[p].next;}return false;}int main(){int t;scanf("%d",&t);while(t--){scanf("%d",&n);memset(h,-1,sizeof(h));memset(Hash,-1,sizeof(Hash));for(int i = 1; i <= n; i++){scanf("%d%d",&star[i].x,&star[i].y);add(i);}int ans = 0;for(int i = 1; i <= n; i++)for(int j = i+1; j <= n; j++){if(star[i].x == star[j].x)//横坐标相等,往左右两面找正方形{int dy = abs(star[i].y - star[j].y);if(find(star[i].x + dy, star[i].y) && find(star[j].x + dy, star[j].y))ans++;if(find(star[i].x - dy, star[i].y) && find(star[j].x - dy, star[j].y))ans++;}else if(star[i].y == star[j].y) //纵坐标相等,往上下找正方形{int dx = abs(star[i].x - star[j].x);if(find(star[i].x, star[i].y + dx) && find(star[j].x, star[j].y + dx))ans++;if(find(star[i].x, star[i].y - dx) && find(star[j].x, star[j].y - dx))ans++;}}printf("%d\n",ans>>2);}return 0;}
0 0
- hdu 2428(哈希)
- hdu 1800 (哈希)
- HDU-6046 hash(哈希)
- HDU 2428
- hdu 2111(Saving HDU)
- hdu 2109(fighting hdu)
- HDU Saving HDU (贪心)
- HDU 4287 Intelligent IME(哈希)
- HDU 4821 String 解题报告(哈希)
- HDU 4334 Trouble(哈希|线性查找)
- HDU 1067 Gap(哈希+bfs)
- HDU 4908 BestCoder Sequence (哈希)
- hdu 1050+hdu 1789+hdu 3177(贪心)
- 排位赛dp总结(HDU 2955,HDU 1864,HDU 1421)
- DFS基础 (HDU 1010、HDU 1016、HDU 1072)
- hdu 3715 hdu 1816 hdu 4115 (2-sat)
- AC自动机小结 (HDU 2222,HDU 2896,HDU 3065)
- hdu---2428Stars
- 修复super block
- Maven实战学习2-初步使用
- SDWebImage
- C++ Primer Pluse(第6版)中文版 第12章编程练习第5题
- hdu 2048 神、上帝以及老天爷(错排)
- hdu 2428(哈希)
- C++虚函数多态
- bootstrap分页
- 1012 数字分类 PAT
- h5学习笔记(2)this 、循环赋值引用问题
- Linux内核版本信息获取
- JVM内存划分
- bootstrap按钮组(一)
- SVN:E175002 REPORT