poj_2002 Squares(哈希)
来源:互联网 发布:任我行软件txyapp 编辑:程序博客网 时间:2024/06/03 22:58
Squares
Time Limit: 3500MS Memory Limit: 65536KTotal Submissions: 19231 Accepted: 7429
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.
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
题目要求找直角坐标系中所给点一共能组成多少个正方形。
在纸上模拟一下,比较容易发现已知两点(x1,y1),(x2,y2)能求出正方形中的另外两点(x3,y3),(x4,y4)
即 x3 = x1 - (y2 - y1),y3 = y1 + (x2 - x1),x4 = x2 - (y2 - y1),y4 = y2 + (x2 - x1)。
或 x3 = x1 + (y2 - y1),y3 = y1 - (x2 - x1),x4 = x2 + (y2 - y1),y4 = y2 - (x2 - x1)。
然后就可以用哈希表保存所有的点,然后枚举两点去找另外两点。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Node{ int x, y; int next;}node[maxn];int n;int cot;int hashTable[maxn];void initHash(){ cot = 0; memset(hashTable, -1, sizeof(hashTable));}int getHash(int x, int y){ return ((x * x) % mod + (y * y) % mod) % mod;}void insertHash(int x, int y){ int key = getHash(x, y); node[cot].x = x, node[cot].y = y; node[cot].next = hashTable[key]; hashTable[key] = cot++;}bool searchHash(int x, int y){ int key = getHash(x, y); int next = hashTable[key]; while(next != -1) { if(x == node[next].x && y == node[next].y) return true; next = node[next].next; } return false;}struct Point{ int x, y;}p[1010];int main(){ while(scanf("%d", &n) && n) { int ans = 0; initHash(); for(int i = 1; i <= n; i++) scanf("%d%d", &p[i].x, &p[i].y), insertHash(p[i].x, p[i].y); for(int i = 1; i <= n; i++) { for(int j = i+1; j <= n; j++) { int x1 = p[i].x + (p[j].y - p[i].y), y1 = p[i].y - (p[j].x - p[i].x); int x2 = p[j].x + (p[j].y - p[i].y), y2 = p[j].y - (p[j].x - p[i].x); int x3 = p[i].x - (p[j].y - p[i].y), y3 = p[i].y + (p[j].x - p[i].x); int x4 = p[j].x - (p[j].y - p[i].y), y4 = p[j].y + (p[j].x - p[i].x); if((searchHash(x1, y1) && searchHash(x2, y2))) ans++; if((searchHash(x3, y3) && searchHash(x4, y4))) ans++; } } printf("%d\n", ans / 4); } return 0;}
0 0
- poj_2002 Squares(哈希)
- poj_2002 哈希
- poj_2002
- (哈希)Squares (p2002)
- POJ2002《Squares》方法:哈希
- Squares - POJ 2002 哈希
- Squares - POJ 2002 哈希
- POJ 2002 Squares 哈希
- poj 2002 Squares 哈希
- POJ 2002 Squares 哈希
- POJ2002 Squares(hash)
- poj2002 Squares(hash)
- Counting Squares(1264)
- Squares
- Squares
- Squares
- Squares
- Squares
- sharedpreferences存储数据
- Java简单输入&通过控制台输入相关指令,输出System类, RunTime类相关方法返回信息
- Wifi 的Log分析
- SQL Having的用法
- NOIP2012复赛提高组day2(A:同余方程 B:借教室 C:疫情控制)
- poj_2002 Squares(哈希)
- Neo4j的Rest接口使用
- Android杂谈(19)Glide实践
- 最近感想20161102
- MySQL 连接错误Can't connect to MySQL server on (61)
- centOS release 6.5 git服务器搭建
- 136. Single Number
- UVA 12086 Potentiometers(树状数组)
- 野指针出现以及如何避免问题