1. 程式人生 > >18.12.20 DSA 正方形

18.12.20 DSA 正方形

描述

輸入

包括多組資料,每組資料的第一行是整點的個數n(1<=n<=1000),其後n行每行由兩個整陣列成,表示一個點的x、y座標。輸入保證一組資料中不會出現相同的點,且座標的絕對值小於等於20000。輸入以一組n=0的資料結尾。輸出對於每組輸入資料,輸出一個數,表示這組資料中的點可以組成的正方形的數量。

樣例輸入

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

樣例輸出

1
6
1

提示

40%的資料,座標絕對值小於等於1000

來源

Rocky Mountain 2004

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <set>
11 #include <vector>
12
#include <fstream> 13 #define maxn 200005 14 #define inf 999999 15 #define cha 127 16 #define eps 1e-6 17 #define oo 1503 18 using namespace std; 19 20 struct node { 21 int x, y; 22 node(int xx, int yy) :x(xx), y(yy) { } 23 node() { } 24 }; 25 node Hash[2003]; 26 node all[1003
]; 27 bool visited[2003]; 28 int n; 29 30 bool findval(node val) { 31 int first = ((val.x*val.x) % oo + (val.y*val.y) % oo) % oo; 32 while (Hash[first].x != val.x || Hash[first].y != val.y) { 33 if (visited[first]==false)return 0; 34 first++; 35 } 36 return 1; 37 } 38 39 void insert(node val) { 40 int first = ((val.x*val.x) % oo + (val.y*val.y) % oo) % oo; 41 while (visited[first]) 42 first++; 43 Hash[first] = val; 44 visited[first] = true; 45 } 46 47 void init() { 48 memset(Hash, 0, sizeof(Hash)); 49 memset(visited, 0, sizeof(visited)); 50 for (int i = 1; i <= n; i++) { 51 scanf("%d%d", &all[i].x, &all[i].y); 52 insert(all[i]); 53 } 54 int ans = 0; 55 for(int i=1;i<=n;i++) 56 for (int j = 1; j <= n; j++) { 57 if (i == j)continue; 58 int x1, x2, y1, y2; 59 int xg = all[j].x - all[i].x, yg = all[j].y - all[i].y; 60 x1 = all[i].x - yg, x2 = x1 + xg; 61 y1 = all[i].y + xg, y2 = y1 + yg; 62 if (findval(node(x1, y1)) && findval(node(x2, y2))) 63 ans++; 64 } 65 printf("%d\n", ans/4); 66 } 67 68 int main() { 69 while(scanf("%d",&n)&&n) 70 init(); 71 return 0; 72 }
View Code

直接貼原來的程式碼結果WA了……

果然MOOC資料太水