poj 2002 -- Squares
Squares
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 15886 | Accepted: 6013 |
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
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
思路:
正方形已知两点(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;
计算出坐标后可以用hash查找。用静态邻接表模拟拉链。。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : Squares.cpp 4 * Creat time : 2014-07-25 09:41 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 20000 15 using namespace std; 16 struct Node 17 { 18 int x,y; 19 }node[1005]; 20 int head[2*M+5]; 21 struct EdgeNode{ 22 int to,next; 23 }; 24 EdgeNode Edges[2*M+5]; 25 26 void AddEdges(int i,int j,int cnt) 27 { 28 Edges[cnt].to = j; 29 Edges[cnt].next = head[i]; 30 head[i] = cnt; 31 } 32 bool judge(int x,int y) 33 { 34 for(int k = head[x]; k != -1; k = Edges[k].next){ 35 if(Edges[k].to == y) 36 return true; 37 } 38 return false; 39 } 40 int main(int argc,char *argv[]) 41 { 42 int n; 43 while(scanf("%d",&n)!=EOF && n){ 44 clr(node,0); 45 clr(head,-1); 46 clr(Edges,0); 47 int k = 0; 48 for(int i = 0; i < n; i++){ 49 scanf("%d%d",&node[i].x,&node[i].y); 50 AddEdges(M+node[i].x,M+node[i].y,k++); 51 } 52 int x1,x2,x3,x4,y1,y2,y3,y4; 53 int cnt = 0; 54 for(int i = 0; i < n; i++){ 55 for(int j = i+1; j < n; j++){ 56 x1 = node[i].x + node[i].y - node[j].y; 57 y1 = node[i].y - node[i].x + node[j].x; 58 x2 = node[j].x + node[i].y - node[j].y; 59 y2 = node[j].y - node[i].x + node[j].x; 60 if(judge(x1+M,y1+M)){ 61 if(judge(x2+M,y2+M)){ 62 cnt++; 63 } 64 } 65 x3 = node[i].x - node[i].y + node[j].y; 66 y3 = node[i].y + node[i].x - node[j].x; 67 x4 = node[j].x - node[i].y + node[j].y; 68 y4 = node[j].y + node[i].x - node[j].x; 69 if(judge(x3+M,y3+M)){ 70 if(judge(x4+M,y4+M)){ 71 cnt++; 72 } 73 } 74 } 75 } 76 printf("%d\n",cnt/4); 77 } 78 return 0; 79 }
Do one thing , and do it well !